mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
#19645: Fixing IageFileNameProvider exceptions
Work Item: 19645 --HG-- branch : 1.x extra : rebase_source : 6ace0d73f638e0502d875a27386bddb927bf8331
This commit is contained in:
@@ -16,30 +16,38 @@ namespace Orchard.MediaProcessing.Services {
|
||||
}
|
||||
|
||||
public string GetFileName(string profile, string path) {
|
||||
var fileNames = GetFileNames(profile);
|
||||
var cacheKey = GetCacheKey(profile, path);
|
||||
var profileCache = GetProfileCache(profile);
|
||||
|
||||
string fileName;
|
||||
if (!fileNames.TryGetValue(path, out fileName)) {
|
||||
if (!profileCache.TryGetValue(cacheKey, out fileName)) {
|
||||
var profilePart = _imageProfileService.GetImageProfileByName(profile);
|
||||
if (profilePart != null) {
|
||||
|
||||
foreach (var fileNameRecord in profilePart.FileNames) {
|
||||
fileNames.Add(path, fileNameRecord.FileName);
|
||||
var fileNameRecordCacheKey = GetCacheKey(fileNameRecord.ImageProfilePartRecord.Name, fileNameRecord.Path);
|
||||
profileCache.Add(fileNameRecordCacheKey, fileNameRecord.FileName);
|
||||
}
|
||||
|
||||
// now the cache has been initialized, call the same method again
|
||||
return GetFileName(profile, path);
|
||||
//now the cache has been initialized, look in the dictionary again
|
||||
if (!profileCache.TryGetValue(cacheKey, out fileName)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void UpdateFileName(string profile, string path, string fileName) {
|
||||
var fileNames = GetFileNames(profile);
|
||||
var cacheKey = GetCacheKey(profile, path);
|
||||
var profileCache = GetProfileCache(profile);
|
||||
|
||||
string existingFileName;
|
||||
if (fileNames.TryGetValue(path, out existingFileName) && existingFileName == fileName) {
|
||||
if (profileCache.TryGetValue(cacheKey, out existingFileName) && existingFileName == fileName) {
|
||||
return;
|
||||
}
|
||||
fileNames[path] = fileName;
|
||||
|
||||
profileCache[cacheKey] = fileName;
|
||||
var profilePart = _imageProfileService.GetImageProfileByName(profile);
|
||||
|
||||
// profile might not exist in the db if its a dynamic profile
|
||||
@@ -56,7 +64,11 @@ namespace Orchard.MediaProcessing.Services {
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, string> GetFileNames(string profile) {
|
||||
private static string GetCacheKey(string profile, string path) {
|
||||
return profile + "_" + path;
|
||||
}
|
||||
|
||||
private IDictionary<string, string> GetProfileCache(string profile) {
|
||||
return _cacheManager.Get("MediaProcessing_" + profile, ctx => {
|
||||
ctx.Monitor(_signals.When("MediaProcessing_" + profile + "_Saved"));
|
||||
return new Dictionary<string, string>();
|
||||
|
||||
@@ -74,34 +74,32 @@ namespace Orchard.MediaProcessing.Shapes {
|
||||
|
||||
[Shape]
|
||||
public void MediaUrl(dynamic Shape, dynamic Display, TextWriter Output, string Profile, string Path, ContentItem ContentItem, FilterRecord CustomFilter) {
|
||||
|
||||
try {
|
||||
Shape.IgnoreShapeTracer = true;
|
||||
var services = new Lazy<IOrchardServices>(() => _services.Value);
|
||||
var storageProvider = new Lazy<IStorageProvider>(() => _storageProvider.Value);
|
||||
|
||||
var services = new Lazy<IOrchardServices>(() => _services.Value);
|
||||
var storageProvider = new Lazy<IStorageProvider>(() => _storageProvider.Value);
|
||||
// try to load the processed filename from cache
|
||||
var filePath = _fileNameProvider.Value.GetFileName(Profile, Path);
|
||||
bool process = false;
|
||||
|
||||
// try to load the processed filename from cache
|
||||
var filePath = _fileNameProvider.Value.GetFileName(Profile, Path);
|
||||
bool process = false;
|
||||
|
||||
// if the filename is not cached, process it
|
||||
if (!string.IsNullOrEmpty(filePath)) {
|
||||
process = true;
|
||||
}
|
||||
|
||||
// the processd file doesn't exist anymore, process it
|
||||
else if (!storageProvider.Value.FileExists(filePath)) {
|
||||
// if the filename is not cached, process it
|
||||
if (!string.IsNullOrEmpty(filePath)) {
|
||||
process = true;
|
||||
}
|
||||
}
|
||||
|
||||
// the processd file doesn't exist anymore, process it
|
||||
else if (!storageProvider.Value.FileExists(filePath)) {
|
||||
process = true;
|
||||
}
|
||||
|
||||
// if the original file is more recent, process it
|
||||
else if (storageProvider.Value.GetFile(Path).GetLastUpdated() > storageProvider.Value.GetFile(filePath).GetLastUpdated()) {
|
||||
process = true;
|
||||
}
|
||||
|
||||
// todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries.
|
||||
if (process) {
|
||||
try {
|
||||
// if the original file is more recent, process it
|
||||
else if (storageProvider.Value.GetFile(Path).GetLastUpdated() > storageProvider.Value.GetFile(filePath).GetLastUpdated()) {
|
||||
process = true;
|
||||
}
|
||||
|
||||
// todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries.
|
||||
if (process) {
|
||||
ImageProfilePart profilePart;
|
||||
|
||||
if (CustomFilter == null) {
|
||||
@@ -158,16 +156,15 @@ namespace Orchard.MediaProcessing.Shapes {
|
||||
filePath = filterContext.FilePath;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "An error occured while processing {0} for image {1}", Profile, Path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// generate a timestamped url to force client caches to update if the file has changed
|
||||
var publicUrl = storageProvider.Value.GetPublicUrl(filePath);
|
||||
var timestamp = storageProvider.Value.GetFile(storageProvider.Value.GetLocalPath(filePath)).GetLastUpdated().Ticks;
|
||||
Output.Write(publicUrl + "?v=" + timestamp.ToString(CultureInfo.InvariantCulture));
|
||||
// generate a timestamped url to force client caches to update if the file has changed
|
||||
var publicUrl = storageProvider.Value.GetPublicUrl(filePath);
|
||||
var timestamp = storageProvider.Value.GetFile(storageProvider.Value.GetLocalPath(filePath)).GetLastUpdated().Ticks;
|
||||
Output.Write(publicUrl + "?v=" + timestamp.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "An error occured while rendering shape {0} for image {1}", Profile, Path);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Update this method once the storage provider has been updated
|
||||
|
||||
Reference in New Issue
Block a user