🐛 Bug fixes on video uploading

This commit is contained in:
LittleSheep 2025-06-08 20:22:32 +08:00
parent 4bae2ea427
commit 39533cced3
2 changed files with 13 additions and 14 deletions

View File

@ -1,5 +1,6 @@
using System.Globalization; using System.Globalization;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.Json;
using DysonNetwork.Sphere.Email; using DysonNetwork.Sphere.Email;
using DysonNetwork.Sphere.Pages.Emails; using DysonNetwork.Sphere.Pages.Emails;
using DysonNetwork.Sphere.Permission; using DysonNetwork.Sphere.Permission;
@ -159,7 +160,7 @@ public class MagicSpellService(
db.Accounts.Remove(account); db.Accounts.Remove(account);
break; break;
case MagicSpellType.AccountActivation: case MagicSpellType.AccountActivation:
var contactMethod = spell.Meta["contact_method"] as string; var contactMethod = (spell.Meta["contact_method"] as JsonElement? ?? default).ToString();
var contact = await var contact = await
db.AccountContacts.FirstOrDefaultAsync(c => db.AccountContacts.FirstOrDefaultAsync(c =>
c.Content == contactMethod c.Content == contactMethod
@ -189,7 +190,7 @@ public class MagicSpellService(
break; break;
case MagicSpellType.ContactVerification: 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 var verifyContact = await db.AccountContacts
.FirstOrDefaultAsync(c => c.Content == verifyContactMethod); .FirstOrDefaultAsync(c => c.Content == verifyContactMethod);
if (verifyContact is not null) if (verifyContact is not null)

View File

@ -52,7 +52,7 @@ public class FileService(
} }
private static readonly string TempFilePrefix = "dyn-cloudfile"; 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 // 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 // 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 result = new List<(string filePath, string suffix)>();
var ogFilePath = Path.Join(configuration.GetValue<string>("Tus:StorePath"), fileId); var ogFilePath = Path.GetFullPath(Path.Join(configuration.GetValue<string>("Tus:StorePath"), fileId));
var fileSize = stream.Length; var fileSize = stream.Length;
var hash = await HashFileAsync(stream, fileSize: fileSize); var hash = await HashFileAsync(stream, fileSize: fileSize);
contentType ??= !fileName.Contains('.') ? "application/octet-stream" : MimeTypes.GetMimeType(fileName); contentType ??= !fileName.Contains('.') ? "application/octet-stream" : MimeTypes.GetMimeType(fileName);
@ -144,7 +144,7 @@ public class FileService(
case "audio": case "audio":
try try
{ {
var mediaInfo = await FFProbe.AnalyseAsync(stream); var mediaInfo = await FFProbe.AnalyseAsync(ogFilePath);
file.FileMeta = new Dictionary<string, object> file.FileMeta = new Dictionary<string, object>
{ {
["duration"] = mediaInfo.Duration.TotalSeconds, ["duration"] = mediaInfo.Duration.TotalSeconds,
@ -156,9 +156,10 @@ public class FileService(
["chapters"] = mediaInfo.Chapters, ["chapters"] = mediaInfo.Chapters,
}; };
} }
catch catch (Exception ex)
{ {
// ignored logger.LogError("File analyzed failed, unable collect video / audio information: {Message}",
ex.Message);
} }
break; break;
@ -179,7 +180,7 @@ public class FileService(
if (contentType.Split('/')[0] == "image") if (contentType.Split('/')[0] == "image")
{ {
// Skip compression for animated image types // Skip compression for animated image types
var animatedMimeTypes = function; var animatedMimeTypes = AnimatedImageTypes;
if (animatedMimeTypes.Contains(contentType)) if (animatedMimeTypes.Contains(contentType))
{ {
logger.LogInformation( logger.LogInformation(
@ -192,7 +193,7 @@ public class FileService(
file.MimeType = "image/webp"; 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}"); var imagePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}");
vipsImage.Autorot(); vipsImage.Autorot();
vipsImage.WriteToFile(imagePath + ".webp", vipsImage.WriteToFile(imagePath + ".webp",
@ -216,11 +217,8 @@ public class FileService(
} }
else else
{ {
var tempFilePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}"); // No extra process for video, add it to the upload queue.
await using var fileStream = File.Create(tempFilePath); result.Add((ogFilePath, string.Empty));
stream.Position = 0;
await stream.CopyToAsync(fileStream);
result.Add((tempFilePath, string.Empty));
} }
logger.LogInformation("Optimized file {fileId}, now uploading...", fileId); logger.LogInformation("Optimized file {fileId}, now uploading...", fileId);