DefaultFileStorage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Microsoft.AspNetCore.StaticFiles;
  2. using Microsoft.Extensions.Options;
  3. using XF.Domain.Dependency;
  4. namespace FileStorage;
  5. public class DefaultFileStorage : IFileStorage, IScopeDependency
  6. {
  7. private readonly IFileMetadataRepository _fileMetadataRepository;
  8. private readonly IOptionsSnapshot<StorageConfiguration> _fileUploadOptions;
  9. public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository,IOptionsSnapshot<StorageConfiguration> optionsSnapshot)
  10. {
  11. _fileMetadataRepository = fileMetadataRepository;
  12. _fileUploadOptions = optionsSnapshot;
  13. }
  14. /// <summary>
  15. /// 本地上传
  16. /// </summary>
  17. /// <param name="fileName"></param>
  18. /// <param name="length"></param>
  19. /// <param name="extraInfo"></param>
  20. /// <param name="fileData"></param>
  21. /// <param name="client"></param>
  22. /// <returns></returns>
  23. public FileMetadata Upload(string fileName, long length, string extraInfo, Stream fileData, string client)
  24. {
  25. if (fileName == null)
  26. {
  27. fileName = "unkown";
  28. }
  29. fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
  30. var rv = UploadLoca(fileName, length, extraInfo, fileData,client);
  31. if (!string.IsNullOrEmpty(rv))
  32. {
  33. FileMetadata fileModel = new FileMetadata();
  34. fileModel.Client = client;
  35. fileModel.FileName = fileName;
  36. var ext = string.Empty;
  37. if (string.IsNullOrEmpty(fileName) == false)
  38. {
  39. var dotPos = fileName.LastIndexOf('.');
  40. ext = fileName[(dotPos + 1)..];
  41. }
  42. fileModel.FileExt = ext;
  43. fileModel.Path = rv;
  44. fileModel.Length = length;
  45. fileModel.SaveMode = _fileUploadOptions.Value.Impt;
  46. fileModel.ExtraInfo = extraInfo;
  47. fileModel.UploadTime = DateTime.Now;
  48. _fileMetadataRepository.AddAsync(fileModel);
  49. return fileModel;
  50. }
  51. return null;
  52. }
  53. public async Task<Uri> GetFileUrlAsync(string id)
  54. {
  55. var fileMetadata = await _fileMetadataRepository.GetAsync(id);
  56. if (fileMetadata != null)
  57. {
  58. return new Uri(_fileUploadOptions.Value.Local.VisDomain+fileMetadata.Path);
  59. }
  60. return new Uri("");
  61. }
  62. public (Stream stream, string contentType,string fileName) DownLoadFile(string id)
  63. {
  64. var fileMetadata = _fileMetadataRepository.Get(id);
  65. if (fileMetadata!=null)
  66. {
  67. string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
  68. var contentType = TaskGetFileContentTypeAsync(fileMetadata.Path);
  69. var stream = File.OpenRead(filePath);
  70. return (stream,contentType,fileMetadata.FileName);
  71. }
  72. return (null,"","");
  73. }
  74. public async Task<bool> DelFileAsync(string id)
  75. {
  76. var fileMetadata = await _fileMetadataRepository.GetAsync(id);
  77. if (fileMetadata!=null)
  78. {
  79. string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
  80. await _fileMetadataRepository.RemoveAsync(id);
  81. File.Delete(filePath);
  82. return true;
  83. }
  84. return false;
  85. }
  86. private string TaskGetFileContentTypeAsync(string fileName)
  87. {
  88. string suffix = Path.GetExtension(fileName);
  89. var provider = new FileExtensionContentTypeProvider();
  90. var contentType = provider.Mappings[suffix];
  91. return contentType;
  92. }
  93. private string UploadLoca(string fileName, long length, string extraInfo, Stream fileData,string client)
  94. {
  95. var settings = _fileUploadOptions.Value.Local;
  96. string groupDir = "";
  97. if (settings != null)
  98. {
  99. groupDir = settings.Path;
  100. }
  101. if (string.IsNullOrEmpty(groupDir))
  102. {
  103. groupDir = "uploads";
  104. }
  105. string sub = DateTime.Now.ToString("yyyyMMdd");
  106. groupDir = Path.Combine(groupDir,client, sub);
  107. string pathHeader = groupDir;
  108. string fulldir = GetFullPath(pathHeader);
  109. if (!Directory.Exists(fulldir))
  110. {
  111. Directory.CreateDirectory(fulldir);
  112. }
  113. var ext = string.Empty;
  114. if (!string.IsNullOrEmpty(fileName))
  115. {
  116. var dotPos = fileName.LastIndexOf('.');
  117. ext = fileName.Substring(dotPos + 1);
  118. }
  119. var filename = $"{Guid.NewGuid().ToString().Replace("-", string.Empty)}.{ext}";
  120. var fullPath = Path.Combine(fulldir, filename);
  121. using (var fileStream = File.Create(fullPath))
  122. {
  123. fileData.CopyTo(fileStream);
  124. }
  125. fileData.Dispose();
  126. return (Path.Combine(pathHeader, filename));
  127. }
  128. private string GetFullPath(string path)
  129. {
  130. string rv = "";
  131. if (path.StartsWith("."))
  132. {
  133. rv = Path.Combine(Directory.GetCurrentDirectory(), path);
  134. }
  135. else
  136. {
  137. rv = path;
  138. }
  139. rv = Path.GetFullPath(rv);
  140. return rv;
  141. }
  142. }