FileUploadService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using AipGateway.API.Domain.IServices.IUtilities;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.StaticFiles;
  5. using Microsoft.Extensions.Logging;
  6. using System.Net.Http.Headers;
  7. namespace AipGateway.API.Application.UtilityServices.FileUploadService
  8. {
  9. public class FileUploadService : IFileUploadService
  10. {
  11. private readonly ILogger<FileUploadService> _log;
  12. protected readonly string BaseUrl;
  13. protected readonly string PhysicalPath;
  14. public const string FolderName = "wwwroot//Uploads";
  15. private readonly IWebHostEnvironment _webHostEnvironment;
  16. private readonly IHttpContextAccessor _httpContext;
  17. public FileUploadService(ILogger<FileUploadService> logger, IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContext)
  18. {
  19. _log = logger;
  20. _webHostEnvironment = webHostEnvironment;
  21. _httpContext = httpContext;
  22. BaseUrl = $"{_httpContext.HttpContext.Request.Scheme}://{_httpContext.HttpContext.Request.Host}";
  23. PhysicalPath = _webHostEnvironment.ContentRootPath;
  24. if (!Directory.Exists(FolderName))
  25. {
  26. Directory.CreateDirectory(FolderName);
  27. }
  28. _log.LogError("FileUploadService: {0}, ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", FolderName);
  29. }
  30. public string GetMimeType(string fileName)
  31. {
  32. var provider = new FileExtensionContentTypeProvider();
  33. string contentType;
  34. if (!provider.TryGetContentType(fileName, out contentType))
  35. {
  36. contentType = "application/octet-stream";
  37. }
  38. return contentType;
  39. }
  40. public bool DeleteFile(string Image)
  41. {
  42. var pathToDelete = Path.Combine(BaseUrl, "wwwroot");
  43. var fullPathUrl = Path.Combine(pathToDelete, Image);
  44. var file = new FileInfo(fullPathUrl);
  45. if (file.Exists)
  46. {
  47. file.Delete();
  48. return true;
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. }
  55. public string GetFileCompleteUrl(string Image)
  56. {
  57. return $"{BaseUrl}//{Image}".Replace("\\", "//");
  58. }
  59. public string UploadFile(IFormFile file, string DirectoryName = "DEFAULT")
  60. {
  61. var pathToSave = $"{PhysicalPath}//{FolderName}//{DirectoryName}";
  62. var responsePath = $"{BaseUrl}//{FolderName}//{DirectoryName}";
  63. if (file.Length > 0)
  64. {
  65. var existingFileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Replace("\"", "");
  66. var fileExtension = Path.GetExtension(existingFileName);
  67. var fileName = existingFileName?.Replace(existingFileName, Guid.NewGuid().ToString());
  68. fileName = fileName.Insert(fileName.Length, fileExtension);
  69. var fullPath = $"{pathToSave}//{fileName}";
  70. UploadFile(fullPath, file);
  71. var filePathUsedToDisplay = $"{responsePath}//{fileName}";
  72. var filePathToSaveInDB = $"Uploads//{fileName}";
  73. return filePathToSaveInDB;
  74. }
  75. return string.Empty;
  76. }
  77. private static bool UploadFile(string savingPath, IFormFile file)
  78. {
  79. var check = false;
  80. if (!Directory.Exists(savingPath))
  81. {
  82. Directory.CreateDirectory(savingPath);
  83. }
  84. using (var stream = new FileStream(savingPath.Replace("//", "\\"), FileMode.Create))
  85. {
  86. file.CopyTo(stream);
  87. check = true;
  88. }
  89. return check;
  90. }
  91. }
  92. }