From 35f1c8d5700303c04824d77c022b1cc78975b29c Mon Sep 17 00:00:00 2001
From: Andrea Piovanelli <83577153+AndreaPiovanelli@users.noreply.github.com>
Date: Thu, 29 Feb 2024 19:39:32 +0100
Subject: [PATCH] Corrections on media resizing process (#8762)
* Added checks to GetImageProfileUrl function to avoid trying to resize files with no ImagePart. If no content item is passed as a parameter, file gets to be processed anyway (throwing and logging exception is the file isn't processable - e.g. is a svg file)
* Added reference to Orchard.MediaLibrary.
---
.../Orchard.MediaProcessing.csproj | 6 +-
.../Services/ImageProfileManager.cs | 83 +++++++++++--------
2 files changed, 52 insertions(+), 37 deletions(-)
diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Orchard.MediaProcessing.csproj b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Orchard.MediaProcessing.csproj
index a505f057e6b..624b6adfe7d 100644
--- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Orchard.MediaProcessing.csproj
+++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Orchard.MediaProcessing.csproj
@@ -123,6 +123,10 @@
{642A49D7-8752-4177-80D6-BFBBCFAD3DE0}
Orchard.Forms
+
+ {73a7688a-5bd3-4f7e-adfa-ce36c5a10e3b}
+ Orchard.MediaLibrary
+
{6f759635-13d7-4e94-bcc9-80445d63f117}
Orchard.Tokens
@@ -241,4 +245,4 @@
-
+
\ No newline at end of file
diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs
index d1d521a41fe..2ff726644a2 100644
--- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs
+++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs
@@ -4,12 +4,12 @@
using System.IO;
using System.Linq;
using System.Net;
-using System.Security.Cryptography;
using System.Web;
using Orchard.ContentManagement;
using Orchard.FileSystems.Media;
using Orchard.Forms.Services;
using Orchard.Logging;
+using Orchard.MediaLibrary.Models;
using Orchard.MediaProcessing.Descriptors.Filter;
using Orchard.MediaProcessing.Media;
using Orchard.MediaProcessing.Models;
@@ -44,7 +44,7 @@ public ImageProfileManager(
public ILogger Logger { get; set; }
- public string GetImageProfileUrl(string path, string profileName) {
+ public string GetImageProfileUrl(string path, string profileName) {
return GetImageProfileUrl(path, profileName, null, new FilterRecord[] { });
}
@@ -69,42 +69,56 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
var filePath = _fileNameProvider.GetFileName(profileName, System.Web.HttpUtility.UrlDecode(path));
bool process = false;
- //after reboot the app cache is empty so we reload the image in the cache if it exists in the _Profiles folder
- if (string.IsNullOrEmpty(filePath)) {
- var profileFilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, System.Web.HttpUtility.UrlDecode(path)));
-
- if (_storageProvider.FileExists(profileFilePath)) {
- _fileNameProvider.UpdateFileName(profileName, System.Web.HttpUtility.UrlDecode(path), profileFilePath);
- filePath = profileFilePath;
+ // Before checking everything else, ensure that the content item that needs to be processed has a ImagePart.
+ // If it's not the case (e.g. if media is a svg file), processing would throw a exception.
+ // If content item is null (it means it's not passed as a parameter of the ResizeMediaUrl call),
+ // this function processes the file like it did before this patch;
+ // this means it could possibly throw and log exceptions for svg files.
+ bool checkForProfile = (contentItem == null || contentItem.Has());
+
+ if (checkForProfile) {
+ //after reboot the app cache is empty so we reload the image in the cache if it exists in the _Profiles folder
+ if (string.IsNullOrEmpty(filePath)) {
+ var profileFilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, System.Web.HttpUtility.UrlDecode(path)));
+
+ if (_storageProvider.FileExists(profileFilePath)) {
+ _fileNameProvider.UpdateFileName(profileName, System.Web.HttpUtility.UrlDecode(path), profileFilePath);
+ filePath = profileFilePath;
+ }
}
- }
-
- // if the filename is not cached, process it
- if (string.IsNullOrEmpty(filePath)) {
- Logger.Debug("FilePath is null, processing required, profile {0} for image {1}", profileName, path);
- process = true;
- }
+ // if the filename is not cached, process it
+ if (string.IsNullOrEmpty(filePath)) {
+ Logger.Debug("FilePath is null, processing required, profile {0} for image {1}", profileName, path);
+
+ process = true;
+ }
// the processd file doesn't exist anymore, process it
- else if (!_storageProvider.FileExists(filePath)) {
- Logger.Debug("Processed file no longer exists, processing required, profile {0} for image {1}", profileName, path);
+ else if (!_storageProvider.FileExists(filePath)) {
+ Logger.Debug("Processed file no longer exists, processing required, profile {0} for image {1}", profileName, path);
- process = true;
- }
+ process = true;
+ }
- // if the original file is more recent, process it
- else {
- DateTime pathLastUpdated;
- if (TryGetImageLastUpdated(path, out pathLastUpdated)) {
- var filePathLastUpdated = _storageProvider.GetFile(filePath).GetLastUpdated();
+ // if the original file is more recent, process it
+ else {
+ DateTime pathLastUpdated;
+ if (TryGetImageLastUpdated(path, out pathLastUpdated)) {
+ var filePathLastUpdated = _storageProvider.GetFile(filePath).GetLastUpdated();
- if (pathLastUpdated > filePathLastUpdated) {
- Logger.Debug("Original file more recent, processing required, profile {0} for image {1}", profileName, path);
+ if (pathLastUpdated > filePathLastUpdated) {
+ Logger.Debug("Original file more recent, processing required, profile {0} for image {1}", profileName, path);
- process = true;
+ process = true;
+ }
}
}
+ } else {
+ // Since media with no ImagePart have no profile, filePath is null, so it's set again to its original path on the storage provider.
+ if (string.IsNullOrWhiteSpace(filePath)) {
+ filePath = _storageProvider.GetStoragePath(path);
+ }
}
// todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries.
@@ -117,11 +131,10 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
profilePart = _profileService.GetImageProfileByName(profileName);
if (profilePart == null)
return String.Empty;
- }
- else {
+ } else {
profilePart = _services.ContentManager.New("ImageProfile");
profilePart.Name = profileName;
- foreach (var customFilter in customFilters) {
+ foreach (var customFilter in customFilters) {
profilePart.Filters.Add(customFilter);
}
}
@@ -174,8 +187,7 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
// the storage provider may have altered the filepath
filterContext.FilePath = newFile.GetPath();
}
- }
- catch(Exception e) {
+ } catch (Exception e) {
Logger.Error(e, "A profile could not be processed: " + path);
}
}
@@ -203,8 +215,7 @@ private Stream GetImage(string path) {
try {
var file = _storageProvider.GetFile(storagePath);
return file.OpenRead();
- }
- catch(Exception e) {
+ } catch (Exception e) {
Logger.Error(e, "path:" + path + " storagePath:" + storagePath);
}
}
@@ -236,7 +247,7 @@ private bool TryGetImageLastUpdated(string path, out DateTime lastUpdated) {
}
private string FormatProfilePath(string profileName, string path) {
-
+
var filenameWithExtension = Path.GetFileName(path) ?? "";
var fileLocation = path.Substring(0, path.Length - filenameWithExtension.Length);