videoController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using GxPress.Service.Interface.Video;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.StaticFiles;
  12. using Microsoft.Extensions.Caching.Distributed;
  13. using Microsoft.Net.Http.Headers;
  14. namespace GxPress.Api.AppControllers
  15. {
  16. /// <summary>
  17. /// 访问
  18. /// </summary>
  19. [Route("/api/app/video")]
  20. [ApiController]
  21. [Authorize]
  22. public class VideoController : ControllerBase
  23. {
  24. private readonly IWebHostEnvironment _environment;
  25. private readonly IDistributedCache _cache;
  26. private readonly IVideoService videoService;
  27. private IHttpContextAccessor _contextAccessor;
  28. private HttpContext _context { get { return _contextAccessor.HttpContext; } }
  29. public VideoController(IWebHostEnvironment environment, IDistributedCache cache, IVideoService videoService, IHttpContextAccessor contextAccessor)
  30. {
  31. _environment = environment;
  32. _cache = cache;
  33. this.videoService = videoService;
  34. _contextAccessor = contextAccessor;
  35. }
  36. // /// <summary>
  37. // /// 分页视频流
  38. // /// </summary>
  39. // /// <returns></returns>
  40. // [HttpGet]
  41. // [AllowAnonymous]
  42. // public IActionResult GetVideo(int page)
  43. // {
  44. // try
  45. // {
  46. // var path = Common.Tools.StringUtils.GetWebRootPath(_environment.WebRootPath);
  47. // var filepath = path + "/cache/20200428/1588144602647614.mp4";
  48. // FileInfo fileInfo = new FileInfo(filepath);
  49. // var key = $"15881446026476141.mp4{page}";
  50. // var result = _cache.Get(key);
  51. // if (result != null)
  52. // return File(result, "application/octet-stream", fileInfo.Name);
  53. // var blength = 1024 * 1024 * 1;
  54. // byte[] bytsize = new byte[blength];
  55. // //获取文件名后缀
  56. // string extension = Path.GetExtension(filepath);
  57. // //
  58. // videoService.RedisVideo(filepath, bytsize, blength, "15881446026476141.mp4");
  59. // //获取文件流
  60. // using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  61. // {
  62. // while (true)
  63. // {
  64. // int r = stream.Read(bytsize, 0, blength);
  65. // //如果读取到的字节数为0,说明已到达文件结尾,则退出while循
  66. // break;
  67. // }
  68. // stream.Close();
  69. // }
  70. // //获取后缀名
  71. // var ext = fileInfo.Extension;
  72. // new FileExtensionContentTypeProvider().Mappings.TryGetValue(ext, out var contenttype);
  73. // var actionResult = File(bytsize, contenttype ?? "application/octet-stream", fileInfo.Name);
  74. // return actionResult;
  75. // }
  76. // catch (FileNotFoundException e)
  77. // {
  78. // throw new Common.Exceptions.BusinessException(e.Message);
  79. // }
  80. // catch (IOException e)
  81. // {
  82. // throw new Common.Exceptions.BusinessException(e.Message);
  83. // }
  84. // }
  85. /// <summary>
  86. /// 分页视频流
  87. /// </summary>
  88. /// <returns></returns>
  89. [HttpHead]
  90. [AllowAnonymous]
  91. public IActionResult GetVideoInfo()
  92. {
  93. try
  94. {
  95. var path = Common.Tools.StringUtils.GetWebRootPath(_environment.WebRootPath);
  96. var filepath = path + "/cache/20200428/1588144602647614.mp4";
  97. FileInfo fileInfo = new FileInfo(filepath);
  98. byte[] bytsize = new byte[fileInfo.Length];
  99. var ext = fileInfo.Extension;
  100. new FileExtensionContentTypeProvider().Mappings.TryGetValue(ext, out var contenttype);
  101. var actionResult = File(bytsize, contenttype ?? "application/octet-stream", fileInfo.Name);
  102. return actionResult;
  103. }
  104. catch (FileNotFoundException e)
  105. {
  106. throw new Common.Exceptions.BusinessException(e.Message);
  107. }
  108. catch (IOException e)
  109. {
  110. throw new Common.Exceptions.BusinessException(e.Message);
  111. }
  112. }
  113. /// <summary>
  114. /// 分页视频流
  115. /// </summary>
  116. /// <returns></returns>
  117. [HttpGet]
  118. [AllowAnonymous]
  119. public IActionResult GetVideo()
  120. {
  121. var range = Request.Headers["Range"].ToString().Replace("bytes=", "");
  122. try
  123. {
  124. var path = Common.Tools.StringUtils.GetWebRootPath(_environment.WebRootPath);
  125. var filepath = path + "/cache/20200428/1588144602647614.mp4";
  126. //var filepath = "http://localhost:83/cache/20200428/1588144602647614.mp4";
  127. //从第3个到第12个字节,共10个字节。(0是第一个字节)
  128. var begin = int.Parse(range.Split('-')[0]);
  129. var end = int.Parse(range.Split('-')[1]);
  130. var byteLength = (end - begin)+1;
  131. var bytes = new byte[byteLength];
  132. //var fristBytes = new byte[1024];
  133. FileInfo fileInfo = new FileInfo(filepath);
  134. //获取后缀名
  135. var ext = fileInfo.Extension;
  136. new FileExtensionContentTypeProvider().Mappings.TryGetValue(ext, out var contenttype);
  137. //获取文件流
  138. StreamReader stream = new StreamReader(System.IO.File.OpenRead(filepath));
  139. //stream.BaseStream.Read(fristBytes, 0, 1024);
  140. stream.BaseStream.Seek(begin, SeekOrigin.Begin);
  141. stream.BaseStream.Read(bytes, 0, byteLength);
  142. stream.Close();
  143. //var newBytes = new byte[fristBytes.Length + bytes.Length];
  144. // fristBytes.CopyTo(newBytes, 0);
  145. // bytes.CopyTo(newBytes, fristBytes.Length);
  146. _context.Response.Headers.Add(HeaderNames.AcceptRanges, "bytes");
  147. _context.Response.Headers.Add(HeaderNames.ContentRange, $"bytes {Request.Headers["Range"].ToString()}/{fileInfo.Length}");
  148. _context.Response.StatusCode = 206;
  149. var actionResult = File(bytes, contenttype ?? "application/octet-stream", fileInfo.Name);
  150. return actionResult;
  151. }
  152. catch (Exception ex)
  153. {
  154. throw new Common.Exceptions.BusinessException($"{ex.Message}");
  155. }
  156. }
  157. }
  158. }