From 52635d80f2d4722298c458871c0d58a4d079ea38 Mon Sep 17 00:00:00 2001 From: trueai-org Date: Tue, 20 Aug 2024 17:52:46 +0800 Subject: [PATCH] v5.1.0 --- .../DiscordAccountInitializer.cs | 111 ++++++++++++++++-- .../GlobalConfiguration.cs | 2 +- .../Options/ProxyProperties.cs | 6 + 3 files changed, 108 insertions(+), 11 deletions(-) diff --git a/src/Midjourney.API/DiscordAccountInitializer.cs b/src/Midjourney.API/DiscordAccountInitializer.cs index bc300b19..66e92f88 100644 --- a/src/Midjourney.API/DiscordAccountInitializer.cs +++ b/src/Midjourney.API/DiscordAccountInitializer.cs @@ -15,15 +15,15 @@ // along with this program. If not, see . // Additional Terms: -// This software shall not be used for any illegal activities. +// This software shall not be used for any illegal activities. // Users must comply with all applicable laws and regulations, -// particularly those related to image and video processing. +// particularly those related to image and video processing. // The use of this software for any form of illegal face swapping, -// invasion of privacy, or any other unlawful purposes is strictly prohibited. +// invasion of privacy, or any other unlawful purposes is strictly prohibited. // Violation of these terms may result in termination of the license and may subject the violator to legal action. + using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; -using Midjourney.Infrastructure; using Midjourney.Infrastructure.Data; using Midjourney.Infrastructure.LoadBalancer; using Midjourney.Infrastructure.Services; @@ -52,7 +52,6 @@ public class DiscordAccountInitializer : IHostedService private readonly SemaphoreSlim _semaphoreSlim = new(1, 1); private Timer _timer; - public DiscordAccountInitializer( DiscordLoadBalancer discordLoadBalancer, DiscordAccountHelper discordAccountHelper, @@ -174,9 +173,9 @@ public async Task StartAsync(CancellationToken cancellationToken) DbHelper.BannedWordStore.Add(bannedWord); } - if (GlobalConfiguration.Setting.IsMongo) + _ = Task.Run(() => { - _ = Task.Run(() => + if (GlobalConfiguration.Setting.IsMongo) { // 索引 MongoIndexInit(); @@ -186,8 +185,14 @@ public async Task StartAsync(CancellationToken cancellationToken) { MongoAutoMigrate(); } - }); - } + } + + var oss = GlobalConfiguration.Setting.AliyunOss; + if (oss?.Enable == true && oss?.IsAutoMigrationLocalFile == true) + { + AutoMigrationLocalFileToOss(); + } + }); _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); @@ -302,6 +307,93 @@ public void MongoAutoMigrate() } } + /// + /// 自动迁移本地文件到 oss + /// + public void AutoMigrationLocalFileToOss() + { + try + { + LocalLock.TryLock("AutoMigrationLocalFileToOss", TimeSpan.FromSeconds(10), () => + { + var oss = GlobalConfiguration.Setting.AliyunOss; + var dis = GlobalConfiguration.Setting.NgDiscord; + var db = DbHelper.TaskStore; + var cdn = dis.CustomCdn; + + // 并且开启了本地域名 + if (oss?.Enable == true && oss?.IsAutoMigrationLocalFile == true && !string.IsNullOrWhiteSpace(cdn)) + { + var localPath1 = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "attachments"); + var localPath2 = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "ephemeral-attachments"); + var paths = new List { localPath1, localPath2 }; + var process = 0; + foreach (var dir in paths) + { + if (Directory.Exists(dir)) + { + var files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories); + foreach (var fileFullPath in files) + { + var fileName = Path.GetFileName(fileFullPath); + + var model = db.GetCollection().Query().Where(c => c.ImageUrl.StartsWith(cdn) && c.ImageUrl.Contains(fileName)) + .FirstOrDefault(); + if (model != null) + { + // 创建保存路径 + var uri = new Uri(model.ImageUrl); + var localPath = uri.AbsolutePath.TrimStart('/'); + + var stream = File.OpenRead(fileFullPath); + var ossService = new AliyunOssStorageService(); + + var mm = MimeKit.MimeTypes.GetMimeType(Path.GetFileName(localPath)); + if (string.IsNullOrWhiteSpace(mm)) + { + mm = "image/png"; + } + + var result = ossService.SaveAsync(stream, localPath, mm); + + // 替换 url + var aliCdn = oss.CustomCdn; + var url = $"{aliCdn?.Trim()?.Trim('/')}/{localPath}{uri?.Query}"; + + if (model.Action != TaskAction.SWAP_VIDEO_FACE) + { + model.ImageUrl = url.ToStyle(oss.ImageStyle); + model.ThumbnailUrl = url.ToStyle(oss.ThumbnailImageStyle); + } + else + { + model.ImageUrl = url; + model.ThumbnailUrl = url.ToStyle(oss.VideoSnapshotStyle); + } + db.Update(model); + + stream.Close(); + + // 删除 + File.Delete(fileFullPath); + + process++; + Log.Information("文件已自动迁移到阿里云 {@0}, {@1}", process, fileFullPath); + } + } + + Log.Information("文件已自动迁移到阿里云完成 {@0}", process); + } + } + } + }); + } + catch (Exception ex) + { + _logger.Error(ex, "AutoMigrationLocalFileToOss error"); + } + } + private async void DoWork(object state) { if (_semaphoreSlim.CurrentCount == 0) @@ -780,7 +872,6 @@ public void DeleteAccount(string id) } catch { - } DbHelper.AccountStore.Delete(id); diff --git a/src/Midjourney.Infrastructure/GlobalConfiguration.cs b/src/Midjourney.Infrastructure/GlobalConfiguration.cs index b55de1cc..8723b15c 100644 --- a/src/Midjourney.Infrastructure/GlobalConfiguration.cs +++ b/src/Midjourney.Infrastructure/GlobalConfiguration.cs @@ -39,7 +39,7 @@ public class GlobalConfiguration /// /// 版本号 /// - public static string Version { get; set; } = "v5.0.3-beta.1"; + public static string Version { get; set; } = "v5.1.0"; /// /// 全局配置项 diff --git a/src/Midjourney.Infrastructure/Options/ProxyProperties.cs b/src/Midjourney.Infrastructure/Options/ProxyProperties.cs index 2632f268..2c8649d7 100644 --- a/src/Midjourney.Infrastructure/Options/ProxyProperties.cs +++ b/src/Midjourney.Infrastructure/Options/ProxyProperties.cs @@ -21,6 +21,7 @@ // The use of this software for any form of illegal face swapping, // invasion of privacy, or any other unlawful purposes is strictly prohibited. // Violation of these terms may result in termination of the license and may subject the violator to legal action. + using LiteDB; using Midjourney.Infrastructure.Data; @@ -546,6 +547,11 @@ public class AliyunOssOptions /// x-oss-process=video/snapshot,t_6000,f_jpg,w_400,m_fast /// public string VideoSnapshotStyle { get; set; } + + /// + /// 开启自动迁移本地文件到阿里云支持 + /// + public bool IsAutoMigrationLocalFile { get; set; } } ///