AttachFilesController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Threading.Tasks;
  11. using wispro.sp.entity;
  12. using wispro.sp.share;
  13. namespace wispro.sp.api.Controllers
  14. {
  15. [Route("api/[controller]/[action]")]
  16. [ApiController]
  17. public class AttachFilesController : ControllerBase
  18. {
  19. private readonly IWebHostEnvironment env;
  20. private readonly ILogger<AttachFilesController> logger;
  21. private readonly spDbContext Context;
  22. public AttachFilesController(IWebHostEnvironment env,
  23. ILogger<AttachFilesController> logger,spDbContext context)
  24. {
  25. this.env = env;
  26. this.logger = logger;
  27. this.Context = context;
  28. }
  29. [HttpPost]
  30. public async Task<ActionResult<IList<AttachFile>>> PostFile(
  31. [FromForm] IEnumerable<IFormFile> files)
  32. {
  33. var maxAllowedFiles = 3;
  34. long maxFileSize = 1024 * 1024 * 15;
  35. var filesProcessed = 0;
  36. var resourcePath = new Uri($"{Request.Scheme}://{Request.Host}/");
  37. List<AttachFile> uploadResults = new();
  38. foreach (var file in files)
  39. {
  40. var attachFile = new AttachFile();
  41. string trustedFileNameForFileStorage;
  42. var untrustedFileName = file.FileName;
  43. attachFile.Name = untrustedFileName;
  44. Staff uploadUser = Context.Staffs.Where<Staff>(s => s.Name == "何倚雯").FirstOrDefault();
  45. if (uploadUser != null)
  46. {
  47. attachFile.UploadUserId = uploadUser.Id;
  48. }
  49. var trustedFileNameForDisplay =
  50. WebUtility.HtmlEncode(untrustedFileName);
  51. if (filesProcessed < maxAllowedFiles)
  52. {
  53. if (file.Length == 0)
  54. {
  55. logger.LogInformation("{FileName} length is 0 (Err: 1)",
  56. trustedFileNameForDisplay);
  57. //uploadResult.ErrorCode = 1;
  58. }
  59. else if (file.Length > maxFileSize)
  60. {
  61. logger.LogInformation("{FileName} of {Length} bytes is " +
  62. "larger than the limit of {Limit} bytes (Err: 2)",
  63. trustedFileNameForDisplay, file.Length, maxFileSize);
  64. //uploadResult.ErrorCode = 2;
  65. }
  66. else
  67. {
  68. try
  69. {
  70. trustedFileNameForFileStorage = Path.GetRandomFileName();
  71. var attachfileSavePath = utility.ConfigHelper.GetSectionValue("AttachFileSavePath");
  72. var path = Path.Combine(attachfileSavePath,
  73. trustedFileNameForFileStorage);
  74. await using FileStream fs = new(path, FileMode.Create);
  75. await file.CopyToAsync(fs);
  76. attachFile.SavePath = path;
  77. logger.LogInformation("{FileName} saved at {Path}",
  78. trustedFileNameForDisplay, path);
  79. Context.AttachFiles.Add(attachFile);
  80. Context.SaveChanges();
  81. //uploadResult.Uploaded = true;
  82. //uploadResult.StoredFileName = trustedFileNameForFileStorage;
  83. }
  84. catch (IOException ex)
  85. {
  86. logger.LogError("{FileName} error on upload (Err: 3): {Message}",
  87. trustedFileNameForDisplay, ex.Message);
  88. //uploadResult.ErrorCode = 3;
  89. }
  90. }
  91. filesProcessed++;
  92. }
  93. else
  94. {
  95. logger.LogInformation("{FileName} not uploaded because the " +
  96. "request exceeded the allowed {Count} of files (Err: 4)",
  97. trustedFileNameForDisplay, maxAllowedFiles);
  98. //uploadResult.ErrorCode = 4;
  99. }
  100. uploadResults.Add(attachFile);
  101. }
  102. return new CreatedResult(resourcePath, uploadResults);
  103. }
  104. }
  105. }