From 39533cced3c01e836ba6c4c3cf341edf3ba195cb Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 8 Jun 2025 20:22:32 +0800 Subject: [PATCH] :bug: Bug fixes on video uploading --- .../Account/MagicSpellService.cs | 5 +++-- DysonNetwork.Sphere/Storage/FileService.cs | 22 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/DysonNetwork.Sphere/Account/MagicSpellService.cs b/DysonNetwork.Sphere/Account/MagicSpellService.cs index ed651c4..4cf2521 100644 --- a/DysonNetwork.Sphere/Account/MagicSpellService.cs +++ b/DysonNetwork.Sphere/Account/MagicSpellService.cs @@ -1,5 +1,6 @@ using System.Globalization; using System.Security.Cryptography; +using System.Text.Json; using DysonNetwork.Sphere.Email; using DysonNetwork.Sphere.Pages.Emails; using DysonNetwork.Sphere.Permission; @@ -159,7 +160,7 @@ public class MagicSpellService( db.Accounts.Remove(account); break; case MagicSpellType.AccountActivation: - var contactMethod = spell.Meta["contact_method"] as string; + var contactMethod = (spell.Meta["contact_method"] as JsonElement? ?? default).ToString(); var contact = await db.AccountContacts.FirstOrDefaultAsync(c => c.Content == contactMethod @@ -189,7 +190,7 @@ public class MagicSpellService( break; case MagicSpellType.ContactVerification: - var verifyContactMethod = spell.Meta["contact_method"] as string; + var verifyContactMethod = (spell.Meta["contact_method"] as JsonElement? ?? default).ToString(); var verifyContact = await db.AccountContacts .FirstOrDefaultAsync(c => c.Content == verifyContactMethod); if (verifyContact is not null) diff --git a/DysonNetwork.Sphere/Storage/FileService.cs b/DysonNetwork.Sphere/Storage/FileService.cs index 1f912d2..896925a 100644 --- a/DysonNetwork.Sphere/Storage/FileService.cs +++ b/DysonNetwork.Sphere/Storage/FileService.cs @@ -52,7 +52,7 @@ public class FileService( } private static readonly string TempFilePrefix = "dyn-cloudfile"; - private static readonly string[] function = new[] { "image/gif", "image/apng", "image/webp", "image/avif" }; + private static readonly string[] AnimatedImageTypes = new[] { "image/gif", "image/apng", "image/webp", "image/avif" }; // The analysis file method no longer will remove the GPS EXIF data // It should be handled on the client side, and for some specific cases it should be keep @@ -66,7 +66,7 @@ public class FileService( { var result = new List<(string filePath, string suffix)>(); - var ogFilePath = Path.Join(configuration.GetValue("Tus:StorePath"), fileId); + var ogFilePath = Path.GetFullPath(Path.Join(configuration.GetValue("Tus:StorePath"), fileId)); var fileSize = stream.Length; var hash = await HashFileAsync(stream, fileSize: fileSize); contentType ??= !fileName.Contains('.') ? "application/octet-stream" : MimeTypes.GetMimeType(fileName); @@ -144,7 +144,7 @@ public class FileService( case "audio": try { - var mediaInfo = await FFProbe.AnalyseAsync(stream); + var mediaInfo = await FFProbe.AnalyseAsync(ogFilePath); file.FileMeta = new Dictionary { ["duration"] = mediaInfo.Duration.TotalSeconds, @@ -156,9 +156,10 @@ public class FileService( ["chapters"] = mediaInfo.Chapters, }; } - catch + catch (Exception ex) { - // ignored + logger.LogError("File analyzed failed, unable collect video / audio information: {Message}", + ex.Message); } break; @@ -179,7 +180,7 @@ public class FileService( if (contentType.Split('/')[0] == "image") { // Skip compression for animated image types - var animatedMimeTypes = function; + var animatedMimeTypes = AnimatedImageTypes; if (animatedMimeTypes.Contains(contentType)) { logger.LogInformation( @@ -192,7 +193,7 @@ public class FileService( file.MimeType = "image/webp"; - using var vipsImage = NetVips.Image.NewFromFile(ogFilePath); + using var vipsImage = Image.NewFromFile(ogFilePath); var imagePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}"); vipsImage.Autorot(); vipsImage.WriteToFile(imagePath + ".webp", @@ -216,11 +217,8 @@ public class FileService( } else { - var tempFilePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}"); - await using var fileStream = File.Create(tempFilePath); - stream.Position = 0; - await stream.CopyToAsync(fileStream); - result.Add((tempFilePath, string.Empty)); + // No extra process for video, add it to the upload queue. + result.Add((ogFilePath, string.Empty)); } logger.LogInformation("Optimized file {fileId}, now uploading...", fileId);