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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.IO;
using MediaBrowser.Model.IO;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Net;
namespace Emby.Server.Implementations.Images
{
public abstract class BaseDynamicImageProvider<T> : IHasItemChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, IHasOrder
where T : IHasMetadata
{
protected IFileSystem FileSystem { get; private set; }
protected IProviderManager ProviderManager { get; private set; }
protected IApplicationPaths ApplicationPaths { get; private set; }
protected IImageProcessor ImageProcessor { get; set; }
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
{
ApplicationPaths = applicationPaths;
ProviderManager = providerManager;
FileSystem = fileSystem;
ImageProcessor = imageProcessor;
}
protected virtual bool Supports(IHasImages item)
{
return true;
}
public virtual IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
return new List<ImageType>
{
ImageType.Primary,
ImageType.Thumb
};
}
private IEnumerable<ImageType> GetEnabledImages(IHasImages item)
{
//var options = ProviderManager.GetMetadataOptions(item);
return GetSupportedImages(item);
//return GetSupportedImages(item).Where(i => IsEnabled(options, i, item)).ToList();
}
private bool IsEnabled(MetadataOptions options, ImageType type, IHasImages item)
{
if (type == ImageType.Backdrop)
{
if (item.LockedFields.Contains(MetadataFields.Backdrops))
{
return false;
}
}
else if (type == ImageType.Screenshot)
{
if (item.LockedFields.Contains(MetadataFields.Screenshots))
{
return false;
}
}
else
{
if (item.LockedFields.Contains(MetadataFields.Images))
{
return false;
}
}
return options.IsEnabled(type);
}
public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
if (!Supports(item))
{
return ItemUpdateType.None;
}
var updateType = ItemUpdateType.None;
var supportedImages = GetEnabledImages(item).ToList();
if (supportedImages.Contains(ImageType.Primary))
{
var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
updateType = updateType | primaryResult;
}
if (supportedImages.Contains(ImageType.Thumb))
{
var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
updateType = updateType | thumbResult;
}
return updateType;
}
protected async Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
var image = item.GetImageInfo(imageType, 0);
if (image != null)
{
if (!image.IsLocalFile)
{
return ItemUpdateType.None;
}
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
{
return ItemUpdateType.None;
}
}
var items = await GetItemsWithImages(item).ConfigureAwait(false);
return await FetchToFileInternal(item, items, imageType, cancellationToken).ConfigureAwait(false);
}
protected async Task<ItemUpdateType> FetchToFileInternal(IHasImages item,
List<BaseItem> itemsWithImages,
ImageType imageType,
CancellationToken cancellationToken)
{
var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPathWithoutExtension));
string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0);
if (string.IsNullOrWhiteSpace(outputPath))
{
return ItemUpdateType.None;
}
var mimeType = MimeTypes.GetMimeType(outputPath);
if (string.Equals(mimeType, "application/octet-stream", StringComparison.OrdinalIgnoreCase))
{
mimeType = "image/png";
}
await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).ConfigureAwait(false);
return ItemUpdateType.ImageUpdate;
}
protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
protected string CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
{
return CreateCollage(primaryItem, items, outputPath, 640, 360);
}
protected virtual IEnumerable<string> GetStripCollageImagePaths(IHasImages primaryItem, IEnumerable<BaseItem> items)
{
return items
.Select(i =>
{
var image = i.GetImageInfo(ImageType.Primary, 0);
if (image != null && image.IsLocalFile)
{
return image.Path;
}
image = i.GetImageInfo(ImageType.Thumb, 0);
if (image != null && image.IsLocalFile)
{
return image.Path;
}
return null;
})
.Where(i => !string.IsNullOrWhiteSpace(i));
}
protected string CreatePosterCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
{
return CreateCollage(primaryItem, items, outputPath, 400, 600);
}
protected string CreateSquareCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
{
return CreateCollage(primaryItem, items, outputPath, 600, 600);
}
protected string CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height)
{
return CreateCollage(primaryItem, items, outputPath, width, height);
}
private string CreateCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height)
{
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPath));
var options = new ImageCollageOptions
{
Height = height,
Width = width,
OutputPath = outputPath,
InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
};
if (options.InputPaths.Length == 0)
{
return null;
}
if (!ImageProcessor.SupportsImageCollageCreation)
{
return null;
}
ImageProcessor.CreateImageCollage(options);
return outputPath;
}
public string Name
{
get { return "Dynamic Image Provider"; }
}
protected virtual string CreateImage(IHasImages item,
List<BaseItem> itemsWithImages,
string outputPathWithoutExtension,
ImageType imageType,
int imageIndex)
{
if (itemsWithImages.Count == 0)
{
return null;
}
string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
if (imageType == ImageType.Thumb)
{
return CreateThumbCollage(item, itemsWithImages, outputPath);
}
if (imageType == ImageType.Primary)
{
if (item is UserView)
{
return CreateSquareCollage(item, itemsWithImages, outputPath);
}
if (item is Playlist || item is MusicGenre || item is Genre || item is GameGenre || item is PhotoAlbum)
{
return CreateSquareCollage(item, itemsWithImages, outputPath);
}
return CreatePosterCollage(item, itemsWithImages, outputPath);
}
throw new ArgumentException("Unexpected image type");
}
protected virtual int MaxImageAgeDays
{
get { return 7; }
}
public bool HasChanged(IHasMetadata item, IDirectoryService directoryServicee)
{
if (!Supports(item))
{
return false;
}
var supportedImages = GetEnabledImages(item).ToList();
if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
{
return true;
}
if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
{
return true;
}
return false;
}
protected bool HasChanged(IHasImages item, ImageType type)
{
var image = item.GetImageInfo(type, 0);
if (image != null)
{
if (!image.IsLocalFile)
{
return false;
}
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
{
return false;
}
var age = DateTime.UtcNow - image.DateModified;
if (age.TotalDays <= MaxImageAgeDays)
{
return false;
}
}
return true;
}
protected List<BaseItem> GetFinalItems(List<BaseItem> items)
{
return GetFinalItems(items, 4);
}
protected virtual List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
{
// Rotate the images once every x days
var random = DateTime.Now.DayOfYear % MaxImageAgeDays;
return items
.OrderBy(i => (random + string.Empty + items.IndexOf(i)).GetMD5())
.Take(limit)
.OrderBy(i => i.Name)
.ToList();
}
public int Order
{
get
{
// Run before the default image provider which will download placeholders
return 0;
}
}
protected string CreateSingleImage(List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
{
var image = itemsWithImages
.Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.GetImagePath(imageType)))
.Select(i => i.GetImagePath(imageType))
.FirstOrDefault();
if (string.IsNullOrWhiteSpace(image))
{
return null;
}
var ext = Path.GetExtension(image);
var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
FileSystem.CopyFile(image, outputPath, true);
return outputPath;
}
}
}
|