refactor: 统一命名空间为Foxel.Api,移除不必要的using语句,优化代码结构

This commit is contained in:
shiyu
2025-06-22 17:07:43 +08:00
parent 9612a69f9d
commit 49c84658b5
50 changed files with 112 additions and 210 deletions

View File

@@ -1,5 +1,7 @@
using Foxel.Models.DataBase;
namespace Foxel.Services.AI;
public interface IFaceClusteringService
{
/// <summary>

View File

@@ -10,7 +10,10 @@ using static Foxel.Utils.AuthHelper;
namespace Foxel.Services.Auth;
public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfigService configuration, ILogger<AuthService> logger)
public class AuthService(
IDbContextFactory<MyDbContext> dbContextFactory,
IConfigService configuration,
ILogger<AuthService> logger)
: IAuthService
{
public async Task<(bool success, string message, User? user)> RegisterUserAsync(RegisterRequest request)
@@ -196,11 +199,11 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
$"https://github.com/login/oauth/authorize?client_id={Uri.EscapeDataString(githubClientId)}&redirect_uri={Uri.EscapeDataString(githubCallback)}";
}
public async Task<(GitHubAuthResult result, string message, string? data)> ProcessGitHubCallbackAsync(string code)
public async Task<(OAuthResult result, string message, string? data)> ProcessGitHubCallbackAsync(string code)
{
if (string.IsNullOrEmpty(code))
{
return (GitHubAuthResult.InvalidCode, "GitHub授权码无效", null);
return (OAuthResult.InvalidCode, "GitHub授权码无效", null);
}
string githubClientId = configuration["Authentication:GitHubClientId"];
@@ -219,7 +222,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
{
var errorContent = await tokenResponse.Content.ReadAsStringAsync();
logger.LogError("获取GitHub访问令牌失败: {StatusCode}, {ErrorContent}", tokenResponse.StatusCode, errorContent);
return (GitHubAuthResult.TokenRequestFailed, $"获取GitHub访问令牌失败: {errorContent}", null);
return (OAuthResult.TokenRequestFailed, $"获取GitHub访问令牌失败: {errorContent}", null);
}
var tokenResponseContent = await tokenResponse.Content.ReadAsStringAsync();
@@ -229,7 +232,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
accessTokenElement.GetString() == null)
{
logger.LogError("GitHub响应中未找到access_token: {TokenResponseContent}", tokenResponseContent);
return (GitHubAuthResult.TokenRequestFailed, "获取GitHub访问令牌失败响应中未包含令牌。", null);
return (OAuthResult.TokenRequestFailed, "获取GitHub访问令牌失败响应中未包含令牌。", null);
}
var accessToken = accessTokenElement.GetString();
@@ -242,7 +245,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
{
var errorContent = await userResponse.Content.ReadAsStringAsync();
logger.LogError("获取GitHub用户信息失败: {StatusCode}, {ErrorContent}", userResponse.StatusCode, errorContent);
return (GitHubAuthResult.UserInfoFailed, $"获取GitHub用户信息失败: {errorContent}", null);
return (OAuthResult.UserInfoFailed, $"获取GitHub用户信息失败: {errorContent}", null);
}
var userContent = await userResponse.Content.ReadAsStringAsync();
@@ -275,34 +278,34 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
if (string.IsNullOrEmpty(githubUserId))
{
return (GitHubAuthResult.InvalidUserId, "无法从GitHub获取用户ID", null);
return (OAuthResult.InvalidUserId, "无法从GitHub获取用户ID", null);
}
var (isSuccess, message, user) = await FindGitHubUserAsync(githubUserId);
if (!isSuccess || user == null)
{
return (GitHubAuthResult.UserNotBound, "GitHub用户未绑定到系统账户", githubUserId);
return (OAuthResult.UserNotBound, "GitHub用户未绑定到系统账户", githubUserId);
}
var jwtToken = await GenerateJwtTokenAsync(user);
return (GitHubAuthResult.Success, "GitHub授权成功", jwtToken);
return (OAuthResult.Success, "GitHub授权成功", jwtToken);
}
public string GetLinuxDoLoginUrl()
{
string linuxdoClientId = configuration["Authentication:LinuxDoClientId"];
string linuxdoCallback = configuration["Authentication:LinuxDoCallbackUrl"];
string state = Guid.NewGuid().ToString();
string state = Guid.NewGuid().ToString();
return
$"https://connect.linux.do/oauth2/authorize?response_type=code&client_id={Uri.EscapeDataString(linuxdoClientId)}&redirect_uri={Uri.EscapeDataString(linuxdoCallback)}&state={Uri.EscapeDataString(state)}";
}
public async Task<(LinuxDoAuthResult result, string message, string? data)> ProcessLinuxDoCallbackAsync(string code)
public async Task<(OAuthResult result, string message, string? data)> ProcessLinuxDoCallbackAsync(string code)
{
if (string.IsNullOrEmpty(code))
{
return (LinuxDoAuthResult.InvalidCode, "LinuxDo授权码无效", null);
return (OAuthResult.InvalidCode, "LinuxDo授权码无效", null);
}
string linuxdoClientId = configuration["Authentication:LinuxDoClientId"];
@@ -331,7 +334,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
{
var errorContent = await tokenResponse.Content.ReadAsStringAsync();
logger.LogError("获取LinuxDo访问令牌失败: {StatusCode}, {ErrorContent}", tokenResponse.StatusCode, errorContent);
return (LinuxDoAuthResult.TokenRequestFailed, $"获取LinuxDo访问令牌失败: {errorContent}", null);
return (OAuthResult.TokenRequestFailed, $"获取LinuxDo访问令牌失败: {errorContent}", null);
}
var tokenResponseContent = await tokenResponse.Content.ReadAsStringAsync();
@@ -341,7 +344,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
accessTokenElement.GetString() == null)
{
logger.LogError("LinuxDo响应中未找到access_token: {TokenResponseContent}", tokenResponseContent);
return (LinuxDoAuthResult.TokenRequestFailed, "获取LinuxDo访问令牌失败响应中未包含令牌。", null);
return (OAuthResult.TokenRequestFailed, "获取LinuxDo访问令牌失败响应中未包含令牌。", null);
}
var accessToken = accessTokenElement.GetString();
@@ -355,7 +358,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
{
var errorContent = await userResponse.Content.ReadAsStringAsync();
logger.LogError("获取LinuxDo用户信息失败: {StatusCode}, {ErrorContent}", userResponse.StatusCode, errorContent);
return (LinuxDoAuthResult.UserInfoFailed, $"获取LinuxDo用户信息失败: {errorContent}", null);
return (OAuthResult.UserInfoFailed, $"获取LinuxDo用户信息失败: {errorContent}", null);
}
var userContent = await userResponse.Content.ReadAsStringAsync();
@@ -382,18 +385,18 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
if (string.IsNullOrEmpty(linuxdoUserId))
{
return (LinuxDoAuthResult.InvalidUserId, "无法从LinuxDo获取用户ID", null);
return (OAuthResult.InvalidUserId, "无法从LinuxDo获取用户ID", null);
}
var (isSuccess, message, user) = await FindLinuxDoUserAsync(linuxdoUserId);
if (!isSuccess || user == null)
{
return (LinuxDoAuthResult.UserNotBound, "LinuxDo用户未绑定到系统账户", linuxdoUserId);
return (OAuthResult.UserNotBound, "LinuxDo用户未绑定到系统账户", linuxdoUserId);
}
var jwtToken = await GenerateJwtTokenAsync(user);
return (LinuxDoAuthResult.Success, "LinuxDo授权成功", jwtToken);
return (OAuthResult.Success, "LinuxDo授权成功", jwtToken);
}
public async Task<(bool success, string message, User? user)> BindAccountAsync(BindAccountRequest request)

