MediaService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using AI.Platform.Core;
  2. using AI.Platform.Core.Entity.Device;
  3. using AI.Platform.Core.Entity.Media;
  4. using AI.Platform.Core.Entity.Site;
  5. using AI.Platform.Service.Common;
  6. using AI.Platform.Service.Output;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. using System;
  11. using System.IO;
  12. using System.Net;
  13. using System.Net.Http;
  14. using System.Net.Http.Headers;
  15. using System.Text.Json;
  16. using System.Threading.Tasks;
  17. namespace AI.Platform.Service;
  18. [ApiGroup(ApiGroupNames.Media)]
  19. public class MediaService:BaseService
  20. {
  21. private readonly SqlSugarRepository<MediaEntity> _mediaRepository;
  22. private readonly SqlSugarRepository<SiteEntity> _siteRepository;
  23. private readonly SqlSugarRepository<ScreentEntity> _screenRepository;
  24. private readonly IHttpContextAccessor _contextAccessor;
  25. public MediaService(IHttpContextAccessor contextAccessor, SqlSugarRepository<MediaEntity> mediaRepository, SqlSugarRepository<SiteEntity> siteRepository, SqlSugarRepository<ScreentEntity> screenRepository)
  26. {
  27. _contextAccessor = contextAccessor;
  28. _mediaRepository = mediaRepository;
  29. _siteRepository = siteRepository;
  30. _screenRepository = screenRepository;
  31. }
  32. /// <summary>
  33. /// 查询可播放广告
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpGet]
  37. public async Task<List<PlayAbleMedia>> Playable()
  38. {
  39. var SN = _contextAccessor.HttpContext.Request.Headers["DeviceSn"];
  40. var playAbleMedias = await _screenRepository.AsQueryable()
  41. .LeftJoin<SiteEntity>((scrren, site) => scrren.SiteId == site.Id)
  42. .LeftJoin<MediaEntity>((scrren, site, media) => site.Id == media.BusinessUnitId)
  43. .Where((scrren, site, media) => scrren.sn == SN)
  44. .Where((scrren, site, media) => media.MediaState == 1)
  45. .Where((scrren, site, media) => media.EffectiveTime <= DateTime.Now && media.FailureTime >= DateTime.Now)
  46. .Select((scrren, site, media) => new
  47. {
  48. media.Id,
  49. media.GuidFileName,
  50. media.StartTime,
  51. media.EndTime,
  52. media.MachineState
  53. })
  54. .ToListAsync();
  55. var result = playAbleMedias.Select(media => new PlayAbleMedia()
  56. {
  57. id = media.Id,
  58. guidFileName = media.GuidFileName,
  59. startTime = media.StartTime,
  60. endTime = media.EndTime,
  61. machineState = JsonSerializer.Deserialize<List<string>>(media.MachineState),
  62. }).ToList();
  63. return result;
  64. }
  65. /// <summary>
  66. /// 查询可播放广告
  67. /// </summary>
  68. /// <returns></returns>
  69. [HttpGet("api/Media/download/{id}")]
  70. public async Task<IActionResult> DownloadFile(long id)
  71. {
  72. MediaEntity mediaEntity = await _mediaRepository.AsQueryable()
  73. .Where(it => it.Id == id)
  74. .FirstAsync();
  75. // 假设文件存放在 wwwroot/files 目录下
  76. var filePath = mediaEntity.LocalPath;
  77. if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
  78. {
  79. return new ObjectResult(new
  80. {
  81. Message = "没找到文件",
  82. StatusCode = StatusCodes.Status204NoContent
  83. });
  84. }
  85. byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
  86. var response = new HttpResponseMessage(HttpStatusCode.OK)
  87. {
  88. Content = new ByteArrayContent(fileBytes)
  89. };
  90. FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  91. //_contextAccessor.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  92. string contentType = GetContentType(Path.GetExtension(mediaEntity.FileExtension));
  93. return new FileStreamResult(fileStream, contentType)
  94. {
  95. FileDownloadName = mediaEntity.GuidFileName,
  96. };
  97. }
  98. private string GetContentType(string fileExtension)
  99. {
  100. switch (fileExtension.ToLower())
  101. {
  102. case ".txt":
  103. return "text/plain";
  104. case ".html":
  105. case ".htm":
  106. return "text/html";
  107. case ".css":
  108. return "text/css";
  109. case ".js":
  110. return "application/javascript";
  111. case ".json":
  112. return "application/json";
  113. case ".jpg" or ".jpeg":
  114. return "image/jpeg";
  115. case ".png":
  116. return "image/png";
  117. case ".gif":
  118. return "image/gif";
  119. case ".rar":
  120. return "application/x-rar-compressed";
  121. case ".zip":
  122. return "application/zip";
  123. default:
  124. return "application/octet-stream";
  125. }
  126. }
  127. }