feat: 添加文件模块

This commit is contained in:
橙子
2023-12-15 00:11:08 +08:00
parent f8ee7bc015
commit ba97bd3eb5
5 changed files with 299 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Helper;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Application.FileManger;
using Yi.Framework.Rbac.Domain.Entities;
namespace Yi.Framework.Rbac.Application.Services
{
public class FileService : ApplicationService, IFileService
{
private readonly IRepository<FileAggregateRoot> _repository;
private readonly HttpContext _httpContext;
private IGuidGenerator _guidGenerator;
public FileService(IRepository<FileAggregateRoot> repository, IHttpContextAccessor httpContextAccessor, IGuidGenerator guidGenerator)
{
_guidGenerator = guidGenerator;
_repository = repository;
_httpContext = httpContextAccessor.HttpContext;
}
/// <summary>
/// 下载文件,是否缩略图
/// </summary>
/// <returns></returns>
[Route("file/{code}/{isThumbnail?}")]
public async Task<IActionResult> Get([FromRoute] Guid code, [FromRoute] bool? isThumbnail)
{
var path = await GetReturnPathAsync(code, isThumbnail);
var steam = await File.ReadAllBytesAsync(path);
//考虑从路径中获取
var fileContentType = MimeHelper.GetMimeMapping(Path.GetFileName(path));
//设置附件下载,下载名称
//_httpContext.FileAttachmentHandle(file.FileName);
return new FileContentResult(steam, fileContentType ?? @"text/plain");
}
public async Task<string> GetReturnPathAsync(Guid code, bool? isThumbnail)
{
var file = await _repository.GetAsync(x => x.Id == code);
if (file is null)
{
throw new UserFriendlyException("文件编号未匹配", "404");
}
var path = file.FilePath;
//如果为缩略图,需要修改路径
if (isThumbnail is true)
{
path = $"wwwroot/{FileTypeEnum.Thumbnail}/{file.Id}{Path.GetExtension(file.FileName)}";
}
//路径为: 文件路径/文件id+文件扩展名
if (!File.Exists(path))
{
throw new UserFriendlyException("本地文件不存在", "404");
}
return path;
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public async Task<List<FileGetListOutputDto>> Post([FromForm] IFormFileCollection file)
{
if (file.Count() == 0)
{
throw new ArgumentException("文件上传为空!");
}
//批量插入
List<FileAggregateRoot> entities = new();
foreach (var f in file)
{
FileAggregateRoot data = new(_guidGenerator.Create());
data.FileSize = (decimal)f.Length / 1024;
data.FileName = f.FileName;
var type = MimeHelper.GetFileType(f.FileName);
//落盘文件文件名为雪花id+自己的扩展名
string filename = data.Id.ToString() + Path.GetExtension(f.FileName);
string typePath = $"wwwroot/{type}";
if (!Directory.Exists(typePath))
{
Directory.CreateDirectory(typePath);
}
var filePath = Path.Combine(typePath, filename);
data.FilePath = filePath;
//生成文件
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
await f.CopyToAsync(stream);
//如果是图片类型,还需要生成缩略图,当然,如果图片很小,直接复制过去即可
if (FileTypeEnum.Image.Equals(type))
{
string thumbnailPath = $"wwwroot/{FileTypeEnum.Thumbnail}";
if (!Directory.Exists(thumbnailPath))
{
Directory.CreateDirectory(thumbnailPath);
}
string thumbnailFilePath = Path.Combine(thumbnailPath, filename);
try
{
// _imageSharpManager.ImageCompress(f.FileName, f.OpenReadStream(), thumbnailFilePath);
}
catch
{
var result = new byte[stream.Length];
await stream.ReadAsync(result, 0, result.Length);
await File.WriteAllBytesAsync(thumbnailFilePath, result);
}
}
};
entities.Add(data);
}
await _repository.InsertManyAsync(entities);
return entities.Adapt<List<FileGetListOutputDto>>();
}
}
}