View File

@@ -1,11 +0,0 @@
namespace Foxel.Services.Auth;
public enum GitHubAuthResult
{
Success, // 授权成功并找到绑定用户
UserNotBound, // 授权成功但用户未绑定
InvalidCode, // 授权码无效
TokenRequestFailed, // 获取访问令牌失败
UserInfoFailed, // 获取用户信息失败
InvalidUserId // 无法获取GitHub用户ID
}

View File

@@ -3,6 +3,16 @@ using Foxel.Models.Request.Auth;
namespace Foxel.Services.Auth;
public enum OAuthResult
{
Success,
UserNotBound,
InvalidCode,
TokenRequestFailed,
UserInfoFailed,
InvalidUserId
}
public interface IAuthService
{
Task<(bool success, string message, User? user)> RegisterUserAsync(RegisterRequest request);
@@ -14,7 +24,7 @@ public interface IAuthService
Task<(bool success, string message, User? user)> UpdateUserInfoAsync(int userId, UpdateUserRequest request);
string GetGitHubLoginUrl();
string GetLinuxDoLoginUrl();
Task<(GitHubAuthResult result, string message, string? data)> ProcessGitHubCallbackAsync(string code);
Task<(LinuxDoAuthResult result, string message, string? data)> ProcessLinuxDoCallbackAsync(string code);
Task<(OAuthResult result, string message, string? data)> ProcessGitHubCallbackAsync(string code);
Task<(OAuthResult result, string message, string? data)> ProcessLinuxDoCallbackAsync(string code);
Task<(bool success, string message, User? user)> BindAccountAsync(BindAccountRequest request);
}
}

