| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.IO; |
| | 5 | | using System.Linq; |
| | 6 | | using Jellyfin.Extensions; |
| | 7 | | using MediaBrowser.Controller.Entities; |
| | 8 | | using MediaBrowser.Controller.Entities.Audio; |
| | 9 | | using MediaBrowser.Controller.Entities.Movies; |
| | 10 | | using MediaBrowser.Controller.Entities.TV; |
| | 11 | | using MediaBrowser.Controller.Providers; |
| | 12 | | using MediaBrowser.Model.Entities; |
| | 13 | | using MediaBrowser.Model.IO; |
| | 14 | |
|
| | 15 | | namespace MediaBrowser.LocalMetadata.Images |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Local image provider. |
| | 19 | | /// </summary> |
| | 20 | | public class LocalImageProvider : ILocalImageProvider, IHasOrder |
| | 21 | | { |
| 1 | 22 | | private static readonly string[] _commonImageFileNames = |
| 1 | 23 | | { |
| 1 | 24 | | "poster", |
| 1 | 25 | | "folder", |
| 1 | 26 | | "cover", |
| 1 | 27 | | "default" |
| 1 | 28 | | }; |
| | 29 | |
|
| 1 | 30 | | private static readonly string[] _musicImageFileNames = |
| 1 | 31 | | { |
| 1 | 32 | | "folder", |
| 1 | 33 | | "poster", |
| 1 | 34 | | "cover", |
| 1 | 35 | | "jacket", |
| 1 | 36 | | "default", |
| 1 | 37 | | "albumart" |
| 1 | 38 | | }; |
| | 39 | |
|
| 1 | 40 | | private static readonly string[] _personImageFileNames = |
| 1 | 41 | | { |
| 1 | 42 | | "folder", |
| 1 | 43 | | "poster" |
| 1 | 44 | | }; |
| | 45 | |
|
| 1 | 46 | | private static readonly string[] _seriesImageFileNames = |
| 1 | 47 | | { |
| 1 | 48 | | "poster", |
| 1 | 49 | | "folder", |
| 1 | 50 | | "cover", |
| 1 | 51 | | "default", |
| 1 | 52 | | "show" |
| 1 | 53 | | }; |
| | 54 | |
|
| 1 | 55 | | private static readonly string[] _videoImageFileNames = |
| 1 | 56 | | { |
| 1 | 57 | | "poster", |
| 1 | 58 | | "folder", |
| 1 | 59 | | "cover", |
| 1 | 60 | | "default", |
| 1 | 61 | | "movie" |
| 1 | 62 | | }; |
| | 63 | |
|
| | 64 | | private readonly IFileSystem _fileSystem; |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Initializes a new instance of the <see cref="LocalImageProvider"/> class. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | 70 | | public LocalImageProvider(IFileSystem fileSystem) |
| | 71 | | { |
| 34 | 72 | | _fileSystem = fileSystem; |
| 34 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <inheritdoc /> |
| 49 | 76 | | public string Name => "Local Images"; |
| | 77 | |
|
| | 78 | | /// <inheritdoc /> |
| 49 | 79 | | public int Order => 0; |
| | 80 | |
|
| | 81 | | /// <inheritdoc /> |
| | 82 | | public bool Supports(BaseItem item) |
| | 83 | | { |
| 49 | 84 | | if (item.SupportsLocalMetadata) |
| | 85 | | { |
| | 86 | | // Episode has its own provider |
| 49 | 87 | | if (item is Episode || item is Audio || item is Photo) |
| | 88 | | { |
| 0 | 89 | | return false; |
| | 90 | | } |
| | 91 | |
|
| 49 | 92 | | return true; |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | if (item.LocationType == LocationType.Virtual) |
| | 96 | | { |
| 0 | 97 | | var season = item as Season; |
| 0 | 98 | | var series = season?.Series; |
| 0 | 99 | | if (series is not null && series.IsFileProtocol) |
| | 100 | | { |
| 0 | 101 | | return true; |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | return false; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | private static IEnumerable<FileSystemMetadata> GetFiles(BaseItem item, bool includeDirectories, IDirectoryServic |
| | 109 | | { |
| 49 | 110 | | if (!item.IsFileProtocol) |
| | 111 | | { |
| 0 | 112 | | return Enumerable.Empty<FileSystemMetadata>(); |
| | 113 | | } |
| | 114 | |
|
| 49 | 115 | | var path = item.ContainingFolderPath; |
| | 116 | |
|
| | 117 | | // Exit if the cache dir does not exist, alternative solution is to create it, but that's a lot of empty dir |
| 49 | 118 | | if (!Directory.Exists(path)) |
| | 119 | | { |
| 0 | 120 | | return Enumerable.Empty<FileSystemMetadata>(); |
| | 121 | | } |
| | 122 | |
|
| 49 | 123 | | return directoryService.GetFileSystemEntries(path) |
| 49 | 124 | | .Where(i => |
| 49 | 125 | | (includeDirectories && i.IsDirectory) |
| 49 | 126 | | || BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparison.OrdinalIgnoreCase)) |
| 49 | 127 | | .OrderBy(i => Array.IndexOf(BaseItem.SupportedImageExtensions, i.Extension ?? string.Empty)); |
| | 128 | | } |
| | 129 | |
|
| | 130 | | /// <inheritdoc /> |
| | 131 | | public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IDirectoryService directoryService) |
| | 132 | | { |
| 49 | 133 | | var files = GetFiles(item, true, directoryService).ToList(); |
| | 134 | |
|
| 49 | 135 | | var list = new List<LocalImageInfo>(); |
| | 136 | |
|
| 49 | 137 | | PopulateImages(item, list, files, true, directoryService); |
| | 138 | |
|
| 49 | 139 | | return list; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Get images for item. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="item">The item.</param> |
| | 146 | | /// <param name="path">The images path.</param> |
| | 147 | | /// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param> |
| | 148 | | /// <returns>The local image info.</returns> |
| | 149 | | public IEnumerable<LocalImageInfo> GetImages(BaseItem item, string path, IDirectoryService directoryService) |
| | 150 | | { |
| 0 | 151 | | return GetImages(item, new[] { path }, directoryService); |
| | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Get images for item from multiple paths. |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="item">The item.</param> |
| | 158 | | /// <param name="paths">The image paths.</param> |
| | 159 | | /// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param> |
| | 160 | | /// <returns>The local image info.</returns> |
| | 161 | | public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IEnumerable<string> paths, IDirectoryService directo |
| | 162 | | { |
| 13 | 163 | | IEnumerable<FileSystemMetadata> files = paths.SelectMany(i => _fileSystem.GetFiles(i, BaseItem.SupportedImag |
| | 164 | |
|
| 13 | 165 | | files = files |
| 13 | 166 | | .OrderBy(i => Array.IndexOf(BaseItem.SupportedImageExtensions, i.Extension ?? string.Empty)); |
| | 167 | |
|
| 13 | 168 | | var list = new List<LocalImageInfo>(); |
| | 169 | |
|
| 13 | 170 | | PopulateImages(item, list, files.ToList(), false, directoryService); |
| | 171 | |
|
| 13 | 172 | | return list; |
| | 173 | | } |
| | 174 | |
|
| | 175 | | private void PopulateImages(BaseItem item, List<LocalImageInfo> images, List<FileSystemMetadata> files, bool sup |
| | 176 | | { |
| 62 | 177 | | if (supportParentSeriesFiles) |
| | 178 | | { |
| 49 | 179 | | if (item is Season season) |
| | 180 | | { |
| 0 | 181 | | PopulateSeasonImagesFromSeriesFolder(season, images, directoryService); |
| | 182 | | } |
| | 183 | | } |
| | 184 | |
|
| 62 | 185 | | var imagePrefix = item.FileNameWithoutExtension + "-"; |
| 62 | 186 | | var isInMixedFolder = item.IsInMixedFolder; |
| | 187 | |
|
| 62 | 188 | | PopulatePrimaryImages(item, images, files, imagePrefix, isInMixedFolder); |
| | 189 | |
|
| 62 | 190 | | var added = false; |
| 62 | 191 | | var isEpisode = item is Episode; |
| 62 | 192 | | var isSong = item.GetType() == typeof(Audio); |
| 62 | 193 | | var isPerson = item is Person; |
| | 194 | |
|
| | 195 | | // Logo |
| 62 | 196 | | if (!isEpisode && !isSong && !isPerson) |
| | 197 | | { |
| 62 | 198 | | added = AddImage(files, images, "logo", imagePrefix, isInMixedFolder, ImageType.Logo); |
| 62 | 199 | | if (!added) |
| | 200 | | { |
| 62 | 201 | | AddImage(files, images, "clearlogo", imagePrefix, isInMixedFolder, ImageType.Logo); |
| | 202 | | } |
| | 203 | | } |
| | 204 | |
|
| | 205 | | // Art |
| 62 | 206 | | if (!isEpisode && !isSong && !isPerson) |
| | 207 | | { |
| 62 | 208 | | AddImage(files, images, "clearart", imagePrefix, isInMixedFolder, ImageType.Art); |
| | 209 | | } |
| | 210 | |
|
| | 211 | | // For music albums, prefer cdart before disc |
| 62 | 212 | | if (item is MusicAlbum) |
| | 213 | | { |
| 0 | 214 | | added = AddImage(files, images, "cdart", imagePrefix, isInMixedFolder, ImageType.Disc); |
| | 215 | |
|
| 0 | 216 | | if (!added) |
| | 217 | | { |
| 0 | 218 | | AddImage(files, images, "disc", imagePrefix, isInMixedFolder, ImageType.Disc); |
| | 219 | | } |
| | 220 | | } |
| 62 | 221 | | else if (item is Video || item is BoxSet) |
| | 222 | | { |
| 0 | 223 | | added = AddImage(files, images, "disc", imagePrefix, isInMixedFolder, ImageType.Disc); |
| | 224 | |
|
| 0 | 225 | | if (!added) |
| | 226 | | { |
| 0 | 227 | | added = AddImage(files, images, "cdart", imagePrefix, isInMixedFolder, ImageType.Disc); |
| | 228 | | } |
| | 229 | |
|
| 0 | 230 | | if (!added) |
| | 231 | | { |
| 0 | 232 | | AddImage(files, images, "discart", imagePrefix, isInMixedFolder, ImageType.Disc); |
| | 233 | | } |
| | 234 | | } |
| | 235 | |
|
| | 236 | | // Banner |
| 62 | 237 | | if (!isEpisode && !isSong && !isPerson) |
| | 238 | | { |
| 62 | 239 | | AddImage(files, images, "banner", imagePrefix, isInMixedFolder, ImageType.Banner); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | // Thumb |
| 62 | 243 | | if (!isEpisode && !isSong && !isPerson) |
| | 244 | | { |
| 62 | 245 | | added = AddImage(files, images, "landscape", imagePrefix, isInMixedFolder, ImageType.Thumb); |
| 62 | 246 | | if (!added) |
| | 247 | | { |
| 62 | 248 | | AddImage(files, images, "thumb", imagePrefix, isInMixedFolder, ImageType.Thumb); |
| | 249 | | } |
| | 250 | | } |
| | 251 | |
|
| 62 | 252 | | if (!isEpisode && !isSong && !isPerson) |
| | 253 | | { |
| 62 | 254 | | PopulateBackdrops(item, images, files, imagePrefix, isInMixedFolder); |
| | 255 | | } |
| 62 | 256 | | } |
| | 257 | |
|
| | 258 | | private void PopulatePrimaryImages(BaseItem item, List<LocalImageInfo> images, List<FileSystemMetadata> files, s |
| | 259 | | { |
| | 260 | | string[] imageFileNames; |
| | 261 | |
|
| 62 | 262 | | if (item is MusicAlbum || item is MusicArtist || item is PhotoAlbum) |
| | 263 | | { |
| | 264 | | // these prefer folder |
| 0 | 265 | | imageFileNames = _musicImageFileNames; |
| | 266 | | } |
| 62 | 267 | | else if (item is Person) |
| | 268 | | { |
| | 269 | | // these prefer folder |
| 0 | 270 | | imageFileNames = _personImageFileNames; |
| | 271 | | } |
| 62 | 272 | | else if (item is Series) |
| | 273 | | { |
| 0 | 274 | | imageFileNames = _seriesImageFileNames; |
| | 275 | | } |
| 62 | 276 | | else if (item is Video && item is not Episode) |
| | 277 | | { |
| 0 | 278 | | imageFileNames = _videoImageFileNames; |
| | 279 | | } |
| | 280 | | else |
| | 281 | | { |
| 62 | 282 | | imageFileNames = _commonImageFileNames; |
| | 283 | | } |
| | 284 | |
|
| 62 | 285 | | var fileNameWithoutExtension = item.FileNameWithoutExtension; |
| 62 | 286 | | if (!string.IsNullOrEmpty(fileNameWithoutExtension)) |
| | 287 | | { |
| 62 | 288 | | if (AddImage(files, images, fileNameWithoutExtension, ImageType.Primary)) |
| | 289 | | { |
| 0 | 290 | | return; |
| | 291 | | } |
| | 292 | | } |
| | 293 | |
|
| 620 | 294 | | foreach (var name in imageFileNames) |
| | 295 | | { |
| 248 | 296 | | if (AddImage(files, images, name, ImageType.Primary, imagePrefix)) |
| | 297 | | { |
| 0 | 298 | | return; |
| | 299 | | } |
| | 300 | | } |
| | 301 | |
|
| 62 | 302 | | if (!isInMixedFolder) |
| | 303 | | { |
| 620 | 304 | | foreach (var name in imageFileNames) |
| | 305 | | { |
| 248 | 306 | | if (AddImage(files, images, name, ImageType.Primary)) |
| | 307 | | { |
| 0 | 308 | | return; |
| | 309 | | } |
| | 310 | | } |
| | 311 | | } |
| 62 | 312 | | } |
| | 313 | |
|
| | 314 | | private void PopulateBackdrops(BaseItem item, List<LocalImageInfo> images, List<FileSystemMetadata> files, strin |
| | 315 | | { |
| 62 | 316 | | if (!string.IsNullOrEmpty(item.Path)) |
| | 317 | | { |
| 62 | 318 | | var name = item.FileNameWithoutExtension; |
| | 319 | |
|
| 62 | 320 | | if (!string.IsNullOrEmpty(name)) |
| | 321 | | { |
| 62 | 322 | | AddImage(files, images, name + "-fanart", ImageType.Backdrop, imagePrefix); |
| | 323 | |
|
| | 324 | | // Support without the prefix if it's in its own folder |
| 62 | 325 | | if (!isInMixedFolder) |
| | 326 | | { |
| 62 | 327 | | AddImage(files, images, name + "-fanart", ImageType.Backdrop); |
| | 328 | | } |
| | 329 | | } |
| | 330 | | } |
| | 331 | |
|
| 62 | 332 | | PopulateBackdrops(images, files, imagePrefix, "fanart", "fanart-", isInMixedFolder, ImageType.Backdrop); |
| 62 | 333 | | PopulateBackdrops(images, files, imagePrefix, "background", "background-", isInMixedFolder, ImageType.Backdr |
| 62 | 334 | | PopulateBackdrops(images, files, imagePrefix, "art", "art-", isInMixedFolder, ImageType.Backdrop); |
| | 335 | |
|
| 62 | 336 | | var extraFanartFolder = files |
| 62 | 337 | | .FirstOrDefault(i => string.Equals(i.Name, "extrafanart", StringComparison.OrdinalIgnoreCase)); |
| | 338 | |
|
| 62 | 339 | | if (extraFanartFolder is not null) |
| | 340 | | { |
| 0 | 341 | | PopulateBackdropsFromExtraFanart(extraFanartFolder.FullName, images); |
| | 342 | | } |
| | 343 | |
|
| 62 | 344 | | PopulateBackdrops(images, files, imagePrefix, "backdrop", "backdrop", isInMixedFolder, ImageType.Backdrop); |
| 62 | 345 | | } |
| | 346 | |
|
| | 347 | | private void PopulateBackdropsFromExtraFanart(string path, List<LocalImageInfo> images) |
| | 348 | | { |
| 0 | 349 | | var imageFiles = _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, false); |
| | 350 | |
|
| 0 | 351 | | images.AddRange(imageFiles.Where(i => i.Length > 0).Select(i => new LocalImageInfo |
| 0 | 352 | | { |
| 0 | 353 | | FileInfo = i, |
| 0 | 354 | | Type = ImageType.Backdrop |
| 0 | 355 | | })); |
| 0 | 356 | | } |
| | 357 | |
|
| | 358 | | private void PopulateBackdrops(List<LocalImageInfo> images, List<FileSystemMetadata> files, string imagePrefix, |
| | 359 | | { |
| 248 | 360 | | AddImage(files, images, imagePrefix + firstFileName, type); |
| | 361 | |
|
| 248 | 362 | | var unfound = 0; |
| 1488 | 363 | | for (var i = 1; i <= 20; i++) |
| | 364 | | { |
| | 365 | | // Screenshot Image |
| 744 | 366 | | var found = AddImage(files, images, imagePrefix + subsequentFileNamePrefix + i, type); |
| | 367 | |
|
| 744 | 368 | | if (!found) |
| | 369 | | { |
| 744 | 370 | | unfound++; |
| | 371 | |
|
| 744 | 372 | | if (unfound >= 3) |
| | 373 | | { |
| | 374 | | break; |
| | 375 | | } |
| | 376 | | } |
| | 377 | | } |
| | 378 | |
|
| | 379 | | // Support without the prefix |
| 248 | 380 | | if (!isInMixedFolder) |
| | 381 | | { |
| 248 | 382 | | AddImage(files, images, firstFileName, type); |
| | 383 | |
|
| 248 | 384 | | unfound = 0; |
| 1488 | 385 | | for (var i = 1; i <= 20; i++) |
| | 386 | | { |
| | 387 | | // Screenshot Image |
| 744 | 388 | | var found = AddImage(files, images, subsequentFileNamePrefix + i, type); |
| | 389 | |
|
| 744 | 390 | | if (!found) |
| | 391 | | { |
| 744 | 392 | | unfound++; |
| | 393 | |
|
| 744 | 394 | | if (unfound >= 3) |
| | 395 | | { |
| | 396 | | break; |
| | 397 | | } |
| | 398 | | } |
| | 399 | | } |
| | 400 | | } |
| 248 | 401 | | } |
| | 402 | |
|
| | 403 | | private void PopulateSeasonImagesFromSeriesFolder(Season season, List<LocalImageInfo> images, IDirectoryService |
| | 404 | | { |
| 0 | 405 | | var seasonNumber = season.IndexNumber; |
| | 406 | |
|
| 0 | 407 | | var series = season.Series; |
| 0 | 408 | | if (!seasonNumber.HasValue || !series.IsFileProtocol) |
| | 409 | | { |
| 0 | 410 | | return; |
| | 411 | | } |
| | 412 | |
|
| 0 | 413 | | var seriesFiles = GetFiles(series, false, directoryService).ToList(); |
| | 414 | |
|
| | 415 | | // Try using the season name |
| 0 | 416 | | var prefix = season.Name.Replace(" ", string.Empty, StringComparison.Ordinal).ToLowerInvariant(); |
| | 417 | |
|
| 0 | 418 | | var filenamePrefixes = new List<string> { prefix }; |
| | 419 | |
|
| 0 | 420 | | var seasonMarker = seasonNumber.Value == 0 |
| 0 | 421 | | ? "-specials" |
| 0 | 422 | | : seasonNumber.Value.ToString("00", CultureInfo.InvariantCulture); |
| | 423 | |
|
| | 424 | | // Get this one directly from the file system since we have to go up a level |
| 0 | 425 | | if (!string.Equals(prefix, seasonMarker, StringComparison.OrdinalIgnoreCase)) |
| | 426 | | { |
| 0 | 427 | | filenamePrefixes.Add("season" + seasonMarker); |
| | 428 | | } |
| | 429 | |
|
| 0 | 430 | | foreach (var filename in filenamePrefixes) |
| | 431 | | { |
| 0 | 432 | | AddImage(seriesFiles, images, filename + "-poster", ImageType.Primary); |
| 0 | 433 | | AddImage(seriesFiles, images, filename + "-fanart", ImageType.Backdrop); |
| 0 | 434 | | AddImage(seriesFiles, images, filename + "-banner", ImageType.Banner); |
| 0 | 435 | | AddImage(seriesFiles, images, filename + "-landscape", ImageType.Thumb); |
| | 436 | | } |
| 0 | 437 | | } |
| | 438 | |
|
| | 439 | | private bool AddImage(List<FileSystemMetadata> files, List<LocalImageInfo> images, string name, string imagePref |
| | 440 | | { |
| 372 | 441 | | var added = AddImage(files, images, name, type, imagePrefix); |
| | 442 | |
|
| 372 | 443 | | if (!isInMixedFolder) |
| | 444 | | { |
| 372 | 445 | | if (AddImage(files, images, name, type)) |
| | 446 | | { |
| 0 | 447 | | added = true; |
| | 448 | | } |
| | 449 | | } |
| | 450 | |
|
| 372 | 451 | | return added; |
| | 452 | | } |
| | 453 | |
|
| | 454 | | private static bool AddImage(IReadOnlyList<FileSystemMetadata> files, List<LocalImageInfo> images, string name, |
| | 455 | | { |
| 3410 | 456 | | var image = GetImage(files, name, prefix); |
| | 457 | |
|
| 3410 | 458 | | if (image is null) |
| | 459 | | { |
| 3410 | 460 | | return false; |
| | 461 | | } |
| | 462 | |
|
| 0 | 463 | | images.Add(new LocalImageInfo |
| 0 | 464 | | { |
| 0 | 465 | | FileInfo = image, |
| 0 | 466 | | Type = type |
| 0 | 467 | | }); |
| | 468 | |
|
| 0 | 469 | | return true; |
| | 470 | | } |
| | 471 | |
|
| | 472 | | private static FileSystemMetadata? GetImage(IReadOnlyList<FileSystemMetadata> files, string name, string? prefix |
| | 473 | | { |
| 3410 | 474 | | var fileNameLength = name.Length + (prefix?.Length ?? 0); |
| 9900 | 475 | | for (var i = 0; i < files.Count; i++) |
| | 476 | | { |
| 1540 | 477 | | var file = files[i]; |
| 1540 | 478 | | if (file.IsDirectory || file.Length <= 0) |
| | 479 | | { |
| | 480 | | continue; |
| | 481 | | } |
| | 482 | |
|
| 0 | 483 | | var fileName = Path.GetFileNameWithoutExtension(file.FullName.AsSpan()); |
| 0 | 484 | | if (fileName.Length == fileNameLength |
| 0 | 485 | | && fileName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) |
| 0 | 486 | | && fileName.EndsWith(name, StringComparison.OrdinalIgnoreCase)) |
| | 487 | | { |
| 0 | 488 | | return file; |
| | 489 | | } |
| | 490 | | } |
| | 491 | |
|
| 3410 | 492 | | return null; |
| | 493 | | } |
| | 494 | | } |
| | 495 | | } |