FileService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Http;
  4. using AI.Platform.Service.Common;
  5. using AI.Platform.Core.Entity;
  6. using Microsoft.AspNetCore.Components;
  7. using AI.Platform.Core;
  8. using static AI.Platform.Core.Entity.PublicEnum;
  9. using System.Net;
  10. using System;
  11. using AI.Platform.Service.Output;
  12. using Microsoft.AspNetCore.Http.HttpResults;
  13. using AI.Platform.Core.Entity.Site;
  14. namespace AI.Platform.Service;
  15. [ApiGroup(ApiGroupNames.System)]
  16. public class FileService : BaseService
  17. {
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. private readonly IHttpContextAccessor _contextAccessor;
  22. private readonly SqlSugarRepository<SiteEntity> _siteReponsitory;
  23. public FileService(IHttpContextAccessor contextAccessor, SqlSugarRepository<SiteEntity> siteReponsitory)
  24. {
  25. _contextAccessor = contextAccessor;
  26. _siteReponsitory = siteReponsitory;
  27. }
  28. /// <summary>
  29. /// 上传图片
  30. /// </summary>
  31. /// <param name="classification"></param>
  32. /// <returns></returns>
  33. [HttpPost, AllowAnonymous]
  34. public async Task<object> UploadImage(string classification = "default")
  35. {
  36. var host = _contextAccessor.GetHost();
  37. var file = _contextAccessor.HttpContext.Request.Form.Files[0];
  38. if (file.Length <= 0)
  39. {
  40. return "";
  41. }
  42. using var memoryStream = new MemoryStream();
  43. file.CopyTo(memoryStream);
  44. var fileBytes = memoryStream.ToArray();
  45. // 获取文件扩展名
  46. var fileExtension = Path.GetExtension(file.FileName);
  47. // 生成文件名
  48. var id = Guid.NewGuid().ToString().ToUpper().Replace("-", "");
  49. var fileName = $"{id}{fileExtension}";
  50. // 获取当前日期
  51. var currentDate = DateTime.Now;
  52. // 构建目录路径
  53. var directoryPath = Path.Combine("wwwroot", "image", classification, currentDate.ToString("yyyy"), currentDate.ToString("MM"), currentDate.ToString("dd"));
  54. // 创建目录
  55. if (!Directory.Exists(directoryPath))
  56. {
  57. Directory.CreateDirectory(directoryPath);
  58. }
  59. // 构建文件路径
  60. var filePath = Path.Combine(directoryPath, fileName);
  61. // 保存文件
  62. await System.IO.File.WriteAllBytesAsync(filePath, fileBytes);
  63. // 构建返回的相对路径
  64. var relativePath = $"/image/{classification}/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}";
  65. return relativePath;
  66. }
  67. /// <summary>
  68. /// 上传多媒体文件
  69. /// </summary>
  70. /// <param name="classification"></param>
  71. /// <returns></returns>
  72. [HttpPost]
  73. //[RequestSizeLimit(307_200_000)]
  74. public async Task<MediaFileUploadOutput> UploadMedia(IFormFile file,long siteId)
  75. {
  76. if (file == null || file.Length <= 0)
  77. {
  78. return new MediaFileUploadOutput()
  79. {
  80. isSuccess = false,
  81. msg = "上传文件为空"
  82. };
  83. }
  84. using var memoryStream = new MemoryStream();
  85. file.CopyTo(memoryStream);
  86. var fileBytes = memoryStream.ToArray();
  87. // 获取文件扩展名
  88. var fileExtension = Path.GetExtension(file.FileName);
  89. // 生成文件名
  90. var id = Guid.NewGuid().ToString().ToUpper().Replace("-", "");
  91. var fileName = $"{id}_{file.FileName}";
  92. string siteName = "未知站点";
  93. SiteEntity siteEntity = await _siteReponsitory.AsQueryable().Where(it => it.Id == siteId).FirstAsync();
  94. if(siteEntity != null)
  95. {
  96. siteName = siteEntity.Name;
  97. }
  98. // 构建目录路径
  99. var directoryPath = Path.Combine("wwwroot", "madia", siteName);
  100. // 创建目录
  101. if (!Directory.Exists(directoryPath))
  102. {
  103. Directory.CreateDirectory(directoryPath);
  104. }
  105. // 构建文件路径
  106. var filePath = Path.Combine(directoryPath, fileName);
  107. // 保存文件
  108. await System.IO.File.WriteAllBytesAsync(filePath, fileBytes);
  109. //// 构建返回的相对路径
  110. //var relativePath = $"/image/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}";
  111. //return relativePath;
  112. return new MediaFileUploadOutput()
  113. {
  114. isSuccess = true,
  115. msg = "上传成功",
  116. fileName = file.FileName,
  117. guidName = fileName,
  118. savePath = filePath,
  119. extension = fileExtension
  120. };
  121. }
  122. }