FileController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using FileStorage.Repository.SqlSugar;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.StaticFiles;
  6. using Microsoft.Extensions.Options;
  7. using SqlSugar;
  8. using XF.Domain.Authentications;
  9. namespace FileStorage.Host.Controllers
  10. {
  11. [ApiController]
  12. [Route("file")]
  13. public class FileController : ControllerBase
  14. {
  15. private readonly ILogger<FileController> _logger;
  16. private readonly IFileStorage _fileStorage;
  17. private readonly ISugarUnitOfWork<FileStorageDbContext> _uow;
  18. public FileController(
  19. ILogger<FileController> logger,
  20. IFileStorage fileStorage,
  21. ISugarUnitOfWork<FileStorageDbContext> uow
  22. )
  23. {
  24. _logger = logger;
  25. _fileStorage = fileStorage;
  26. _uow = uow;
  27. }
  28. /// <summary>
  29. /// 内部调用,不限制文件格式
  30. /// </summary>
  31. /// <param name="fileData"></param>
  32. /// <param name="source"></param>
  33. /// <returns></returns>
  34. [HttpPost("uploadinside")]
  35. public async Task<IActionResult> UploadInside(IFormFile fileData, string source)
  36. {
  37. var file = await _fileStorage.UploadAsync(fileData.FileName, fileData.Length, "", fileData.OpenReadStream(), source ?? string.Empty, false);
  38. var Path = await _fileStorage.GetFileUrlIndefinitelyAsync(file.Id, source ?? string.Empty);
  39. return Ok(new { Id = file.Id, fileName = file.FileName, Path = Path });
  40. }
  41. /// <summary>
  42. /// 限制文件格式
  43. /// </summary>
  44. /// <param name="fileData"></param>
  45. /// <param name="source"></param>
  46. /// <returns></returns>
  47. [HttpPost("upload")]
  48. public async Task<IActionResult> Upload(IFormFile fileData, string source)
  49. {
  50. var file = await _fileStorage.UploadAsync(fileData.FileName, fileData.Length, "", fileData.OpenReadStream(), source ?? string.Empty, true);
  51. var Path = await _fileStorage.GetFileUrlIndefinitelyAsync(file.Id, source ?? string.Empty);
  52. return Ok(new { Id = file.Id, fileName = file.FileName, Path = Path });
  53. }
  54. [HttpGet("getfileurl")]
  55. public async Task<IActionResult> GetFileUrl([FromQuery] UploadGetDto dto)
  56. {
  57. var uri = await _fileStorage.GetFileUrlAsync(dto.Id, dto.Source ?? string.Empty);
  58. return Ok(new { Uri = uri });
  59. }
  60. [HttpGet("downloadfile")]
  61. public async Task<IActionResult> DownLoadFile([FromQuery] UploadGetDto dto)
  62. {
  63. var (stream, content, fileName) = _fileStorage.DownLoadFile(dto.Id);
  64. if (stream != null)
  65. {
  66. return File(stream, content, fileName);
  67. }
  68. return null;
  69. }
  70. [HttpGet("delfile")]
  71. public async Task<bool> DelFile([FromQuery] UploadGetDto dto)
  72. {
  73. return await _fileStorage.DelFileAsync(dto.Id, dto.Source ?? string.Empty);
  74. }
  75. [HttpGet("file_path")]
  76. public async Task<IActionResult> GetFilePath([FromQuery] UploadGetDto dto)
  77. {
  78. var uri = await _fileStorage.GetFilePathAsync(dto.Id, dto.Source ?? string.Empty);
  79. return Ok(new { Path = uri });
  80. }
  81. [HttpGet("createdb")]
  82. public Task CreateDb()
  83. {
  84. var db = _uow.Db;
  85. db.DbMaintenance.CreateDatabase();
  86. db.CodeFirst.InitTables<FileMetadata>();
  87. return Task.CompletedTask;
  88. }
  89. /// <summary>
  90. /// 访问文件
  91. /// </summary>
  92. /// <param name="id"></param>
  93. /// <param name="expires"></param>
  94. /// <param name="clientid"></param>
  95. /// <param name="signature"></param>
  96. /// <returns></returns>
  97. [AllowAnonymous]
  98. [HttpGet("files")]
  99. public async Task Files(string id, string expires, string clientid, string signature)
  100. {
  101. var fullPath = await _fileStorage.GetFilePath(id, expires, clientid, signature);
  102. HttpContext.Response.ContentType = "application/octet-stream";
  103. await HttpContext.Response.SendFileAsync(fullPath);
  104. }
  105. /// <summary>
  106. /// 访问文件无限期
  107. /// </summary>
  108. /// <param name="id"></param>
  109. /// <param name="expires"></param>
  110. /// <param name="clientid"></param>
  111. /// <param name="signature"></param>
  112. /// <returns></returns>
  113. [AllowAnonymous]
  114. [HttpGet("files_indefinitely")]
  115. public async Task FilesIndefinitely(string id, string expires, string clientid, string signature)
  116. {
  117. var fullPath = await _fileStorage.GetFilePathIndefinitely(id, expires, clientid, signature);
  118. HttpContext.Response.ContentType = "application/octet-stream";
  119. await HttpContext.Response.SendFileAsync(fullPath);
  120. }
  121. [HttpGet("test")]
  122. [AllowAnonymous]
  123. public string Test()
  124. {
  125. return DateTime.Now.ToString();
  126. }
  127. }
  128. }