Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LuFsFilesCrudController.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. get.ThrowIfNotSuccess();
  34. string type;
  35. var contentTypeProvider = new FileExtensionContentTypeProvider();
  36. if (!contentTypeProvider.TryGetContentType(id, out type))
  37. {
  38. type = "application/octet-stream";
  39. }
  40. var fullPath = Path.Combine(new FileInfo(GetBasePath()).FullName, get.Data.Path);
  41. var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  42. if (forceDownload)
  43. {
  44. return File(stream, type, get.Data.Path);
  45. }
  46. return new FileStreamResult(stream, type);
  47. }
  48. [HttpGet]
  49. [Route("[controller]/{id}")]
  50. public virtual LuApiWrapperDbo<LuFsFilesDbo> GetSingleById([Required]string id)
  51. {
  52. return Handle(Business.GetSingleById(id));
  53. }
  54. [HttpPost]
  55. [Route("[controller]")]
  56. public LuApiWrapperDbo<LuFsFilesDbo> AddDbo([Required]IFormFile file, string path)
  57. {
  58. return Handle(Business.AddDbo(new LuFsFilesAddDbo
  59. {
  60. File = file.OpenReadStream(),
  61. Path = string.IsNullOrEmpty(path) ? file.FileName : path
  62. }));
  63. }
  64. [HttpGet]
  65. [Route("[controller]")]
  66. public virtual LuApiWrapperDbo<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
  67. {
  68. return Handle(Business.GetMultiple(request));
  69. }
  70. [HttpPost]
  71. [Route("[controller]/{id}")]
  72. public virtual LuApiWrapperDbo<LuFsFilesDbo> EditSingleByIdDbo([Required]string id, IFormFile file, string path)
  73. {
  74. return Handle(Business.EditSingleByIdDbo(id, new LuFsFilesAddDbo
  75. {
  76. File = file?.OpenReadStream(),
  77. Path = path
  78. }));
  79. }
  80. [HttpDelete]
  81. [Route("[controller]/{id}")]
  82. public virtual LuApiWrapperDbo<LuFsFilesDbo> Delete([Required]string id)
  83. {
  84. return Handle(Business.DeleteSingleByIdDbo(id));
  85. }
  86. }
  87. }