Files
Yi.Admin/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/FileService.cs

83 lines
2.9 KiB
C#
Raw Normal View History

2023-12-15 00:11:08 +08:00
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;
2025-07-04 19:13:21 +08:00
using Microsoft.Extensions.Caching.Memory;
2023-12-15 00:11:08 +08:00
using Volo.Abp;
using Volo.Abp.Application.Services;
2025-07-04 19:13:21 +08:00
using Volo.Abp.Caching;
2023-12-15 00:11:08 +08:00
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Helper;
2024-01-11 18:51:53 +08:00
using Yi.Framework.Rbac.Application.Contracts.Dtos.FileManager;
2023-12-15 00:11:08 +08:00
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities;
2024-12-21 18:00:43 +08:00
using Yi.Framework.Rbac.Domain.Managers;
2025-07-04 19:13:21 +08:00
using Yi.Framework.Rbac.Domain.Shared.Caches;
2023-12-15 00:11:08 +08:00
namespace Yi.Framework.Rbac.Application.Services
{
public class FileService : ApplicationService, IFileService
{
private readonly IRepository<FileAggregateRoot> _repository;
2024-12-21 18:00:43 +08:00
private readonly FileManager _fileManager;
2025-07-04 19:13:21 +08:00
private readonly IMemoryCache _memoryCache;
2024-12-21 18:00:43 +08:00
2025-07-04 19:13:21 +08:00
public FileService(IRepository<FileAggregateRoot> repository, FileManager fileManager, IMemoryCache memoryCache)
2023-12-15 00:11:08 +08:00
{
_repository = repository;
2024-12-21 18:00:43 +08:00
_fileManager = fileManager;
2025-07-04 19:13:21 +08:00
_memoryCache = memoryCache;
2023-12-15 00:11:08 +08:00
}
/// <summary>
2024-12-21 18:00:43 +08:00
/// 下载文件,支持缩略图
2023-12-15 00:11:08 +08:00
/// </summary>
/// <returns></returns>
[Route("file/{code}/{isThumbnail?}")]
public async Task<IActionResult> Get([FromRoute] Guid code, [FromRoute] bool? isThumbnail)
{
2025-07-04 19:13:21 +08:00
var fileCache = await _memoryCache.GetOrCreateAsync($"File:{code}", async (options) =>
{
options.AbsoluteExpiration = DateTime.Now.AddDays(1);
var file = await _repository.GetAsync(x => x.Id == code);
if (file == null!) return null;
return file.Adapt<FileCacheItem>();
});
var file = fileCache?.Adapt<FileAggregateRoot>();
2024-12-26 23:02:10 +08:00
var path = file?.GetQueryFileSavePath(isThumbnail);
2024-12-21 18:00:43 +08:00
if (path is null || !File.Exists(path))
2024-08-07 11:12:01 +08:00
{
2024-10-04 00:00:44 +08:00
return new NotFoundResult();
2024-08-07 11:12:01 +08:00
}
2025-07-04 19:13:21 +08:00
2023-12-15 00:11:08 +08:00
var steam = await File.ReadAllBytesAsync(path);
2024-12-21 18:00:43 +08:00
return new FileContentResult(steam, file.GetMimeMapping());
2023-12-15 00:11:08 +08:00
}
2025-07-04 19:13:21 +08:00
2023-12-15 00:11:08 +08:00
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public async Task<List<FileGetListOutputDto>> Post([FromForm] IFormFileCollection file)
{
2024-12-21 18:00:43 +08:00
var entities = await _fileManager.CreateAsync(file);
2023-12-15 00:11:08 +08:00
2024-12-21 18:00:43 +08:00
for (int i = 0; i < file.Count; i++)
2023-12-15 00:11:08 +08:00
{
2025-07-04 19:13:21 +08:00
var entity = entities[i];
using (var steam = file[i].OpenReadStream())
{
await _fileManager.SaveFileAsync(entity, steam);
}
2023-12-15 00:11:08 +08:00
}
2025-07-04 19:13:21 +08:00
2023-12-15 00:11:08 +08:00
return entities.Adapt<List<FileGetListOutputDto>>();
}
}
2024-12-21 18:00:43 +08:00
}