Skip to content

Commit

Permalink
Formatting and code styling ImageProfileManager
Browse files Browse the repository at this point in the history
  • Loading branch information
BenedekFarkas committed Apr 17, 2024
1 parent 097979d commit 03f775d
Showing 1 changed file with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
// path is the publicUrl of the media, so it might contain url-encoded chars

// try to load the processed filename from cache
var filePath = _fileNameProvider.GetFileName(profileName, System.Web.HttpUtility.UrlDecode(path));
var filePath = _fileNameProvider.GetFileName(profileName, HttpUtility.UrlDecode(path));
bool process = false;

// Before checking everything else, ensure that the content item that needs to be processed has a ImagePart.
Expand All @@ -79,10 +79,10 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
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)));
var profileFilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, HttpUtility.UrlDecode(path)));

if (_storageProvider.FileExists(profileFilePath)) {
_fileNameProvider.UpdateFileName(profileName, System.Web.HttpUtility.UrlDecode(path), profileFilePath);
_fileNameProvider.UpdateFileName(profileName, HttpUtility.UrlDecode(path), profileFilePath);
filePath = profileFilePath;
}
}
Expand All @@ -93,28 +93,24 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co

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);

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();
else if (TryGetImageLastUpdated(path, out DateTime 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 {
}
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);
Expand All @@ -129,9 +125,11 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co

if (customFilters == null || !customFilters.Any(c => c != null)) {
profilePart = _profileService.GetImageProfileByName(profileName);
if (profilePart == null)
return String.Empty;
} else {
if (profilePart == null) {
return string.Empty;
}
}
else {
profilePart = _services.ContentManager.New<ImageProfilePart>("ImageProfile");
profilePart.Name = profileName;
foreach (var customFilter in customFilters) {
Expand All @@ -142,13 +140,13 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
// prevent two requests from processing the same file at the same time
// this is only thread safe at the machine level, so there is a try/catch later
// to handle cross machines concurrency
lock (String.Intern(path)) {
lock (string.Intern(path)) {
using (var image = GetImage(path)) {
if (image == null) {
return null;
}

var filterContext = new FilterContext { Media = image, FilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, System.Web.HttpUtility.UrlDecode(path))) };
var filterContext = new FilterContext { Media = image, FilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, HttpUtility.UrlDecode(path))) };

var tokens = new Dictionary<string, object>();
// if a content item is provided, use it while tokenizing
Expand All @@ -166,7 +164,7 @@ public string GetImageProfileUrl(string path, string profileName, ContentItem co
descriptor.Filter(filterContext);
}

_fileNameProvider.UpdateFileName(profileName, System.Web.HttpUtility.UrlDecode(path), filterContext.FilePath);
_fileNameProvider.UpdateFileName(profileName, HttpUtility.UrlDecode(path), filterContext.FilePath);

if (!filterContext.Saved) {
try {
Expand All @@ -187,7 +185,8 @@ 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 @@ -215,14 +214,14 @@ 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);
}
}

// http://blob.storage-provider.net/my-image.jpg
Uri absoluteUri;
if (Uri.TryCreate(path, UriKind.Absolute, out absoluteUri)) {
if (Uri.TryCreate(path, UriKind.Absolute, out Uri absoluteUri)) {
return new WebClient().OpenRead(absoluteUri);
}

Expand All @@ -248,13 +247,12 @@ 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);

return _storageProvider.Combine(
_storageProvider.Combine(profileName.GetHashCode().ToString("x").ToLowerInvariant(), fileLocation.GetHashCode().ToString("x").ToLowerInvariant()),
filenameWithExtension);
_storageProvider.Combine(_profileService.GetNameHashCode(profileName), _profileService.GetNameHashCode(fileLocation)),
filenameWithExtension);
}
}
}

0 comments on commit 03f775d

Please sign in to comment.