using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace vCloud.Server.Videos.Controllers
{
///
/// upload video controller
///
[Produces("application/json")]
[Route("api/Videos")]
public class VideosController : Controller
{
private string _tmpExtension ="tmp";
protected AppSettings AppSettings { get; set; }
public VideosController(IOptions settings)
{
AppSettings = settings.Value;
}
[HttpGet]
public string Get()
{
return "";
}
///
/// get server file md5 API
///
///
[HttpGet("GetRsume")]
public string GetRsume()
{
var statusCode = 0;
string responseMsg;
try
{
var md5str = HttpContext.Request.Query["md5str"].FirstOrDefault();
if (string.IsNullOrEmpty(md5str))
{
statusCode = -1;
responseMsg = statusCode + "&";
return responseMsg;
}
var clientid = HttpContext.Request.Query["clientid"].FirstOrDefault();
var videoDirectory = AppSettings.StoageVideoFolder;
if (!Directory.Exists(videoDirectory))
{
Directory.CreateDirectory(videoDirectory);
}
var path = Path.Combine(videoDirectory, md5str);
var saveFilePath = path;
if (System.IO.File.Exists(saveFilePath))
{
var fileLength = GetFileLength(saveFilePath);
var fileName = Path.GetFileName(saveFilePath);
var videoUrl = GetVideoUrl(fileName);
responseMsg = fileLength + "&" + videoUrl;
return responseMsg;
}
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
var tmppathWithoutExtension = Path.Combine(videoDirectory, fileNameWithoutExtension + "&" + clientid);
var tmpFilePath = Path.ChangeExtension(tmppathWithoutExtension, _tmpExtension);
if (System.IO.File.Exists(tmpFilePath))
{
//if other client uploading file
var fileLength = GetFileLength(tmpFilePath);
responseMsg = fileLength + "&";
return responseMsg;
}
}
catch (Exception e)
{
Console.WriteLine(e);
statusCode = -1;
}
responseMsg = statusCode + "&";
return responseMsg;
}
private string GetVideoUrl(string filename)
{
var playVideosServer = AppSettings.ServerUrl;
if (AppSettings.ServerUrl.LastIndexOf('/') != AppSettings.ServerUrl.Length - 1)
{
playVideosServer += "/";
}
var videoUrl = playVideosServer + "Vod" + "/" + filename;
return videoUrl;
}
///
/// upload video API eg.http://localhost:8891/api/Videos/UploadVideo
///
///
[HttpPost("UploadVideo")]
public string UploadVideo()
{
try
{
var file = HttpContext.Request.Body;
var filename = HttpContext.Request.Query["filename"].FirstOrDefault();
var clientid = HttpContext.Request.Query["clientid"].FirstOrDefault();
var videoDirectory = AppSettings.StoageVideoFolder;
if (!Directory.Exists(videoDirectory))
{
Directory.CreateDirectory(videoDirectory);
}
var path = Path.Combine(videoDirectory, filename);
var finish = this.SaveAs(path, file, clientid, out long endPosition);
string videoUrl = string.Empty;
if (finish)
{
videoUrl= GetVideoUrl(filename);
}
HttpContext.Response.StatusCode = 200;
var responseMsg = endPosition + "&" + videoUrl;
return responseMsg;
}
catch (Exception e)
{
Console.WriteLine(e);
HttpContext.Response.StatusCode = 500;
}
return String.Empty;
}
///
/// save file to server
///
///
///
///
///
private bool SaveAs(string saveFilePath, System.IO.Stream stream, string clientid, out long endPosition)
{
bool finish = false;
var targetFilePath = saveFilePath;
var targetTmpFilePath = Path.ChangeExtension(targetFilePath, _tmpExtension);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(saveFilePath);
var dir = Path.GetDirectoryName(saveFilePath);
var tmppathWithoutExtension = Path.Combine(dir, fileNameWithoutExtension + "&" + clientid);
var tmpFilePath = Path.ChangeExtension(tmppathWithoutExtension, _tmpExtension);
if (System.IO.File.Exists(targetFilePath))
{
if (System.IO.File.Exists(tmpFilePath))
{
System.IO.File.Delete(tmpFilePath);
}
endPosition = GetFileLength(targetFilePath);
return true;
}
long lStartPos = 0;
int startPosition = 0;
endPosition = 0;
long fileLength = 0;
var contentRange = HttpContext.Request.Headers["Content-Range"].ToString();
//bytes 10000-19999/1157632
if (!string.IsNullOrEmpty(contentRange))
{
contentRange = contentRange.Replace("bytes", "").Trim();
string[] ranges = contentRange.Split('/');
string[] positioRanges = ranges[0].Split('-');
startPosition = int.Parse(positioRanges[0]);
endPosition = long.Parse(positioRanges[1]);
fileLength = long.Parse(ranges[1]);
}
System.IO.FileStream fs;
if (System.IO.File.Exists(saveFilePath))
{
fs = System.IO.File.OpenWrite(saveFilePath);
lStartPos = fs.Length;
}
else
{
if (System.IO.File.Exists(tmpFilePath))
{
fs = System.IO.File.OpenWrite(tmpFilePath);
lStartPos = fs.Length;
}
else
{
fs = new System.IO.FileStream(tmpFilePath, System.IO.FileMode.Create);
lStartPos = 0;
}
saveFilePath = tmpFilePath;
}
if (lStartPos > endPosition)
{
fs.Close();
endPosition = lStartPos;
return finish;
}
else if (lStartPos < startPosition)
{
lStartPos = startPosition;
}
else if (lStartPos > startPosition && lStartPos < endPosition)
{
lStartPos = startPosition;
}
fs.Seek(lStartPos, System.IO.SeekOrigin.Current);
var writeLength = 1024;
byte[] nbytes = new byte[writeLength];
int nReadSize = 0;
nReadSize = stream.Read(nbytes, 0, writeLength);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = stream.Read(nbytes, 0, writeLength);
}
finish = fs.Length == fileLength;
endPosition = fs.Length;
fs.Close();
if (finish)
{
if (System.IO.File.Exists(targetFilePath))
{
System.IO.File.Delete(saveFilePath);
}
if (System.IO.File.Exists(saveFilePath))
{
var tmpfileNameWithoutExtension = Path.GetFileNameWithoutExtension(saveFilePath);
if (tmpfileNameWithoutExtension.Contains("&"))
{
System.IO.File.Move(saveFilePath, targetTmpFilePath);
saveFilePath = targetTmpFilePath;
}
System.IO.File.Move(saveFilePath, targetFilePath);
}
}
return finish;
}
private long GetFileLength(string filePath)
{
var fs = System.IO.File.OpenRead(filePath);
var fslength = fs.Length;
fs.Close();
return fslength;
}
}
}