💄 Optimize the background file analyze process

This commit is contained in:
2025-09-26 23:29:27 +08:00
parent d75876a772
commit 51b6f7309e

View File

@@ -151,8 +151,6 @@ public class BroadcastEventHandler(
var hasCompression = false; var hasCompression = false;
var hasThumbnail = false; var hasThumbnail = false;
try
{
logger.LogInformation("Processing file {FileId} in background...", fileId); logger.LogInformation("Processing file {FileId} in background...", fileId);
var fileToUpdate = await scopedDb.Files.AsNoTracking().FirstAsync(f => f.Id == fileId); var fileToUpdate = await scopedDb.Files.AsNoTracking().FirstAsync(f => f.Id == fileId);
@@ -174,9 +172,10 @@ public class BroadcastEventHandler(
break; break;
} }
newMimeType = "image/webp"; try
using (var vipsImage = Image.NewFromFile(processingFilePath))
{ {
newMimeType = "image/webp";
using var vipsImage = Image.NewFromFile(processingFilePath);
var imageToWrite = vipsImage; var imageToWrite = vipsImage;
if (vipsImage.Interpretation is Enums.Interpretation.Scrgb or Enums.Interpretation.Xyz) if (vipsImage.Interpretation is Enums.Interpretation.Scrgb or Enums.Interpretation.Xyz)
@@ -206,6 +205,12 @@ public class BroadcastEventHandler(
imageToWrite.Dispose(); imageToWrite.Dispose();
} }
} }
catch (Exception ex)
{
logger.LogError(ex, "Failed to optimize image {FileId}, uploading original", fileId);
uploads.Add((processingFilePath, string.Empty, contentType, false));
newMimeType = contentType;
}
break; break;
@@ -248,7 +253,10 @@ public class BroadcastEventHandler(
break; break;
} }
} }
else uploads.Add((processingFilePath, string.Empty, contentType, false)); else
{
uploads.Add((processingFilePath, string.Empty, contentType, false));
}
logger.LogInformation("Optimized file {FileId}, now uploading...", fileId); logger.LogInformation("Optimized file {FileId}, now uploading...", fileId);
@@ -278,19 +286,12 @@ public class BroadcastEventHandler(
.SetProperty(f => f.HasCompression, hasCompression) .SetProperty(f => f.HasCompression, hasCompression)
.SetProperty(f => f.HasThumbnail, hasThumbnail) .SetProperty(f => f.HasThumbnail, hasThumbnail)
); );
}
} // Only delete temp file after successful upload and db update
catch (Exception err)
{
logger.LogError(err, "Failed to process and upload {FileId}", fileId);
}
finally
{
if (isTempFile) if (isTempFile)
{
File.Delete(processingFilePath); File.Delete(processingFilePath);
} }
await fs._PurgeCacheAsync(fileId); await fs._PurgeCacheAsync(fileId);
} }
}
} }