Skip to content

Commit

Permalink
Corrections on media resizing process (#8762)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
AndreaPiovanelli authored Feb 29, 2024
1 parent c7d10fd commit 35f1c8d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
<Project>{642A49D7-8752-4177-80D6-BFBBCFAD3DE0}</Project>
<Name>Orchard.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.MediaLibrary\Orchard.MediaLibrary.csproj">
<Project>{73a7688a-5bd3-4f7e-adfa-ce36c5a10e3b}</Project>
<Name>Orchard.MediaLibrary</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Tokens\Orchard.Tokens.csproj">
<Project>{6f759635-13d7-4e94-bcc9-80445d63f117}</Project>
<Name>Orchard.Tokens</Name>
Expand Down Expand Up @@ -241,4 +245,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[] { });
}

Expand All @@ -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<ImagePart>());

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.
Expand All @@ -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<ImageProfilePart>("ImageProfile");
profilePart.Name = profileName;
foreach (var customFilter in customFilters) {
foreach (var customFilter in customFilters) {
profilePart.Filters.Add(customFilter);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 35f1c8d

Please sign in to comment.