mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-04 23:29:00 +08:00
refactor(BackgroundTaskQueue): streamline file handling and storage logic
This commit is contained in:
@@ -217,22 +217,18 @@ public sealed class BackgroundTaskQueue : IBackgroundTaskQueue, IDisposable
|
|||||||
// 根据存储类型获取文件处理路径
|
// 根据存储类型获取文件处理路径
|
||||||
var storageProvider = storageProviderFactory.GetProvider(picture.StorageType);
|
var storageProvider = storageProviderFactory.GetProvider(picture.StorageType);
|
||||||
|
|
||||||
|
// 处理文件获取逻辑
|
||||||
if (picture.StorageType == StorageType.Local)
|
if (picture.StorageType == StorageType.Local)
|
||||||
{
|
{
|
||||||
// 本地存储,直接使用文件路径
|
// 本地存储,直接使用文件路径
|
||||||
localFilePath = Path.Combine(Directory.GetCurrentDirectory(), picture.Path.TrimStart('/'));
|
localFilePath = Path.Combine(Directory.GetCurrentDirectory(), picture.Path.TrimStart('/'));
|
||||||
}
|
}
|
||||||
else if (picture.StorageType == StorageType.Telegram)
|
|
||||||
{
|
|
||||||
// Telegram存储,需要先下载文件
|
|
||||||
await UpdatePictureStatus(task.PictureId, ProcessingStatus.Processing, 15);
|
|
||||||
var telegramProvider = (TelegramStorageProvider)storageProvider;
|
|
||||||
localFilePath = await telegramProvider.DownloadFileAsync(picture.Path);
|
|
||||||
isTempFile = true;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new Exception($"不支持的存储类型: {picture.StorageType}");
|
// 非本地存储需要先下载文件
|
||||||
|
await UpdatePictureStatus(task.PictureId, ProcessingStatus.Processing, 15);
|
||||||
|
localFilePath = await storageProvider.DownloadFileAsync(picture.Path);
|
||||||
|
isTempFile = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(localFilePath) || !File.Exists(localFilePath))
|
if (string.IsNullOrEmpty(localFilePath) || !File.Exists(localFilePath))
|
||||||
@@ -250,37 +246,29 @@ public sealed class BackgroundTaskQueue : IBackgroundTaskQueue, IDisposable
|
|||||||
await ImageHelper.CreateThumbnailAsync(localFilePath, thumbnailPath, 500);
|
await ImageHelper.CreateThumbnailAsync(localFilePath, thumbnailPath, 500);
|
||||||
|
|
||||||
// 更新缩略图路径到数据库
|
// 更新缩略图路径到数据库
|
||||||
string relativeThumbnailPath = "";
|
await UpdatePictureStatus(task.PictureId, ProcessingStatus.Processing, 25);
|
||||||
|
|
||||||
if (picture.StorageType == StorageType.Local)
|
if (picture.StorageType == StorageType.Local)
|
||||||
{
|
{
|
||||||
// 本地存储缩略图
|
// 本地存储缩略图
|
||||||
relativeThumbnailPath =
|
var relativeThumbnailPath = $"/Uploads/{Path.GetRelativePath("Uploads", Path.GetDirectoryName(thumbnailPath)!)}/{Path.GetFileName(thumbnailPath)}";
|
||||||
$"/Uploads/{Path.GetRelativePath("Uploads", Path.GetDirectoryName(thumbnailPath)!)}/{Path.GetFileName(thumbnailPath)}";
|
|
||||||
picture.ThumbnailPath = relativeThumbnailPath.Replace('\\', '/');
|
picture.ThumbnailPath = relativeThumbnailPath.Replace('\\', '/');
|
||||||
}
|
}
|
||||||
else if (picture.StorageType == StorageType.Telegram)
|
else
|
||||||
{
|
{
|
||||||
// 对于Telegram存储,缩略图也上传到Telegram
|
// 非本地存储,上传缩略图到对应的存储服务
|
||||||
await UpdatePictureStatus(task.PictureId, ProcessingStatus.Processing, 25);
|
|
||||||
var telegramProvider = (TelegramStorageProvider)storageProvider;
|
|
||||||
|
|
||||||
// 上传缩略图到Telegram
|
|
||||||
using var thumbnailFileStream = new FileStream(thumbnailPath, FileMode.Open, FileAccess.Read);
|
using var thumbnailFileStream = new FileStream(thumbnailPath, FileMode.Open, FileAccess.Read);
|
||||||
var thumbnailFileName = Path.GetFileName(thumbnailPath);
|
var thumbnailFileName = Path.GetFileName(thumbnailPath);
|
||||||
var thumbnailContentType = "image/jpeg";
|
var thumbnailContentType = Path.GetExtension(thumbnailPath).ToLower() == ".png" ? "image/png" : "image/jpeg";
|
||||||
if (Path.GetExtension(thumbnailPath).ToLower() == ".png")
|
|
||||||
{
|
|
||||||
thumbnailContentType = "image/png";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上传缩略图到Telegram并获取JSON元数据
|
// 上传缩略图并获取存储路径或元数据
|
||||||
string thumbnailMetadata = await telegramProvider.SaveAsync(
|
string thumbnailStoragePath = await storageProvider.SaveAsync(
|
||||||
thumbnailFileStream,
|
thumbnailFileStream,
|
||||||
thumbnailFileName,
|
thumbnailFileName,
|
||||||
thumbnailContentType);
|
thumbnailContentType);
|
||||||
|
|
||||||
// 将元数据存储到ThumbnailPath
|
// 将路径或元数据存储到ThumbnailPath
|
||||||
picture.ThumbnailPath = thumbnailMetadata;
|
picture.ThumbnailPath = thumbnailStoragePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 提取EXIF信息
|
// 3. 提取EXIF信息
|
||||||
|
|||||||
Reference in New Issue
Block a user