using Furion.VirtualFileServer; using Microsoft.Extensions.Options; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; using YBEE.EQM.Core; namespace YBEE.EQM.Application; /// /// 资源文件管理服务 /// [ApiDescriptionSettings(Name = "file")] [Route("file")] public class ResourceFileAppService : IDynamicApiController { private readonly IResourceFileService _resourceFileService; private readonly EqmSiteOptions _eqmSiteOptions; private readonly string[] thumbImageTypes = { ".png", ".jpg", "jpeg", ".gif", ".bmp" }; public ResourceFileAppService(IResourceFileService resourceFileService, IOptions options) { _resourceFileService = resourceFileService; _eqmSiteOptions = options.Value; } /// /// 上传资源文件 /// /// /// [RequestSizeLimit(long.MaxValue)] [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)] public async Task Upload([FromForm] UploadResourceFileInput input) { return await _resourceFileService.Upload(input); } /// /// 根据文件ID下载文件 /// /// /// [HttpGet, NonUnify] public async Task Download([FromQuery][Required] long id) { var item = await _resourceFileService.GetById(id); var filePath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath); return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = item.FileName, }; } /// /// 通过URL /// /// /// [HttpGet, NonUnify, AllowAnonymous] public async Task View([FromQuery][Required] long id) { var item = await _resourceFileService.GetById(id); var filePath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath); if (File.Exists(filePath)) { var fbs = await File.ReadAllBytesAsync(filePath); FS.TryGetContentType(filePath, out var contentType); var ret = new FileContentResult(fbs, contentType); //{ // FileDownloadName = item.FileName //}; return ret; } throw Oops.Oh(ErrorCode.E1011); } }