You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuFsFilesCrudController.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.ComponentModel.DataAnnotations;
  2. using System.IO;
  3. using Luticate2.Utils.Dbo.Basic;
  4. using Luticate2.Utils.Dbo.FsFiles;
  5. using Luticate2.Utils.Dbo.PaginatedRequest;
  6. using Luticate2.Utils.Dbo.Result;
  7. using Luticate2.Utils.Interfaces;
  8. using Luticate2.Utils.Utils;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.StaticFiles;
  12. using Microsoft.Extensions.Options;
  13. namespace Luticate2.Utils.Controllers
  14. {
  15. public abstract class LuFsFilesCrudController<TBusiness> : LuController
  16. where TBusiness : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
  17. {
  18. protected readonly TBusiness Business;
  19. protected LuFsFilesCrudController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, TBusiness business) : base(luUtilsOptionsDbo)
  20. {
  21. Business = business;
  22. }
  23. protected abstract string GetBasePath();
  24. [HttpGet]
  25. [Route("[controller]/{id}/raw")]
  26. public virtual IActionResult GetSingleByIdRaw([Required]string id, bool forceDownload = false)
  27. {
  28. var get = Business.GetSingleById(id);
  29. if (get.Status == LuStatus.NotFound)
  30. {
  31. return StatusCode(404);
  32. }
  33. if (!get)
  34. {
  35. throw new LuResultException(get.To<object>());
  36. }
  37. string type;
  38. var contentTypeProvider = new FileExtensionContentTypeProvider();
  39. if (!contentTypeProvider.TryGetContentType(id, out type))
  40. {
  41. type = "application/octet-stream";
  42. }
  43. var fullPath = Path.Combine(new FileInfo(GetBasePath()).FullName, get.Data.Path);
  44. var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  45. if (forceDownload)
  46. {
  47. return File(stream, type, get.Data.Path);
  48. }
  49. return new FileStreamResult(stream, type);
  50. }
  51. [HttpGet]
  52. [Route("[controller]/{id}")]
  53. public virtual LuApiWrapperDbo<LuFsFilesDbo> GetSingleById([Required]string id)
  54. {
  55. return Handle(Business.GetSingleById(id));
  56. }
  57. [HttpPost]
  58. [Route("[controller]")]
  59. public LuApiWrapperDbo<LuFsFilesDbo> AddDbo([Required]IFormFile file, string path)
  60. {
  61. return Handle(Business.AddDbo(new LuFsFilesAddDbo
  62. {
  63. File = file.OpenReadStream(),
  64. Path = string.IsNullOrEmpty(path) ? file.FileName : path
  65. }));
  66. }
  67. [HttpGet]
  68. [Route("[controller]")]
  69. public virtual LuApiWrapperDbo<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
  70. {
  71. return Handle(Business.GetMultiple(request));
  72. }
  73. [HttpPost]
  74. [Route("[controller]/{id}")]
  75. public virtual LuApiWrapperDbo<LuFsFilesDbo> EditSingleByIdDbo([Required]string id, IFormFile file, string path)
  76. {
  77. return Handle(Business.EditSingleByIdDbo(id, new LuFsFilesAddDbo
  78. {
  79. File = file?.OpenReadStream(),
  80. Path = path
  81. }));
  82. }
  83. [HttpDelete]
  84. [Route("[controller]/{id}")]
  85. public virtual LuApiWrapperDbo<LuFsFilesDbo> Delete([Required]string id)
  86. {
  87. return Handle(Business.DeleteSingleByIdDbo(id));
  88. }
  89. }
  90. }