From fba716ba287bedc88435283ba6beca08978730f5 Mon Sep 17 00:00:00 2001 From: ShiYu Date: Thu, 22 May 2025 21:18:02 +0800 Subject: [PATCH] refactor(services): split services into separate folders and update namespaces --- Controllers/AlbumController.cs | 2 +- Controllers/AuthController.cs | 3 +- Controllers/BackgroundTaskController.cs | 2 +- Controllers/ConfigController.cs | 2 +- Controllers/PictureController.cs | 3 +- Controllers/TagController.cs | 2 +- Extensions/ServiceCollectionExtensions.cs | 10 +++- Program.cs | 2 +- Services/{ => AI}/AiService.cs | 15 +++--- Services/{Interface => AI}/IAiService.cs | 2 +- Services/{ => Auth}/AuthService.cs | 6 +-- Services/{Interface => Auth}/IAuthService.cs | 3 +- .../{ => Background}/BackgroundTaskQueue.cs | 8 +-- .../IBackgroundTaskQueue.cs | 2 +- .../{ => Background}/QueuedHostedService.cs | 4 +- Services/{ => Configuration}/ConfigService.cs | 9 ++-- .../IConfigService.cs | 2 +- .../{ => Initializer}/DatabaseInitializer.cs | 8 +-- .../IDatabaseInitializer.cs | 2 +- Services/{ => Media}/AlbumService.cs | 51 +++++++------------ .../{Interface => Media}/IAlbumService.cs | 3 +- .../{Interface => Media}/IPictureService.cs | 2 +- Services/{Interface => Media}/ITagService.cs | 2 +- Services/{ => Media}/PictureService.cs | 23 +++++---- Services/{ => Media}/TagService.cs | 2 +- .../IStorageProvider.cs | 2 +- .../{Interface => Storage}/IStorageService.cs | 3 +- .../Providers}/CosStorageProvider.cs | 8 +-- .../Providers}/LocalStorageProvider.cs | 4 +- .../Providers}/S3StorageProvider.cs | 6 +-- .../Providers}/TelegramStorageProvider.cs | 6 +-- .../StorageProviderAttribute.cs | 0 Services/{ => Storage}/StorageService.cs | 4 +- 33 files changed, 96 insertions(+), 107 deletions(-) rename Services/{ => AI}/AiService.cs (96%) rename Services/{Interface => AI}/IAiService.cs (97%) rename Services/{ => Auth}/AuthService.cs (99%) rename Services/{Interface => Auth}/IAuthService.cs (92%) rename Services/{ => Background}/BackgroundTaskQueue.cs (99%) rename Services/{Interface => Background}/IBackgroundTaskQueue.cs (97%) rename Services/{ => Background}/QueuedHostedService.cs (94%) rename Services/{ => Configuration}/ConfigService.cs (96%) rename Services/{Interface => Configuration}/IConfigService.cs (92%) rename Services/{ => Initializer}/DatabaseInitializer.cs (98%) rename Services/{Interface => Initializer}/IDatabaseInitializer.cs (65%) rename Services/{ => Media}/AlbumService.cs (83%) rename Services/{Interface => Media}/IAlbumService.cs (91%) rename Services/{Interface => Media}/IPictureService.cs (98%) rename Services/{Interface => Media}/ITagService.cs (94%) rename Services/{ => Media}/PictureService.cs (98%) rename Services/{ => Media}/TagService.cs (99%) rename Services/{Interface => Storage}/IStorageProvider.cs (95%) rename Services/{Interface => Storage}/IStorageService.cs (96%) rename Services/{StorageProvider => Storage/Providers}/CosStorageProvider.cs (98%) rename Services/{StorageProvider => Storage/Providers}/LocalStorageProvider.cs (95%) rename Services/{StorageProvider => Storage/Providers}/S3StorageProvider.cs (98%) rename Services/{StorageProvider => Storage/Providers}/TelegramStorageProvider.cs (99%) rename Services/{Attributes => Storage}/StorageProviderAttribute.cs (100%) rename Services/{ => Storage}/StorageService.cs (97%) diff --git a/Controllers/AlbumController.cs b/Controllers/AlbumController.cs index 18a727a..1f14451 100644 --- a/Controllers/AlbumController.cs +++ b/Controllers/AlbumController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Foxel.Models; using Foxel.Models.Request.Album; using Foxel.Models.Response.Album; -using Foxel.Services.Interface; +using Foxel.Services.Media; namespace Foxel.Controllers; diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs index 28626f5..77723cc 100644 --- a/Controllers/AuthController.cs +++ b/Controllers/AuthController.cs @@ -1,9 +1,10 @@ using Foxel.Models; -using Foxel.Services.Interface; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Foxel.Models.Request.Auth; using Foxel.Models.Response.Auth; +using Foxel.Services.Auth; +using Foxel.Services.Configuration; namespace Foxel.Controllers; diff --git a/Controllers/BackgroundTaskController.cs b/Controllers/BackgroundTaskController.cs index 2c9c228..8f38cfd 100644 --- a/Controllers/BackgroundTaskController.cs +++ b/Controllers/BackgroundTaskController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Foxel.Models; -using Foxel.Services.Interface; +using Foxel.Services.Background; namespace Foxel.Controllers; diff --git a/Controllers/ConfigController.cs b/Controllers/ConfigController.cs index b0cced0..6aee70c 100644 --- a/Controllers/ConfigController.cs +++ b/Controllers/ConfigController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Foxel.Models; using Foxel.Models.DataBase; using Foxel.Models.Request.Config; -using Foxel.Services.Interface; +using Foxel.Services.Configuration; namespace Foxel.Controllers; diff --git a/Controllers/PictureController.cs b/Controllers/PictureController.cs index 613ef4e..cf7c2af 100644 --- a/Controllers/PictureController.cs +++ b/Controllers/PictureController.cs @@ -4,9 +4,10 @@ using Foxel.Models; using Foxel.Models.DataBase; using Foxel.Models.Request.Picture; using Foxel.Models.Response.Picture; -using Foxel.Services.Interface; using System.Text.Json; using System.Text.Json.Serialization; +using Foxel.Services.Configuration; +using Foxel.Services.Media; namespace Foxel.Controllers; diff --git a/Controllers/TagController.cs b/Controllers/TagController.cs index 21004e6..ed2d42a 100644 --- a/Controllers/TagController.cs +++ b/Controllers/TagController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Foxel.Models; using Foxel.Models.Request.Tag; using Foxel.Models.Response.Tag; -using Foxel.Services.Interface; +using Foxel.Services.Media; namespace Foxel.Controllers; diff --git a/Extensions/ServiceCollectionExtensions.cs b/Extensions/ServiceCollectionExtensions.cs index 282c93c..37e2c3d 100644 --- a/Extensions/ServiceCollectionExtensions.cs +++ b/Extensions/ServiceCollectionExtensions.cs @@ -1,13 +1,19 @@ using Foxel.Services; -using Foxel.Services.Interface; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using System.Text; using Foxel.Services.Attributes; -using Foxel.Services.StorageProvider; using System.Reflection; +using Foxel.Services.AI; +using Foxel.Services.Auth; +using Foxel.Services.Background; +using Foxel.Services.Configuration; +using Foxel.Services.Initializer; +using Foxel.Services.Media; +using Foxel.Services.Storage; +using Foxel.Services.Storage.Providers; namespace Foxel.Extensions; diff --git a/Program.cs b/Program.cs index 9e1d099..001f6f7 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,5 @@ using Foxel.Extensions; -using Foxel.Services.Interface; +using Foxel.Services.Initializer; using Microsoft.AspNetCore.HttpOverrides; var builder = WebApplication.CreateBuilder(args); diff --git a/Services/AiService.cs b/Services/AI/AiService.cs similarity index 96% rename from Services/AiService.cs rename to Services/AI/AiService.cs index 521ce2d..fbaa515 100644 --- a/Services/AiService.cs +++ b/Services/AI/AiService.cs @@ -1,9 +1,9 @@ using System.Net.Http.Headers; using System.Text.Json.Serialization; -using Foxel.Services.Interface; +using Foxel.Services.Configuration; using Foxel.Utils; -namespace Foxel.Services; +namespace Foxel.Services.AI; public class AiService : IAiService { @@ -86,7 +86,7 @@ public class AiService : IAiService if (availableTags.Count == 0) return new List(); - string model = _configService["AI:Model"] ?? "deepseek-ai/deepseek-vl2"; // Assuming model can still be dynamic or default + string model = _configService["AI:Model"]; var tagsText = string.Join(", ", availableTags); var textContent = new TextContent { @@ -104,7 +104,7 @@ public class AiService : IAiService var requestContent = new ChatCompletionRequest { Model = model, - Messages = new ChatMessage[] { message }, + Messages = [message], Stream = false, MaxTokens = 200, Temperature = 0.1, // 降低温度使结果更确定性 @@ -146,7 +146,6 @@ public class AiService : IAiService if (result is { Tags.Length: > 0 }) { // 确保返回的标签真的在可用标签列表中 - var availableTagsSet = new HashSet(availableTags, StringComparer.OrdinalIgnoreCase); var matchedTags = new List(); foreach (var tagName in result.Tags) @@ -191,7 +190,7 @@ public class AiService : IAiService try { - string model = _configService["AI:Model"] ?? "deepseek-ai/deepseek-vl2"; // Assuming model can still be dynamic or default + string model = _configService["AI:Model"]; var imageUrl = new ImageUrl { @@ -239,7 +238,7 @@ public class AiService : IAiService var requestContent = new ChatCompletionRequest { Model = model, - Messages = new ChatMessage[] { message }, + Messages = [message], Stream = false, MaxTokens = 200, Temperature = 0.1, // 降低温度使结果更确定性 @@ -335,7 +334,7 @@ public class AiService : IAiService var requestContent = new { - model = model, + model, input = text, encoding_format = "float" }; diff --git a/Services/Interface/IAiService.cs b/Services/AI/IAiService.cs similarity index 97% rename from Services/Interface/IAiService.cs rename to Services/AI/IAiService.cs index 50d715b..2003f52 100644 --- a/Services/Interface/IAiService.cs +++ b/Services/AI/IAiService.cs @@ -1,4 +1,4 @@ -namespace Foxel.Services.Interface; +namespace Foxel.Services.AI; public interface IAiService { diff --git a/Services/AuthService.cs b/Services/Auth/AuthService.cs similarity index 99% rename from Services/AuthService.cs rename to Services/Auth/AuthService.cs index 8803cd3..ce52193 100644 --- a/Services/AuthService.cs +++ b/Services/Auth/AuthService.cs @@ -2,13 +2,13 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using Foxel.Models.DataBase; -using Foxel.Services.Interface; +using Foxel.Models.Request.Auth; +using Foxel.Services.Configuration; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; -using Foxel.Models.Request.Auth; using static Foxel.Utils.AuthHelper; -namespace Foxel.Services; +namespace Foxel.Services.Auth; public class AuthService(IDbContextFactory dbContextFactory, IConfigService configuration) : IAuthService diff --git a/Services/Interface/IAuthService.cs b/Services/Auth/IAuthService.cs similarity index 92% rename from Services/Interface/IAuthService.cs rename to Services/Auth/IAuthService.cs index e30b5e4..15155ee 100644 --- a/Services/Interface/IAuthService.cs +++ b/Services/Auth/IAuthService.cs @@ -1,8 +1,7 @@ using Foxel.Models.DataBase; -using Foxel.Models.Request; using Foxel.Models.Request.Auth; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Auth; public interface IAuthService { diff --git a/Services/BackgroundTaskQueue.cs b/Services/Background/BackgroundTaskQueue.cs similarity index 99% rename from Services/BackgroundTaskQueue.cs rename to Services/Background/BackgroundTaskQueue.cs index da7209e..af06fe7 100644 --- a/Services/BackgroundTaskQueue.cs +++ b/Services/Background/BackgroundTaskQueue.cs @@ -1,12 +1,14 @@ using System.Collections.Concurrent; using System.Threading.Channels; -using Microsoft.EntityFrameworkCore; using Foxel.Models.DataBase; +using Foxel.Services.AI; using Foxel.Services.Attributes; -using Foxel.Services.Interface; +using Foxel.Services.Configuration; +using Foxel.Services.Storage; using Foxel.Utils; +using Microsoft.EntityFrameworkCore; -namespace Foxel.Services; +namespace Foxel.Services.Background; public sealed class BackgroundTaskQueue : IBackgroundTaskQueue, IDisposable { diff --git a/Services/Interface/IBackgroundTaskQueue.cs b/Services/Background/IBackgroundTaskQueue.cs similarity index 97% rename from Services/Interface/IBackgroundTaskQueue.cs rename to Services/Background/IBackgroundTaskQueue.cs index 555a7c8..34ad7b4 100644 --- a/Services/Interface/IBackgroundTaskQueue.cs +++ b/Services/Background/IBackgroundTaskQueue.cs @@ -1,6 +1,6 @@ using Foxel.Models.DataBase; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Background; /// /// 后台任务队列接口 diff --git a/Services/QueuedHostedService.cs b/Services/Background/QueuedHostedService.cs similarity index 94% rename from Services/QueuedHostedService.cs rename to Services/Background/QueuedHostedService.cs index 5c8b3b2..17a4dd9 100644 --- a/Services/QueuedHostedService.cs +++ b/Services/Background/QueuedHostedService.cs @@ -1,6 +1,4 @@ -using Foxel.Services.Interface; - -namespace Foxel.Services; +namespace Foxel.Services.Background; public class QueuedHostedService( ILogger logger, diff --git a/Services/ConfigService.cs b/Services/Configuration/ConfigService.cs similarity index 96% rename from Services/ConfigService.cs rename to Services/Configuration/ConfigService.cs index 78da856..3d54f60 100644 --- a/Services/ConfigService.cs +++ b/Services/Configuration/ConfigService.cs @@ -1,10 +1,9 @@ +using System.Text.Json; +using Foxel.Models.DataBase; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; -using Foxel.Models.DataBase; -using Foxel.Services.Interface; -using System.Text.Json; -namespace Foxel.Services; +namespace Foxel.Services.Configuration; public class ConfigService( IDbContextFactory contextFactory, @@ -32,7 +31,7 @@ public class ConfigService( if (config == null) { // 尝试从环境变量获取 - string? envVarKey = key.ToUpper().Replace(".", "_").Replace("-", "_"); + string envVarKey = key.ToUpper().Replace(".", "_").Replace("-", "_"); string? envVarValue = Environment.GetEnvironmentVariable(envVarKey); if (!string.IsNullOrEmpty(envVarValue)) diff --git a/Services/Interface/IConfigService.cs b/Services/Configuration/IConfigService.cs similarity index 92% rename from Services/Interface/IConfigService.cs rename to Services/Configuration/IConfigService.cs index 9984aee..3b4210d 100644 --- a/Services/Interface/IConfigService.cs +++ b/Services/Configuration/IConfigService.cs @@ -1,6 +1,6 @@ using Foxel.Models.DataBase; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Configuration; public interface IConfigService { diff --git a/Services/DatabaseInitializer.cs b/Services/Initializer/DatabaseInitializer.cs similarity index 98% rename from Services/DatabaseInitializer.cs rename to Services/Initializer/DatabaseInitializer.cs index 736f1f1..322278e 100644 --- a/Services/DatabaseInitializer.cs +++ b/Services/Initializer/DatabaseInitializer.cs @@ -1,10 +1,10 @@ -using Foxel.Models.DataBase; -using Foxel.Services.Interface; -using Microsoft.EntityFrameworkCore; using System.Security.Cryptography; using System.Text; +using Foxel.Models.DataBase; +using Foxel.Services.Configuration; +using Microsoft.EntityFrameworkCore; -namespace Foxel.Services; +namespace Foxel.Services.Initializer; public class DatabaseInitializer( IDbContextFactory contextFactory, diff --git a/Services/Interface/IDatabaseInitializer.cs b/Services/Initializer/IDatabaseInitializer.cs similarity index 65% rename from Services/Interface/IDatabaseInitializer.cs rename to Services/Initializer/IDatabaseInitializer.cs index 4c540e5..4c93eee 100644 --- a/Services/Interface/IDatabaseInitializer.cs +++ b/Services/Initializer/IDatabaseInitializer.cs @@ -1,4 +1,4 @@ -namespace Foxel.Services.Interface; +namespace Foxel.Services.Initializer; public interface IDatabaseInitializer { diff --git a/Services/AlbumService.cs b/Services/Media/AlbumService.cs similarity index 83% rename from Services/AlbumService.cs rename to Services/Media/AlbumService.cs index 5202e22..fc81e6d 100644 --- a/Services/AlbumService.cs +++ b/Services/Media/AlbumService.cs @@ -1,35 +1,22 @@ -using Microsoft.EntityFrameworkCore; using System.Security.Claims; using Foxel.Models; using Foxel.Models.DataBase; using Foxel.Models.Response.Album; -using Foxel.Models.Response.Picture; -using Foxel.Services.Interface; -using Foxel.Utils; +using Microsoft.EntityFrameworkCore; -namespace Foxel.Services; +namespace Foxel.Services.Media; -public class AlbumService : IAlbumService +public class AlbumService( + IDbContextFactory contextFactory, + IHttpContextAccessor httpContextAccessor) + : IAlbumService { - private readonly IDbContextFactory _contextFactory; - private readonly IConfigService _configuration; - private readonly IHttpContextAccessor _httpContextAccessor; - - private string ServerUrl => _configuration["AppSettings:ServerUrl"]; - - public AlbumService(IDbContextFactory contextFactory, IConfigService configuration, IHttpContextAccessor httpContextAccessor) - { - _contextFactory = contextFactory; - _configuration = configuration; - _httpContextAccessor = httpContextAccessor; - } - public async Task> GetAlbumsAsync(int page = 1, int pageSize = 10, int? userId = null) { if (page < 1) page = 1; if (pageSize < 1) pageSize = 10; - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); // 构建查询 IQueryable query = dbContext.Albums @@ -63,9 +50,9 @@ public class AlbumService : IAlbumService Id = a.Id, Name = a.Name, Description = a.Description, - PictureCount = albumPictureCounts.TryGetValue(a.Id, out var count) ? count : 0, + PictureCount = albumPictureCounts.GetValueOrDefault(a.Id, 0), UserId = a.UserId, - Username = a.User?.UserName, + Username = a.User.UserName, CreatedAt = a.CreatedAt, UpdatedAt = a.UpdatedAt }).ToList(); @@ -81,7 +68,7 @@ public class AlbumService : IAlbumService public async Task GetAlbumByIdAsync(int id) { - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); var album = await dbContext.Albums .Include(a => a.User) @@ -103,7 +90,7 @@ public class AlbumService : IAlbumService Description = album.Description, PictureCount = pictureCount, UserId = album.UserId, - Username = album.User?.UserName, + Username = album.User.UserName, CreatedAt = album.CreatedAt, UpdatedAt = album.UpdatedAt }; @@ -116,7 +103,7 @@ public class AlbumService : IAlbumService if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("相册名称不能为空", nameof(name)); - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); // 检查用户是否存在 var user = await dbContext.Users.FindAsync(userId); @@ -156,7 +143,7 @@ public class AlbumService : IAlbumService if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("相册名称不能为空", nameof(name)); - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); // 获取相册 var album = await dbContext.Albums @@ -193,7 +180,7 @@ public class AlbumService : IAlbumService Description = album.Description, PictureCount = album.Pictures?.Count ?? 0, UserId = album.UserId, - Username = album.User?.UserName, + Username = album.User.UserName, CreatedAt = album.CreatedAt, UpdatedAt = album.UpdatedAt }; @@ -201,7 +188,7 @@ public class AlbumService : IAlbumService public async Task DeleteAlbumAsync(int id) { - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); var album = await dbContext.Albums.FindAsync(id); if (album == null) @@ -218,7 +205,7 @@ public class AlbumService : IAlbumService public async Task AddPictureToAlbumAsync(int albumId, int pictureId) { - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); // 获取相册和图片 var album = await dbContext.Albums.FindAsync(albumId); @@ -240,7 +227,7 @@ public class AlbumService : IAlbumService public async Task RemovePictureFromAlbumAsync(int albumId, int pictureId) { - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); // 获取图片 var picture = await dbContext.Pictures @@ -260,13 +247,13 @@ public class AlbumService : IAlbumService public async Task AddPicturesToAlbumAsync(int albumId, List pictureIds) { - await using var dbContext = await _contextFactory.CreateDbContextAsync(); + await using var dbContext = await contextFactory.CreateDbContextAsync(); var album = await dbContext.Albums.FindAsync(albumId) ?? throw new KeyNotFoundException("相册不存在"); // 检查是否有权限修改此相册 - var currentUser = _httpContextAccessor.HttpContext?.User; + var currentUser = httpContextAccessor.HttpContext?.User; if (currentUser != null) { var userId = int.Parse(currentUser.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0"); diff --git a/Services/Interface/IAlbumService.cs b/Services/Media/IAlbumService.cs similarity index 91% rename from Services/Interface/IAlbumService.cs rename to Services/Media/IAlbumService.cs index 4441c15..e732bd1 100644 --- a/Services/Interface/IAlbumService.cs +++ b/Services/Media/IAlbumService.cs @@ -1,8 +1,7 @@ using Foxel.Models; -using Foxel.Models.Response; using Foxel.Models.Response.Album; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Media; public interface IAlbumService { diff --git a/Services/Interface/IPictureService.cs b/Services/Media/IPictureService.cs similarity index 98% rename from Services/Interface/IPictureService.cs rename to Services/Media/IPictureService.cs index e21b4a2..b1c6902 100644 --- a/Services/Interface/IPictureService.cs +++ b/Services/Media/IPictureService.cs @@ -3,7 +3,7 @@ using Foxel.Models.DataBase; using Foxel.Models.Response.Picture; using Foxel.Services.Attributes; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Media; public interface IPictureService { diff --git a/Services/Interface/ITagService.cs b/Services/Media/ITagService.cs similarity index 94% rename from Services/Interface/ITagService.cs rename to Services/Media/ITagService.cs index b8c9cf7..ec1669f 100644 --- a/Services/Interface/ITagService.cs +++ b/Services/Media/ITagService.cs @@ -1,7 +1,7 @@ using Foxel.Models; using Foxel.Models.Response.Tag; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Media; public interface ITagService { diff --git a/Services/PictureService.cs b/Services/Media/PictureService.cs similarity index 98% rename from Services/PictureService.cs rename to Services/Media/PictureService.cs index 68b5b15..1740708 100644 --- a/Services/PictureService.cs +++ b/Services/Media/PictureService.cs @@ -1,15 +1,18 @@ +using System.Text.Json; using Foxel.Models; using Foxel.Models.DataBase; -using Foxel.Services.Interface; +using Foxel.Models.Response.Picture; +using Foxel.Services.AI; +using Foxel.Services.Attributes; +using Foxel.Services.Background; +using Foxel.Services.Configuration; +using Foxel.Services.Storage; using Foxel.Utils; using Microsoft.EntityFrameworkCore; using Pgvector; using Pgvector.EntityFrameworkCore; -using System.Text.Json; -using Foxel.Models.Response.Picture; -using Foxel.Services.Attributes; -namespace Foxel.Services; +namespace Foxel.Services.Media; public class PictureService( IDbContextFactory contextFactory, @@ -19,8 +22,6 @@ public class PictureService( IStorageService storageService) : IPictureService { - private readonly string _serverUrl = configuration["AppSettings:ServerUrl"]; - public async Task> GetPicturesAsync( int page = 1, int pageSize = 8, @@ -110,7 +111,7 @@ public class PictureService( var paginatedResults = allResults .Skip((page - 1) * pageSize) .Take(pageSize) - .Select(r => MapPictureToResponse(r.Picture, _serverUrl)) + .Select(r => MapPictureToResponse(r.Picture)) .ToList(); // 处理收藏信息 @@ -181,7 +182,7 @@ public class PictureService( // 转换为响应格式 var pictures = picturesData - .Select(p => MapPictureToResponse(p, _serverUrl)) + .Select(p => MapPictureToResponse(p)) .ToList(); // 处理收藏信息 @@ -333,7 +334,7 @@ public class PictureService( } // 将数据库实体映射到响应对象 - private PictureResponse MapPictureToResponse(Picture picture, string serverUrl) + private PictureResponse MapPictureToResponse(Picture picture) { return new PictureResponse { @@ -449,7 +450,7 @@ public class PictureService( } string fileExtension = Path.GetExtension(fileName); - string newFileName = $"{Guid.NewGuid()}{fileExtension}"; + _ = $"{Guid.NewGuid()}{fileExtension}"; // 使用存储服务保存文件 string relativePath = await storageService.SaveAsync(storageType.Value, fileStream, fileName, contentType); diff --git a/Services/TagService.cs b/Services/Media/TagService.cs similarity index 99% rename from Services/TagService.cs rename to Services/Media/TagService.cs index ec921fd..2f3c3e9 100644 --- a/Services/TagService.cs +++ b/Services/Media/TagService.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using Foxel.Models; using Foxel.Models.DataBase; using Foxel.Models.Response.Tag; -using Foxel.Services.Interface; +using Foxel.Services.Media; namespace Foxel.Services; diff --git a/Services/Interface/IStorageProvider.cs b/Services/Storage/IStorageProvider.cs similarity index 95% rename from Services/Interface/IStorageProvider.cs rename to Services/Storage/IStorageProvider.cs index d44fa9f..5aace83 100644 --- a/Services/Interface/IStorageProvider.cs +++ b/Services/Storage/IStorageProvider.cs @@ -1,4 +1,4 @@ -namespace Foxel.Services.Interface; +namespace Foxel.Services.Storage; public interface IStorageProvider { diff --git a/Services/Interface/IStorageService.cs b/Services/Storage/IStorageService.cs similarity index 96% rename from Services/Interface/IStorageService.cs rename to Services/Storage/IStorageService.cs index eee5e55..915c1bf 100644 --- a/Services/Interface/IStorageService.cs +++ b/Services/Storage/IStorageService.cs @@ -1,7 +1,6 @@ -using Foxel.Models.DataBase; using Foxel.Services.Attributes; -namespace Foxel.Services.Interface; +namespace Foxel.Services.Storage; /// /// 统一的存储服务接口 diff --git a/Services/StorageProvider/CosStorageProvider.cs b/Services/Storage/Providers/CosStorageProvider.cs similarity index 98% rename from Services/StorageProvider/CosStorageProvider.cs rename to Services/Storage/Providers/CosStorageProvider.cs index fce1891..e2ccebf 100644 --- a/Services/StorageProvider/CosStorageProvider.cs +++ b/Services/Storage/Providers/CosStorageProvider.cs @@ -1,13 +1,13 @@ -using Foxel.Services.Interface; using COSXML; using COSXML.Auth; -using COSXML.Model.Object; -using COSXML.Transfer; using COSXML.CosException; +using COSXML.Model.Object; using COSXML.Model.Tag; +using COSXML.Transfer; using Foxel.Services.Attributes; +using Foxel.Services.Configuration; -namespace Foxel.Services.StorageProvider; +namespace Foxel.Services.Storage.Providers; public class CustomQCloudCredentialProvider : DefaultSessionQCloudCredentialProvider { diff --git a/Services/StorageProvider/LocalStorageProvider.cs b/Services/Storage/Providers/LocalStorageProvider.cs similarity index 95% rename from Services/StorageProvider/LocalStorageProvider.cs rename to Services/Storage/Providers/LocalStorageProvider.cs index 81dfdf6..24fc94a 100644 --- a/Services/StorageProvider/LocalStorageProvider.cs +++ b/Services/Storage/Providers/LocalStorageProvider.cs @@ -1,7 +1,7 @@ using Foxel.Services.Attributes; -using Foxel.Services.Interface; +using Foxel.Services.Configuration; -namespace Foxel.Services.StorageProvider; +namespace Foxel.Services.Storage.Providers; [StorageProvider(StorageType.Local)] public class LocalStorageProvider(IConfigService config) : IStorageProvider diff --git a/Services/StorageProvider/S3StorageProvider.cs b/Services/Storage/Providers/S3StorageProvider.cs similarity index 98% rename from Services/StorageProvider/S3StorageProvider.cs rename to Services/Storage/Providers/S3StorageProvider.cs index dc06f94..a768c05 100644 --- a/Services/StorageProvider/S3StorageProvider.cs +++ b/Services/Storage/Providers/S3StorageProvider.cs @@ -1,10 +1,10 @@ -using Foxel.Services.Attributes; -using Foxel.Services.Interface; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Transfer; +using Foxel.Services.Attributes; +using Foxel.Services.Configuration; -namespace Foxel.Services.StorageProvider; +namespace Foxel.Services.Storage.Providers; [StorageProvider(StorageType.S3)] public class S3StorageProvider : IStorageProvider diff --git a/Services/StorageProvider/TelegramStorageProvider.cs b/Services/Storage/Providers/TelegramStorageProvider.cs similarity index 99% rename from Services/StorageProvider/TelegramStorageProvider.cs rename to Services/Storage/Providers/TelegramStorageProvider.cs index c18c3bf..b1809b1 100644 --- a/Services/StorageProvider/TelegramStorageProvider.cs +++ b/Services/Storage/Providers/TelegramStorageProvider.cs @@ -1,10 +1,10 @@ -using Foxel.Services.Attributes; -using Foxel.Services.Interface; using System.Net.Http.Headers; using System.Text.Json; using System.Text.Json.Serialization; +using Foxel.Services.Attributes; +using Foxel.Services.Configuration; -namespace Foxel.Services.StorageProvider; +namespace Foxel.Services.Storage.Providers; [StorageProvider(StorageType.Telegram)] public class TelegramStorageProvider(IConfigService configService) : IStorageProvider diff --git a/Services/Attributes/StorageProviderAttribute.cs b/Services/Storage/StorageProviderAttribute.cs similarity index 100% rename from Services/Attributes/StorageProviderAttribute.cs rename to Services/Storage/StorageProviderAttribute.cs diff --git a/Services/StorageService.cs b/Services/Storage/StorageService.cs similarity index 97% rename from Services/StorageService.cs rename to Services/Storage/StorageService.cs index 2a7a127..cbf939d 100644 --- a/Services/StorageService.cs +++ b/Services/Storage/StorageService.cs @@ -1,9 +1,7 @@ using System.Reflection; -using Foxel.Models.DataBase; using Foxel.Services.Attributes; -using Foxel.Services.Interface; -namespace Foxel.Services; +namespace Foxel.Services.Storage; /// /// 统一的存储服务实现