VideosController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Options;
  6. namespace vCloud.Server.Videos.Controllers
  7. {
  8. /// <summary>
  9. /// upload video controller
  10. /// </summary>
  11. [Produces("application/json")]
  12. [Route("api/Videos")]
  13. public class VideosController : Controller
  14. {
  15. private string _tmpExtension ="tmp";
  16. protected AppSettings AppSettings { get; set; }
  17. public VideosController(IOptions<AppSettings> settings)
  18. {
  19. AppSettings = settings.Value;
  20. }
  21. [HttpGet]
  22. public string Get()
  23. {
  24. return "";
  25. }
  26. /// <summary>
  27. /// get server file md5 API
  28. /// </summary>
  29. /// <returns></returns>
  30. [HttpGet("GetRsume")]
  31. public string GetRsume()
  32. {
  33. var statusCode = 0;
  34. string responseMsg;
  35. try
  36. {
  37. var md5str = HttpContext.Request.Query["md5str"].FirstOrDefault();
  38. if (string.IsNullOrEmpty(md5str))
  39. {
  40. statusCode = -1;
  41. responseMsg = statusCode + "&";
  42. return responseMsg;
  43. }
  44. var clientid = HttpContext.Request.Query["clientid"].FirstOrDefault();
  45. var videoDirectory = AppSettings.StoageVideoFolder;
  46. if (!Directory.Exists(videoDirectory))
  47. {
  48. Directory.CreateDirectory(videoDirectory);
  49. }
  50. var path = Path.Combine(videoDirectory, md5str);
  51. var saveFilePath = path;
  52. if (System.IO.File.Exists(saveFilePath))
  53. {
  54. var fileLength = GetFileLength(saveFilePath);
  55. var fileName = Path.GetFileName(saveFilePath);
  56. var videoUrl = GetVideoUrl(fileName);
  57. responseMsg = fileLength + "&" + videoUrl;
  58. return responseMsg;
  59. }
  60. var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
  61. var tmppathWithoutExtension = Path.Combine(videoDirectory, fileNameWithoutExtension + "&" + clientid);
  62. var tmpFilePath = Path.ChangeExtension(tmppathWithoutExtension, _tmpExtension);
  63. if (System.IO.File.Exists(tmpFilePath))
  64. {
  65. //if other client uploading file
  66. var fileLength = GetFileLength(tmpFilePath);
  67. responseMsg = fileLength + "&";
  68. return responseMsg;
  69. }
  70. }
  71. catch (Exception e)
  72. {
  73. Console.WriteLine(e);
  74. statusCode = -1;
  75. }
  76. responseMsg = statusCode + "&";
  77. return responseMsg;
  78. }
  79. private string GetVideoUrl(string filename)
  80. {
  81. var playVideosServer = AppSettings.ServerUrl;
  82. if (AppSettings.ServerUrl.LastIndexOf('/') != AppSettings.ServerUrl.Length - 1)
  83. {
  84. playVideosServer += "/";
  85. }
  86. var videoUrl = playVideosServer + "Vod" + "/" + filename;
  87. return videoUrl;
  88. }
  89. /// <summary>
  90. /// upload video API eg.http://localhost:8891/api/Videos/UploadVideo
  91. /// </summary>
  92. /// <returns></returns>
  93. [HttpPost("UploadVideo")]
  94. public string UploadVideo()
  95. {
  96. try
  97. {
  98. var file = HttpContext.Request.Body;
  99. var filename = HttpContext.Request.Query["filename"].FirstOrDefault();
  100. var clientid = HttpContext.Request.Query["clientid"].FirstOrDefault();
  101. var videoDirectory = AppSettings.StoageVideoFolder;
  102. if (!Directory.Exists(videoDirectory))
  103. {
  104. Directory.CreateDirectory(videoDirectory);
  105. }
  106. var path = Path.Combine(videoDirectory, filename);
  107. var finish = this.SaveAs(path, file, clientid, out long endPosition);
  108. string videoUrl = string.Empty;
  109. if (finish)
  110. {
  111. videoUrl= GetVideoUrl(filename);
  112. }
  113. HttpContext.Response.StatusCode = 200;
  114. var responseMsg = endPosition + "&" + videoUrl;
  115. return responseMsg;
  116. }
  117. catch (Exception e)
  118. {
  119. Console.WriteLine(e);
  120. HttpContext.Response.StatusCode = 500;
  121. }
  122. return String.Empty;
  123. }
  124. /// <summary>
  125. /// save file to server
  126. /// </summary>
  127. /// <param name="saveFilePath"></param>
  128. /// <param name="stream"></param>
  129. /// <param name="endPosition"></param>
  130. /// <returns></returns>
  131. private bool SaveAs(string saveFilePath, System.IO.Stream stream, string clientid, out long endPosition)
  132. {
  133. bool finish = false;
  134. var targetFilePath = saveFilePath;
  135. var targetTmpFilePath = Path.ChangeExtension(targetFilePath, _tmpExtension);
  136. var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(saveFilePath);
  137. var dir = Path.GetDirectoryName(saveFilePath);
  138. var tmppathWithoutExtension = Path.Combine(dir, fileNameWithoutExtension + "&" + clientid);
  139. var tmpFilePath = Path.ChangeExtension(tmppathWithoutExtension, _tmpExtension);
  140. if (System.IO.File.Exists(targetFilePath))
  141. {
  142. if (System.IO.File.Exists(tmpFilePath))
  143. {
  144. System.IO.File.Delete(tmpFilePath);
  145. }
  146. endPosition = GetFileLength(targetFilePath);
  147. return true;
  148. }
  149. long lStartPos = 0;
  150. int startPosition = 0;
  151. endPosition = 0;
  152. long fileLength = 0;
  153. var contentRange = HttpContext.Request.Headers["Content-Range"].ToString();
  154. //bytes 10000-19999/1157632
  155. if (!string.IsNullOrEmpty(contentRange))
  156. {
  157. contentRange = contentRange.Replace("bytes", "").Trim();
  158. string[] ranges = contentRange.Split('/');
  159. string[] positioRanges = ranges[0].Split('-');
  160. startPosition = int.Parse(positioRanges[0]);
  161. endPosition = long.Parse(positioRanges[1]);
  162. fileLength = long.Parse(ranges[1]);
  163. }
  164. System.IO.FileStream fs;
  165. if (System.IO.File.Exists(saveFilePath))
  166. {
  167. fs = System.IO.File.OpenWrite(saveFilePath);
  168. lStartPos = fs.Length;
  169. }
  170. else
  171. {
  172. if (System.IO.File.Exists(tmpFilePath))
  173. {
  174. fs = System.IO.File.OpenWrite(tmpFilePath);
  175. lStartPos = fs.Length;
  176. }
  177. else
  178. {
  179. fs = new System.IO.FileStream(tmpFilePath, System.IO.FileMode.Create);
  180. lStartPos = 0;
  181. }
  182. saveFilePath = tmpFilePath;
  183. }
  184. if (lStartPos > endPosition)
  185. {
  186. fs.Close();
  187. endPosition = lStartPos;
  188. return finish;
  189. }
  190. else if (lStartPos < startPosition)
  191. {
  192. lStartPos = startPosition;
  193. }
  194. else if (lStartPos > startPosition && lStartPos < endPosition)
  195. {
  196. lStartPos = startPosition;
  197. }
  198. fs.Seek(lStartPos, System.IO.SeekOrigin.Current);
  199. var writeLength = 1024;
  200. byte[] nbytes = new byte[writeLength];
  201. int nReadSize = 0;
  202. nReadSize = stream.Read(nbytes, 0, writeLength);
  203. while (nReadSize > 0)
  204. {
  205. fs.Write(nbytes, 0, nReadSize);
  206. nReadSize = stream.Read(nbytes, 0, writeLength);
  207. }
  208. finish = fs.Length == fileLength;
  209. endPosition = fs.Length;
  210. fs.Close();
  211. if (finish)
  212. {
  213. if (System.IO.File.Exists(targetFilePath))
  214. {
  215. System.IO.File.Delete(saveFilePath);
  216. }
  217. if (System.IO.File.Exists(saveFilePath))
  218. {
  219. var tmpfileNameWithoutExtension = Path.GetFileNameWithoutExtension(saveFilePath);
  220. if (tmpfileNameWithoutExtension.Contains("&"))
  221. {
  222. System.IO.File.Move(saveFilePath, targetTmpFilePath);
  223. saveFilePath = targetTmpFilePath;
  224. }
  225. System.IO.File.Move(saveFilePath, targetFilePath);
  226. }
  227. }
  228. return finish;
  229. }
  230. private long GetFileLength(string filePath)
  231. {
  232. var fs = System.IO.File.OpenRead(filePath);
  233. var fslength = fs.Length;
  234. fs.Close();
  235. return fslength;
  236. }
  237. }
  238. }