💄 Optimize the background file analyze process
This commit is contained in:
@@ -151,8 +151,6 @@ public class BroadcastEventHandler(
|
||||
var hasCompression = false;
|
||||
var hasThumbnail = false;
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Processing file {FileId} in background...", fileId);
|
||||
|
||||
var fileToUpdate = await scopedDb.Files.AsNoTracking().FirstAsync(f => f.Id == fileId);
|
||||
@@ -174,9 +172,10 @@ public class BroadcastEventHandler(
|
||||
break;
|
||||
}
|
||||
|
||||
newMimeType = "image/webp";
|
||||
using (var vipsImage = Image.NewFromFile(processingFilePath))
|
||||
try
|
||||
{
|
||||
newMimeType = "image/webp";
|
||||
using var vipsImage = Image.NewFromFile(processingFilePath);
|
||||
var imageToWrite = vipsImage;
|
||||
|
||||
if (vipsImage.Interpretation is Enums.Interpretation.Scrgb or Enums.Interpretation.Xyz)
|
||||
@@ -206,6 +205,12 @@ public class BroadcastEventHandler(
|
||||
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;
|
||||
|
||||
@@ -248,7 +253,10 @@ public class BroadcastEventHandler(
|
||||
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);
|
||||
|
||||
@@ -278,19 +286,12 @@ public class BroadcastEventHandler(
|
||||
.SetProperty(f => f.HasCompression, hasCompression)
|
||||
.SetProperty(f => f.HasThumbnail, hasThumbnail)
|
||||
);
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
logger.LogError(err, "Failed to process and upload {FileId}", fileId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
// Only delete temp file after successful upload and db update
|
||||
if (isTempFile)
|
||||
{
|
||||
File.Delete(processingFilePath);
|
||||
}
|
||||
|
||||
await fs._PurgeCacheAsync(fileId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user