123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System;
- using System.IO;
- using System.Linq;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- namespace vCloud.Server.Videos.Controllers
- {
- /// <summary>
- /// upload video controller
- /// </summary>
- [Produces("application/json")]
- [Route("api/Videos")]
- public class VideosController : Controller
- {
- private string _tmpExtension ="tmp";
- protected AppSettings AppSettings { get; set; }
- public VideosController(IOptions<AppSettings> settings)
- {
- AppSettings = settings.Value;
- }
- [HttpGet]
- public string Get()
- {
- return "";
- }
- /// <summary>
- /// get server file md5 API
- /// </summary>
- /// <returns></returns>
- [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;
- }
- /// <summary>
- /// upload video API eg.http://localhost:8891/api/Videos/UploadVideo
- /// </summary>
- /// <returns></returns>
- [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;
- }
- /// <summary>
- /// save file to server
- /// </summary>
- /// <param name="saveFilePath"></param>
- /// <param name="stream"></param>
- /// <param name="endPosition"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|