123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System.ComponentModel.DataAnnotations;
- using System.IO;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.FsFiles;
- using Luticate2.Utils.Dbo.PaginatedRequest;
- using Luticate2.Utils.Dbo.Result;
- using Luticate2.Utils.Interfaces;
- using Luticate2.Utils.Utils;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.StaticFiles;
- using Microsoft.Extensions.Options;
-
- namespace Luticate2.Utils.Controllers
- {
- public abstract class LuFsFilesCrudController<TBusiness> : LuController
- where TBusiness : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
- {
- protected readonly TBusiness Business;
-
- protected LuFsFilesCrudController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, TBusiness business) : base(luUtilsOptionsDbo)
- {
- Business = business;
- }
-
- protected abstract string GetBasePath();
-
- [HttpGet]
- [Route("[controller]/{id}/raw")]
- public virtual IActionResult GetSingleByIdRaw([Required]string id, bool forceDownload = false)
- {
- var get = Business.GetSingleById(id);
- if (get.Status == LuStatus.NotFound)
- {
- return StatusCode(404);
- }
- if (!get)
- {
- throw new LuResultException(get.To<object>());
- }
- string type;
- var contentTypeProvider = new FileExtensionContentTypeProvider();
- if (!contentTypeProvider.TryGetContentType(id, out type))
- {
- type = "application/octet-stream";
- }
- var fullPath = Path.Combine(new FileInfo(GetBasePath()).FullName, get.Data.Path);
- var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
- if (forceDownload)
- {
- return File(stream, type, get.Data.Path);
- }
- return new FileStreamResult(stream, type);
- }
-
- [HttpGet]
- [Route("[controller]/{id}")]
- public virtual LuApiWrapperDbo<LuFsFilesDbo> GetSingleById([Required]string id)
- {
- return Handle(Business.GetSingleById(id));
- }
-
- [HttpPost]
- [Route("[controller]")]
- public LuApiWrapperDbo<LuFsFilesDbo> AddDbo([Required]IFormFile file, string path)
- {
- return Handle(Business.AddDbo(new LuFsFilesAddDbo
- {
- File = file.OpenReadStream(),
- Path = string.IsNullOrEmpty(path) ? file.FileName : path
- }));
- }
-
- [HttpGet]
- [Route("[controller]")]
- public virtual LuApiWrapperDbo<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
- {
- return Handle(Business.GetMultiple(request));
- }
-
- [HttpPost]
- [Route("[controller]/{id}")]
- public virtual LuApiWrapperDbo<LuFsFilesDbo> EditSingleByIdDbo([Required]string id, IFormFile file, string path)
- {
- return Handle(Business.EditSingleByIdDbo(id, new LuFsFilesAddDbo
- {
- File = file?.OpenReadStream(),
- Path = path
- }));
- }
-
- [HttpDelete]
- [Route("[controller]/{id}")]
- public virtual LuApiWrapperDbo<LuFsFilesDbo> Delete([Required]string id)
- {
- return Handle(Business.DeleteSingleByIdDbo(id));
- }
- }
- }
|