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
|
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers
{
/// <summary>
/// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
/// </summary>
public class ImageFromMediaLocationProvider : BaseMetadataProvider
{
public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
: base(logManager, configurationManager)
{
}
/// <summary>
/// Supportses the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
public override bool Supports(BaseItem item)
{
return item.LocationType == LocationType.FileSystem && item.ResolveArgs.IsDirectory;
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
/// <summary>
/// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
/// </summary>
/// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
protected override bool RefreshOnFileSystemStampChange
{
get
{
return true;
}
}
/// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
// Make sure current image paths still exist
ValidateImages(item);
cancellationToken.ThrowIfCancellationRequested();
// Make sure current backdrop paths still exist
ValidateBackdrops(item);
cancellationToken.ThrowIfCancellationRequested();
PopulateBaseItemImages(item);
SetLastRefreshed(item, DateTime.UtcNow);
return TrueTaskResult;
}
/// <summary>
/// Validates that images within the item are still on the file system
/// </summary>
/// <param name="item">The item.</param>
private void ValidateImages(BaseItem item)
{
if (item.Images == null)
{
return;
}
// Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
var deletedKeys = item.Images.Keys.Where(image =>
{
var path = item.Images[image];
return IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null;
}).ToList();
// Now remove them from the dictionary
foreach (var key in deletedKeys)
{
item.Images.Remove(key);
}
}
/// <summary>
/// Validates that backdrops within the item are still on the file system
/// </summary>
/// <param name="item">The item.</param>
private void ValidateBackdrops(BaseItem item)
{
if (item.BackdropImagePaths == null)
{
return;
}
// Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null).ToList();
// Now remove them from the dictionary
foreach (var path in deletedImages)
{
item.BackdropImagePaths.Remove(path);
}
}
/// <summary>
/// Determines whether [is in same directory] [the specified item].
/// </summary>
/// <param name="item">The item.</param>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
private bool IsInMetaLocation(BaseItem item, string path)
{
return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Gets the image.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="filenameWithoutExtension">The filename without extension.</param>
/// <returns>FileSystemInfo.</returns>
protected virtual FileSystemInfo GetImage(BaseItem item, string filenameWithoutExtension)
{
return item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + ".png")) ?? item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + ".jpg"));
}
/// <summary>
/// Fills in image paths based on files win the folder
/// </summary>
/// <param name="item">The item.</param>
private void PopulateBaseItemImages(BaseItem item)
{
var backdropFiles = new List<string>();
var screenshotFiles = new List<string>();
// Primary Image
var image = GetImage(item, "folder");
if (image != null)
{
item.SetImage(ImageType.Primary, image.FullName);
}
// Logo Image
image = GetImage(item, "logo");
if (image != null)
{
item.SetImage(ImageType.Logo, image.FullName);
}
// Banner Image
image = GetImage(item, "banner");
if (image != null)
{
item.SetImage(ImageType.Banner, image.FullName);
}
// Clearart
image = GetImage(item, "clearart");
if (image != null)
{
item.SetImage(ImageType.Art, image.FullName);
}
// Thumbnail Image
image = GetImage(item, "thumb");
if (image != null)
{
item.SetImage(ImageType.Thumb, image.FullName);
}
// Thumbnail Image
image = GetImage(item, "box");
if (image != null)
{
item.SetImage(ImageType.Box, image.FullName);
}
// Thumbnail Image
image = GetImage(item, "menu");
if (image != null)
{
item.SetImage(ImageType.Menu, image.FullName);
}
// Backdrop Image
image = GetImage(item, "backdrop");
if (image != null)
{
backdropFiles.Add(image.FullName);
}
var unfound = 0;
for (var i = 1; i <= 20; i++)
{
// Backdrop Image
image = GetImage(item, "backdrop" + i);
if (image != null)
{
backdropFiles.Add(image.FullName);
}
else
{
unfound++;
if (unfound >= 3)
{
break;
}
}
}
if (backdropFiles.Count > 0)
{
item.BackdropImagePaths = backdropFiles;
}
// Screenshot Image
image = GetImage(item, "screenshot");
if (image != null)
{
screenshotFiles.Add(image.FullName);
}
unfound = 0;
for (var i = 1; i <= 20; i++)
{
// Screenshot Image
image = GetImage(item, "screenshot" + i);
if (image != null)
{
screenshotFiles.Add(image.FullName);
}
else
{
unfound++;
if (unfound >= 3)
{
break;
}
}
}
if (screenshotFiles.Count > 0)
{
item.ScreenshotImagePaths = screenshotFiles;
}
}
}
}
|