From 61f254450c89444cbae71cd6c14d1c46f6734d03 Mon Sep 17 00:00:00 2001 From: ShiYu Date: Mon, 19 May 2025 17:22:39 +0800 Subject: [PATCH] feat: add storage type support and update the configuration interface. --- Controllers/PictureController.cs | 3 +- .../Request/Picture/UploadPictureRequest.cs | 2 + Services/Interface/IPictureService.cs | 2 +- Services/PictureService.cs | 17 +++- View/src/pages/settings/SystemConfig.tsx | 83 ++++++++++++++++--- 5 files changed, 89 insertions(+), 18 deletions(-) diff --git a/Controllers/PictureController.cs b/Controllers/PictureController.cs index 93ba674..613ef4e 100644 --- a/Controllers/PictureController.cs +++ b/Controllers/PictureController.cs @@ -78,7 +78,8 @@ public class PictureController(IPictureService pictureService,IConfigService con request.File.ContentType, userId, (PermissionType)request.Permission!, - request.AlbumId + request.AlbumId, + request.StorageType ); var picture = result.Picture; diff --git a/Models/Request/Picture/UploadPictureRequest.cs b/Models/Request/Picture/UploadPictureRequest.cs index d184207..ce2ce4b 100644 --- a/Models/Request/Picture/UploadPictureRequest.cs +++ b/Models/Request/Picture/UploadPictureRequest.cs @@ -10,4 +10,6 @@ public class UploadPictureRequest public int? Permission { get; set; } = 1; public int? AlbumId { get; set; } = null; + + public StorageType? StorageType { get; set; } = null; } \ No newline at end of file diff --git a/Services/Interface/IPictureService.cs b/Services/Interface/IPictureService.cs index 16d5ed9..8254271 100644 --- a/Services/Interface/IPictureService.cs +++ b/Services/Interface/IPictureService.cs @@ -32,7 +32,7 @@ public interface IPictureService int? userId, PermissionType permission = PermissionType.Public, int? albumId = null, - StorageType storageType = StorageType.Telegram + StorageType? storageType = null ); Task GetPictureExifInfoAsync(int pictureId); diff --git a/Services/PictureService.cs b/Services/PictureService.cs index c9fb5fb..89e8436 100644 --- a/Services/PictureService.cs +++ b/Services/PictureService.cs @@ -436,13 +436,24 @@ public class PictureService( int? userId, PermissionType permission = PermissionType.Public, int? albumId = null, - StorageType storageType = StorageType.Local) + StorageType? storageType = null) { + // 如果未指定存储类型,则从配置中获取默认存储类型 + if (storageType == null) + { + var defaultStorageTypeStr = configuration["Storage:DefaultStorage"]; + if (string.IsNullOrEmpty(defaultStorageTypeStr) || !Enum.TryParse(defaultStorageTypeStr, out var defaultStorageType)) + { + defaultStorageType = StorageType.Local; // 如果配置中没有或解析失败,使用本地存储作为默认 + } + storageType = defaultStorageType; + } + string fileExtension = Path.GetExtension(fileName); string newFileName = $"{Guid.NewGuid()}{fileExtension}"; // 获取对应的存储提供者 - var storageProvider = storageProviderFactory.GetProvider(storageType); + var storageProvider = storageProviderFactory.GetProvider(storageType.Value); // 使用存储提供者保存文件 string relativePath = await storageProvider.SaveAsync(fileStream, fileName, contentType); @@ -493,7 +504,7 @@ public class PictureService( User = user, Permission = permission, AlbumId = albumId, - StorageType = storageType, + StorageType = storageType.Value, ProcessingStatus = isAnonymous ? ProcessingStatus.Completed : ProcessingStatus.Pending, ThumbnailPath = isAnonymous ? relativePath : null }; diff --git a/View/src/pages/settings/SystemConfig.tsx b/View/src/pages/settings/SystemConfig.tsx index 6a94455..c61ff4b 100644 --- a/View/src/pages/settings/SystemConfig.tsx +++ b/View/src/pages/settings/SystemConfig.tsx @@ -1,9 +1,11 @@ import React, { useEffect, useState } from 'react'; -import { Tabs, Card, message, Spin } from 'antd'; +import { Tabs, Card, message, Spin, Select } from 'antd'; +import { CloudOutlined, DatabaseOutlined } from '@ant-design/icons'; import { getAllConfigs, setConfig } from '../../api'; import ConfigGroup from './ConfigGroup.tsx'; const { TabPane } = Tabs; +const { Option } = Select; interface ConfigStructure { [key: string]: { @@ -15,6 +17,7 @@ const SystemConfig: React.FC = () => { const [loading, setLoading] = useState(true); const [configs, setConfigs] = useState({}); const [activeKey, setActiveKey] = useState('AI'); + const [storageType, setStorageType] = useState('Telegram'); // 获取所有配置项 const fetchConfigs = async () => { @@ -32,6 +35,11 @@ const SystemConfig: React.FC = () => { }); setConfigs(configGroups); + + // 设置初始存储类型 + if (configGroups.Storage?.DefaultStorage) { + setStorageType(configGroups.Storage.DefaultStorage); + } } else { message.error('获取配置失败: ' + response.message); } @@ -72,6 +80,13 @@ const SystemConfig: React.FC = () => { } }; + // 存储类型选项 + const storageOptions = [ + { value: 'Telegram', label: 'Telegram存储', icon: }, + { value: 'Local', label: '本地存储', icon: }, + // 未来可以添加更多存储选项 + ]; + useEffect(() => { fetchConfigs(); }, []); @@ -151,18 +166,60 @@ const SystemConfig: React.FC = () => { - +
+ 默认存储: + +
+ +
+ 配置存储: + +
+ + {storageType === 'Telegram' && ( + + )}
)}