From 6d8a859b2080b3ede79f434d6e0b667ddc65ca22 Mon Sep 17 00:00:00 2001 From: chenchun Date: Fri, 4 Jul 2025 19:13:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=B3=E9=97=AD=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FileService.cs | 30 ++++++++++++++----- .../Caches/FileCacheItem.cs | 29 ++++++++++++++++++ Yi.Bbs.Vue3/.env.production | 4 +-- Yi.Bbs.Vue3/src/components/StarBackground.vue | 2 +- 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/FileCacheItem.cs diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/FileService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/FileService.cs index 86c0891b..d7b22f37 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/FileService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/FileService.cs @@ -6,8 +6,10 @@ using System.Threading.Tasks; using Mapster; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; using Volo.Abp; using Volo.Abp.Application.Services; +using Volo.Abp.Caching; using Volo.Abp.Domain.Repositories; using Volo.Abp.Guids; using Yi.Framework.Core.Enums; @@ -16,6 +18,7 @@ using Yi.Framework.Rbac.Application.Contracts.Dtos.FileManager; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Managers; +using Yi.Framework.Rbac.Domain.Shared.Caches; namespace Yi.Framework.Rbac.Application.Services { @@ -23,11 +26,13 @@ namespace Yi.Framework.Rbac.Application.Services { private readonly IRepository _repository; private readonly FileManager _fileManager; + private readonly IMemoryCache _memoryCache; - public FileService(IRepository repository, FileManager fileManager) + public FileService(IRepository repository, FileManager fileManager, IMemoryCache memoryCache) { _repository = repository; _fileManager = fileManager; + _memoryCache = memoryCache; } /// @@ -37,16 +42,24 @@ namespace Yi.Framework.Rbac.Application.Services [Route("file/{code}/{isThumbnail?}")] public async Task Get([FromRoute] Guid code, [FromRoute] bool? isThumbnail) { - var file = await _repository.GetAsync(x => x.Id == code); + 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(); + }); + var file = fileCache?.Adapt(); var path = file?.GetQueryFileSavePath(isThumbnail); if (path is null || !File.Exists(path)) { return new NotFoundResult(); } + var steam = await File.ReadAllBytesAsync(path); return new FileContentResult(steam, file.GetMimeMapping()); } - + /// /// 上传文件 /// @@ -57,12 +70,13 @@ namespace Yi.Framework.Rbac.Application.Services for (int i = 0; i < file.Count; i++) { - var entity= entities[i]; - using (var steam = file[i].OpenReadStream()) - { - await _fileManager.SaveFileAsync(entity,steam); - } + var entity = entities[i]; + using (var steam = file[i].OpenReadStream()) + { + await _fileManager.SaveFileAsync(entity, steam); + } } + return entities.Adapt>(); } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/FileCacheItem.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/FileCacheItem.cs new file mode 100644 index 00000000..47a91d4f --- /dev/null +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/FileCacheItem.cs @@ -0,0 +1,29 @@ +namespace Yi.Framework.Rbac.Domain.Shared.Caches; + +public class FileCacheItem +{ + public Guid Id { get; set; } + + /// + /// 文件大小 + /// + public decimal FileSize { get; set; } + + /// + /// 文件名 + /// + public string FileName { get; set; } + + /// + /// 文件路径 + /// + public string FilePath { get; set; } + + public DateTime CreationTime { get; set; } + + public Guid? CreatorId { get; set; } + + public Guid? LastModifierId { get; set; } + + public DateTime? LastModificationTime { get; set; } +} \ No newline at end of file diff --git a/Yi.Bbs.Vue3/.env.production b/Yi.Bbs.Vue3/.env.production index a5f4a621..17f08c37 100644 --- a/Yi.Bbs.Vue3/.env.production +++ b/Yi.Bbs.Vue3/.env.production @@ -2,11 +2,11 @@ VITE_APP_ENV_NAME = "pro" # 接口前缀 VITE_APP_BASEAPI="/prod-api" -VITE_APP_URL="http://ccnetcore.com:19001/api/app" +VITE_APP_URL="http://data.ccnetcore.com:19001/api/app" # ws VITE_APP_BASE_WS = '/prod-ws' -VITE_APP_BASE_URL_WS="http://ccnetcore.com:19001/hub" +VITE_APP_BASE_URL_WS="http://data.ccnetcore.com:19001/hub" # 是否开启ICP备案模式 VITE_APP_ICP = false \ No newline at end of file diff --git a/Yi.Bbs.Vue3/src/components/StarBackground.vue b/Yi.Bbs.Vue3/src/components/StarBackground.vue index 76428a0f..ad44f744 100644 --- a/Yi.Bbs.Vue3/src/components/StarBackground.vue +++ b/Yi.Bbs.Vue3/src/components/StarBackground.vue @@ -69,7 +69,7 @@ function drawStars() { function animate() { drawStars(); - requestAnimationFrame(animate); + // requestAnimationFrame(animate); }