View File

@@ -1,11 +0,0 @@
namespace Foxel.Services.Auth;
public enum LinuxDoAuthResult
{
Success, // 授权成功并找到绑定用户
UserNotBound, // 授权成功但用户未绑定
InvalidCode, // 授权码无效
TokenRequestFailed, // 获取访问令牌失败
UserInfoFailed, // 获取用户信息失败
InvalidUserId // 无法获取LinuxDo用户ID
}

View File

@@ -85,7 +85,7 @@ namespace Foxel.Services.Background.Processors
using var scope = serviceProvider.CreateScope();
var storageService = scope.ServiceProvider.GetRequiredService<IStorageService>();
if (picture.StorageMode.StorageType == Attributes.StorageType.Local)
if (picture.StorageMode.StorageType == StorageType.Local)
{
logger.LogInformation(
"Picture {PictureId} is Local. Attempting to download via StorageService for consistency.",

View File

@@ -1,10 +1,10 @@
using Foxel.Models.DataBase;
using Foxel.Services.AI;
using Foxel.Services.Storage;
using Foxel.Services.VectorDB;
using Foxel.Utils;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
using Foxel.Services.VectorDb;
// using Foxel.Services.Attributes; // StorageType enum might not be directly needed here anymore
using Microsoft.Extensions.DependencyInjection; // For CreateScope
using Microsoft.AspNetCore.Hosting; // For IWebHostEnvironment
@@ -117,7 +117,7 @@ namespace Foxel.Services.Background.Processors
string actualThumbnailPathForAI;
// Check the StorageType of the associated StorageMode
if (picture.StorageMode.StorageType == Attributes.StorageType.Local)
if (picture.StorageMode.StorageType == StorageType.Local)
{
// As with PictureTaskProcessor, safer to use DownloadFileAsync for consistency
_logger.LogInformation("Picture {PictureId} thumbnail is Local. Attempting to download via StorageService for AI.", pictureId);

View File

@@ -1,7 +1,7 @@
using Foxel.Models.DataBase;
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
using Foxel.Services.Logging;
using Foxel.Services.Storage;
using Microsoft.EntityFrameworkCore;
namespace Foxel.Services.Initializer;

View File

@@ -1,7 +1,9 @@
using Foxel.Models; // For PaginatedResult
using Foxel.Models.Request.Storage;
using Foxel.Models.Response.Storage;
using Foxel.Services.Attributes; // For StorageType
using Foxel.Services.Storage;
// For StorageType
namespace Foxel.Services.Management;

View File

@@ -2,12 +2,12 @@ using Foxel.Models;
using Foxel.Models.DataBase;
using Foxel.Models.Request.Storage;
using Foxel.Models.Response.Storage;
using Foxel.Services.Attributes;
using Foxel.Services.Storage.Providers; // Required for config types like LocalStorageConfig, etc.
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using Foxel.Services.Configuration; // Added for IConfigService
using Foxel.Services.Configuration;
using Foxel.Services.Storage; // Added for IConfigService
namespace Foxel.Services.Management;

View File

@@ -7,8 +7,8 @@ using Foxel.Services.AI;
using Foxel.Services.Background;
using Foxel.Services.Configuration;
using Foxel.Services.Storage;
using Foxel.Services.VectorDB;
using Foxel.Services.Mapping;
using Foxel.Services.VectorDb;
using Foxel.Utils;
using Microsoft.EntityFrameworkCore;

View File

@@ -1,11 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Foxel.Models;
using Foxel.Models.DataBase;
using Foxel.Models.Response.Tag;
using Foxel.Services.Media;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
namespace Foxel.Services;
namespace Foxel.Services.Media;
public class TagService(IDbContextFactory<MyDbContext> contextFactory, ILogger<TagService> logger) : ITagService
{

View File

@@ -1,5 +1,3 @@
using Foxel.Services.Attributes;
namespace Foxel.Services.Storage;
/// <summary>

View File

@@ -4,7 +4,6 @@ using COSXML.CosException;
using COSXML.Model.Object;
using COSXML.Model.Tag;
using COSXML.Transfer;
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
using Microsoft.Extensions.Logging;

View File

@@ -1,4 +1,3 @@
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
using Microsoft.Extensions.Logging;

View File

@@ -1,7 +1,6 @@
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
using Microsoft.Extensions.Logging;

View File

@@ -1,7 +1,6 @@
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
using System.Net;

View File

@@ -1,6 +1,5 @@
using System.Net.Http.Headers;
using System.Text;
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
namespace Foxel.Services.Storage.Providers;

View File

@@ -1,6 +1,4 @@
using Foxel.Models.DataBase;
namespace Foxel.Services.Attributes;
namespace Foxel.Services.Storage;
public enum StorageType
{

View File

@@ -1,5 +1,4 @@
using System.Reflection;
using Foxel.Services.Attributes;
using Microsoft.EntityFrameworkCore; // For IDbContextFactory
using System.Text.Json; // For JsonSerializer
using Foxel.Services.Storage.Providers; // For specific config classes

View File

@@ -1,6 +1,6 @@
using Foxel.Models.Vector;
namespace Foxel.Services.VectorDB;
namespace Foxel.Services.VectorDb;
public interface IVectorDbService
{

View File

@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;
namespace Foxel.Services.VectorDB;
namespace Foxel.Services.VectorDb;
public class InMemoryVectorDbService : IVectorDbService
{

View File

@@ -5,7 +5,7 @@ using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.Qdrant;
using Qdrant.Client;
namespace Foxel.Services.VectorDB;
namespace Foxel.Services.VectorDb;
public class QdrantVectorDbService(IDbContextFactory<MyDbContext> contextFactory, IConfigService configService)
: IVectorDbService

View File

@@ -1,6 +1,6 @@
using Foxel.Services.Configuration;
namespace Foxel.Services.VectorDB;
namespace Foxel.Services.VectorDb;
public class VectorDbInitializer : IHostedService
{

View File

@@ -2,7 +2,7 @@ using Foxel.Models.Vector;
using Foxel.Services.Configuration;
using Microsoft.EntityFrameworkCore;
namespace Foxel.Services.VectorDB;
namespace Foxel.Services.VectorDb;
public class VectorDbManager(IServiceProvider serviceProvider, IConfigService configService)
: IVectorDbService