SysFileService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using Infrastructure.Extensions;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.IO;
  8. using System.Net;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using ZR.Common;
  13. using ZR.Model.System;
  14. using ZR.Service.System.IService;
  15. namespace ZR.Service.System
  16. {
  17. /// <summary>
  18. /// 文件管理
  19. /// </summary>
  20. [AppService(ServiceType = typeof(ISysFileService), ServiceLifetime = LifeTime.Transient)]
  21. public class SysFileService : BaseService<SysFile>, ISysFileService
  22. {
  23. private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
  24. private readonly ISysConfigService SysConfigService;
  25. private OptionsSetting OptionsSetting;
  26. public SysFileService(ISysConfigService sysConfigService, IOptions<OptionsSetting> options)
  27. {
  28. SysConfigService = sysConfigService;
  29. OptionsSetting = options.Value;
  30. }
  31. /// <summary>
  32. /// 存储本地
  33. /// </summary>
  34. /// <param name="fileDir">存储文件夹</param>
  35. /// <param name="rootPath">存储根目录</param>
  36. /// <param name="fileName">自定文件名</param>
  37. /// <param name="formFile">上传的文件流</param>
  38. /// <param name="userName"></param>
  39. /// <returns></returns>
  40. public async Task<SysFile> SaveFileToLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
  41. {
  42. string fileExt = Path.GetExtension(formFile.FileName);
  43. fileName = (fileName.IsEmpty() ? HashFileName() : fileName) + fileExt;
  44. string filePath = GetdirPath(fileDir);
  45. string finalFilePath = Path.Combine(rootPath, filePath, fileName);
  46. double fileSize = Math.Round(formFile.Length / 1024.0, 2);
  47. if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
  48. {
  49. Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
  50. }
  51. using (var stream = new FileStream(finalFilePath, FileMode.Create))
  52. {
  53. await formFile.CopyToAsync(stream);
  54. }
  55. string uploadUrl = OptionsSetting.Upload.UploadUrl;
  56. string accessPath = string.Concat(uploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
  57. SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, userName)
  58. {
  59. StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
  60. FileType = formFile.ContentType,
  61. FileUrl = finalFilePath.Replace("\\", "/"),
  62. AccessUrl = accessPath
  63. };
  64. file.Id = await InsertFile(file);
  65. return file;
  66. }
  67. /// <summary>
  68. /// 上传文件到阿里云
  69. /// </summary>
  70. /// <param name="file"></param>
  71. /// <param name="formFile"></param>
  72. /// <returns></returns>
  73. public async Task<SysFile> SaveFileToAliyun(SysFile file, IFormFile formFile)
  74. {
  75. file.FileName = (file.FileName.IsEmpty() ? HashFileName() : file.FileName) + file.FileExt;
  76. file.StorePath = GetdirPath(file.StorePath);
  77. string finalPath = Path.Combine(file.StorePath, file.FileName);
  78. HttpStatusCode statusCode = AliyunOssHelper.PutObjectFromFile(formFile.OpenReadStream(), finalPath, "");
  79. if (statusCode != HttpStatusCode.OK) return file;
  80. file.StorePath = file.StorePath;
  81. file.FileUrl = finalPath;
  82. file.AccessUrl = string.Concat(domainUrl, "/", file.StorePath.Replace("\\", "/"), "/", file.FileName);
  83. file.Id = await InsertFile(file);
  84. return file;
  85. }
  86. /// <summary>
  87. /// 获取文件存储目录
  88. /// </summary>
  89. /// <param name="storePath"></param>
  90. /// <param name="byTimeStore">是否按年月日存储</param>
  91. /// <returns></returns>
  92. public string GetdirPath(string storePath = "", bool byTimeStore = true)
  93. {
  94. DateTime date = DateTime.Now;
  95. string timeDir = date.ToString("yyyyMMdd");
  96. if (!string.IsNullOrEmpty(storePath))
  97. {
  98. timeDir = Path.Combine(storePath, timeDir);
  99. }
  100. return timeDir;
  101. }
  102. public string HashFileName(string str = null)
  103. {
  104. if (string.IsNullOrEmpty(str))
  105. {
  106. str = Guid.NewGuid().ToString();
  107. }
  108. MD5 md5 = MD5.Create();
  109. return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
  110. }
  111. public Task<long> InsertFile(SysFile file)
  112. {
  113. try
  114. {
  115. return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
  116. }
  117. catch (Exception ex)
  118. {
  119. Console.WriteLine("存储图片失败" + ex.Message);
  120. throw new Exception(ex.Message);
  121. }
  122. }
  123. }
  124. }