DefaultFileProvider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core.Service;
  7. public class DefaultFileProvider : ICustomFileProvider, ITransient
  8. {
  9. /// <summary>
  10. /// 构建文件的完整物理路径
  11. /// </summary>
  12. /// <param name="sysFile"></param>
  13. /// <returns></returns>
  14. private string BuildFullFilePath(SysFile sysFile)
  15. {
  16. var relativePath = Path.Combine(sysFile.FilePath ?? "", $"{sysFile.Id}{sysFile.Suffix}");
  17. return ResolveUnderWebRoot(relativePath);
  18. }
  19. /// <summary>
  20. /// 构建目录的完整物理路径
  21. /// </summary>
  22. /// <param name="relativePath"></param>
  23. /// <returns></returns>
  24. private string BuildFullDirectoryPath(string relativePath)
  25. {
  26. return ResolveUnderWebRoot(relativePath);
  27. }
  28. public static string ResolveUnderRoot(string rootPath, string relativePath)
  29. {
  30. if (string.IsNullOrWhiteSpace(rootPath)) throw new InvalidOperationException("WebRootPath 未配置");
  31. if (Path.IsPathRooted(relativePath)) throw Oops.Oh("非法文件路径");
  32. var root = Path.GetFullPath(rootPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
  33. + Path.DirectorySeparatorChar;
  34. var candidate = Path.GetFullPath(Path.Combine(root, relativePath ?? string.Empty));
  35. if (!candidate.StartsWith(root, StringComparison.OrdinalIgnoreCase))
  36. throw Oops.Oh("非法文件路径");
  37. return candidate;
  38. }
  39. private static string ResolveUnderWebRoot(string relativePath) =>
  40. ResolveUnderRoot(App.WebHostEnvironment.WebRootPath, relativePath);
  41. /// <summary>
  42. /// 确保目录存在
  43. /// </summary>
  44. /// <param name="directoryPath"></param>
  45. private void EnsureDirectoryExists(string directoryPath)
  46. {
  47. if (!Directory.Exists(directoryPath))
  48. Directory.CreateDirectory(directoryPath);
  49. }
  50. public Task DeleteFileAsync(SysFile sysFile)
  51. {
  52. var filePath = BuildFullFilePath(sysFile);
  53. if (File.Exists(filePath))
  54. File.Delete(filePath);
  55. return Task.CompletedTask;
  56. }
  57. public async Task<string> DownloadFileBase64Async(SysFile sysFile)
  58. {
  59. var realFile = BuildFullFilePath(sysFile);
  60. if (!File.Exists(realFile))
  61. {
  62. Log.Error($"DownloadFileBase64:文件[{realFile}]不存在");
  63. throw Oops.Oh($"文件[{sysFile.FilePath}]不存在");
  64. }
  65. byte[] fileBytes = await File.ReadAllBytesAsync(realFile);
  66. return Convert.ToBase64String(fileBytes);
  67. }
  68. public Task<FileStreamResult> GetFileStreamResultAsync(SysFile sysFile, string fileName)
  69. {
  70. var fullPath = BuildFullFilePath(sysFile);
  71. return Task.FromResult(new FileStreamResult(new FileStream(fullPath, FileMode.Open), "application/octet-stream")
  72. {
  73. FileDownloadName = fileName + sysFile.Suffix
  74. });
  75. }
  76. public Task<string?> GetPresignedDownloadUrlAsync(SysFile sysFile, int expireMinutes) =>
  77. Task.FromResult<string?>(null);
  78. public async Task<SysFile> UploadFileAsync(IFormFile file, SysFile newFile, string path, string finalName)
  79. {
  80. newFile.Provider = ""; // 本地存储 Provider 显示为空
  81. var directoryPath = BuildFullDirectoryPath(path);
  82. EnsureDirectoryExists(directoryPath);
  83. var realFile = Path.Combine(directoryPath, finalName);
  84. await using var stream = File.Create(realFile);
  85. await file.CopyToAsync(stream);
  86. newFile.Url = $"{newFile.FilePath}/{newFile.Id + newFile.Suffix}";
  87. return newFile;
  88. }
  89. }