/
ImageProcessingFileNameProvider.cs
78 lines (64 loc) · 3.13 KB
/
ImageProcessingFileNameProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections.Generic;
using System.Linq;
using Orchard.Caching;
using Orchard.MediaProcessing.Models;
namespace Orchard.MediaProcessing.Services {
public class ImageProcessingFileNameProvider : IImageProcessingFileNameProvider {
private readonly IImageProfileService _imageProfileService;
private readonly ICacheManager _cacheManager;
private readonly ISignals _signals;
public ImageProcessingFileNameProvider(IImageProfileService imageProfileService, ICacheManager cacheManager, ISignals signals) {
_imageProfileService = imageProfileService;
_cacheManager = cacheManager;
_signals = signals;
}
public string GetFileName(string profile, string path) {
var cacheKey = GetCacheKey(profile, path);
var profileCache = GetProfileCache(profile);
string fileName;
if (!profileCache.TryGetValue(cacheKey, out fileName)) {
return null;
}
return fileName;
}
public void UpdateFileName(string profile, string path, string fileName) {
var cacheKey = GetCacheKey(profile, path);
var profileCache = GetProfileCache(profile);
string existingFileName;
if (profileCache.TryGetValue(cacheKey, out existingFileName) && existingFileName == fileName) {
return;
}
profileCache[cacheKey] = fileName;
var profilePart = _imageProfileService.GetImageProfileByName(profile);
// profile might not exist in the db if its a dynamic profile
if (profilePart != null) {
var fileNameRecord = profilePart.FileNames.FirstOrDefault(f => f.Path == path);
if (fileNameRecord == null) {
fileNameRecord = new FileNameRecord {
Path = path,
ImageProfilePartRecord = profilePart.Record
};
profilePart.FileNames.Add(fileNameRecord);
}
fileNameRecord.FileName = fileName;
}
}
private static string GetCacheKey(string profile, string path) {
return profile + "_" + path;
}
private IDictionary<string, string> GetProfileCache(string profile) {
return _cacheManager.Get("MediaProcessing_" + profile, true, ctx => {
ctx.Monitor(_signals.When("MediaProcessing_Saved_" + profile));
var dictionary = new Dictionary<string, string>();
var profilePart = _imageProfileService.GetImageProfileByName(profile);
if (profilePart != null) {
foreach (var fileNameRecord in profilePart.FileNames) {
var fileNameRecordCacheKey = GetCacheKey(fileNameRecord.ImageProfilePartRecord.Name, fileNameRecord.Path);
dictionary.Add(fileNameRecordCacheKey, fileNameRecord.FileName);
}
}
return dictionary;
});
}
}
}