您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FilesController.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Diligent.WebAPI.Contracts.DTOs.File;
  2. using Diligent.WebAPI.Contracts.DTOs.Files;
  3. using Diligent.WebAPI.Host.Methods;
  4. namespace Diligent.WebAPI.Host.Controllers.V1
  5. {
  6. [ApiVersion("1.0")]
  7. [Route("v{version:apiVersion}/files")]
  8. [ApiController]
  9. public class FilesController : ControllerBase
  10. {
  11. private readonly ICategoryService _categoryService;
  12. private readonly ITagService _tagService;
  13. private readonly IFileEntityService _fileEntityService;
  14. private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;
  15. public FilesController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IFileEntityService fileEntityService, ICategoryService categoryService, ITagService tagService)
  16. {
  17. _hostingEnvironment = hostingEnvironment;
  18. _fileEntityService = fileEntityService;
  19. _categoryService = categoryService;
  20. _tagService = tagService;
  21. }
  22. [HttpGet]
  23. public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll());
  24. [HttpGet("filtered")]
  25. public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters));
  26. [HttpPost]
  27. public async Task<IActionResult> UploadPdf([FromForm] CreateFileRequest request)
  28. {
  29. if (request == null) throw new BadHttpRequestException("Request cannot be null");
  30. var filePath = await Upload.SaveFile(_hostingEnvironment.ContentRootPath, request.FileToUpload, "files");
  31. var category = await _categoryService.GetCategoryEntityById(request.CategoryId);
  32. if (category == null) throw new BadHttpRequestException("Category cannot found");
  33. List<Tag> tags = new();
  34. foreach (var id in request.TagsIds)
  35. tags.Add(await _tagService.GetTagEntityById(id));
  36. await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, Category = category, Tags = tags, Name = filePath, Extension = "pdf", Size = 100});
  37. return Ok();
  38. }
  39. }
  40. }