ImageSaver.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Extensions;
  11. using MediaBrowser.Common.Configuration;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Controller.Entities;
  14. using MediaBrowser.Controller.Entities.Audio;
  15. using MediaBrowser.Controller.IO;
  16. using MediaBrowser.Controller.Library;
  17. using MediaBrowser.Model.Configuration;
  18. using MediaBrowser.Model.Entities;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.Net;
  21. using Microsoft.Extensions.Logging;
  22. using Episode = MediaBrowser.Controller.Entities.TV.Episode;
  23. using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum;
  24. using Person = MediaBrowser.Controller.Entities.Person;
  25. using Season = MediaBrowser.Controller.Entities.TV.Season;
  26. namespace MediaBrowser.Providers.Manager
  27. {
  28. /// <summary>
  29. /// Class ImageSaver.
  30. /// </summary>
  31. public class ImageSaver
  32. {
  33. /// <summary>
  34. /// The _config.
  35. /// </summary>
  36. private readonly IServerConfigurationManager _config;
  37. /// <summary>
  38. /// The _directory watchers.
  39. /// </summary>
  40. private readonly ILibraryMonitor _libraryMonitor;
  41. private readonly IFileSystem _fileSystem;
  42. private readonly ILogger _logger;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="ImageSaver" /> class.
  45. /// </summary>
  46. /// <param name="config">The config.</param>
  47. /// <param name="libraryMonitor">The directory watchers.</param>
  48. /// <param name="fileSystem">The file system.</param>
  49. /// <param name="logger">The logger.</param>
  50. public ImageSaver(IServerConfigurationManager config, ILibraryMonitor libraryMonitor, IFileSystem fileSystem, ILogger logger)
  51. {
  52. _config = config;
  53. _libraryMonitor = libraryMonitor;
  54. _fileSystem = fileSystem;
  55. _logger = logger;
  56. }
  57. private bool EnableExtraThumbsDuplication
  58. {
  59. get
  60. {
  61. var config = _config.GetConfiguration<XbmcMetadataOptions>("xbmcmetadata");
  62. return config.EnableExtraThumbsDuplication;
  63. }
  64. }
  65. /// <summary>
  66. /// Saves the image.
  67. /// </summary>
  68. /// <param name="item">The item.</param>
  69. /// <param name="source">The source.</param>
  70. /// <param name="mimeType">Type of the MIME.</param>
  71. /// <param name="type">The type.</param>
  72. /// <param name="imageIndex">Index of the image.</param>
  73. /// <param name="cancellationToken">The cancellation token.</param>
  74. /// <returns>Task.</returns>
  75. /// <exception cref="ArgumentNullException">mimeType.</exception>
  76. public Task SaveImage(BaseItem item, Stream source, string mimeType, ImageType type, int? imageIndex, CancellationToken cancellationToken)
  77. {
  78. return SaveImage(item, source, mimeType, type, imageIndex, null, cancellationToken);
  79. }
  80. public async Task SaveImage(BaseItem item, Stream source, string mimeType, ImageType type, int? imageIndex, bool? saveLocallyWithMedia, CancellationToken cancellationToken)
  81. {
  82. ArgumentException.ThrowIfNullOrEmpty(mimeType);
  83. var saveLocally = item.SupportsLocalMetadata && item.IsSaveLocalMetadataEnabled() && !item.ExtraType.HasValue && item is not Audio;
  84. if (type != ImageType.Primary && item is Episode)
  85. {
  86. saveLocally = false;
  87. }
  88. if (!item.IsFileProtocol)
  89. {
  90. saveLocally = false;
  91. // If season is virtual under a physical series, save locally
  92. if (item is Season season)
  93. {
  94. var series = season.Series;
  95. if (series is not null && series.SupportsLocalMetadata && series.IsSaveLocalMetadataEnabled())
  96. {
  97. saveLocally = true;
  98. }
  99. }
  100. }
  101. if (saveLocallyWithMedia.HasValue && !saveLocallyWithMedia.Value)
  102. {
  103. saveLocally = saveLocallyWithMedia.Value;
  104. }
  105. if (!imageIndex.HasValue && item.AllowsMultipleImages(type))
  106. {
  107. imageIndex = item.GetImages(type).Count();
  108. }
  109. var index = imageIndex ?? 0;
  110. var paths = GetSavePaths(item, type, imageIndex, mimeType, saveLocally);
  111. string[] retryPaths = [];
  112. if (saveLocally)
  113. {
  114. retryPaths = GetSavePaths(item, type, imageIndex, mimeType, false);
  115. }
  116. // If there are more than one output paths, the stream will need to be seekable
  117. if (paths.Length > 1 && !source.CanSeek)
  118. {
  119. var memoryStream = new MemoryStream();
  120. await using (source.ConfigureAwait(false))
  121. {
  122. await source.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
  123. }
  124. source = memoryStream;
  125. }
  126. var currentImage = GetCurrentImage(item, type, index);
  127. var currentImageIsLocalFile = currentImage is not null && currentImage.IsLocalFile;
  128. var currentImagePath = currentImage?.Path;
  129. var savedPaths = new List<string>();
  130. await using (source.ConfigureAwait(false))
  131. {
  132. for (int i = 0; i < paths.Length; i++)
  133. {
  134. if (i != 0)
  135. {
  136. source.Position = 0;
  137. }
  138. string retryPath = null;
  139. if (paths.Length == retryPaths.Length)
  140. {
  141. retryPath = retryPaths[i];
  142. }
  143. var savedPath = await SaveImageToLocation(source, paths[i], retryPath, cancellationToken).ConfigureAwait(false);
  144. savedPaths.Add(savedPath);
  145. }
  146. }
  147. // Set the path into the item
  148. SetImagePath(item, type, imageIndex, savedPaths[0]);
  149. // Delete the current path
  150. if (currentImageIsLocalFile
  151. && !savedPaths.Contains(currentImagePath, StringComparison.OrdinalIgnoreCase)
  152. && (saveLocally || currentImagePath.Contains(_config.ApplicationPaths.InternalMetadataPath, StringComparison.OrdinalIgnoreCase)))
  153. {
  154. var currentPath = currentImagePath;
  155. _logger.LogInformation("Deleting previous image {0}", currentPath);
  156. _libraryMonitor.ReportFileSystemChangeBeginning(currentPath);
  157. try
  158. {
  159. _fileSystem.DeleteFile(currentPath);
  160. // Remove local episode metadata directory if it exists and is empty
  161. var directory = Path.GetDirectoryName(currentPath);
  162. if (item is Episode && directory.Equals("metadata", StringComparison.Ordinal))
  163. {
  164. var parentDirectoryPath = Directory.GetParent(currentPath).FullName;
  165. if (_fileSystem.DirectoryExists(parentDirectoryPath) && !_fileSystem.GetFiles(parentDirectoryPath).Any())
  166. {
  167. try
  168. {
  169. _logger.LogInformation("Deleting empty local metadata folder {Folder}", parentDirectoryPath);
  170. Directory.Delete(parentDirectoryPath);
  171. }
  172. catch (UnauthorizedAccessException ex)
  173. {
  174. _logger.LogError(ex, "Error deleting directory {Path}", parentDirectoryPath);
  175. }
  176. catch (IOException ex)
  177. {
  178. _logger.LogError(ex, "Error deleting directory {Path}", parentDirectoryPath);
  179. }
  180. }
  181. }
  182. }
  183. catch (FileNotFoundException)
  184. {
  185. }
  186. finally
  187. {
  188. _libraryMonitor.ReportFileSystemChangeComplete(currentPath, false);
  189. }
  190. }
  191. }
  192. public async Task SaveImage(Stream source, string path)
  193. {
  194. await SaveImageToLocation(source, path, path, CancellationToken.None).ConfigureAwait(false);
  195. }
  196. private async Task<string> SaveImageToLocation(Stream source, string path, string retryPath, CancellationToken cancellationToken)
  197. {
  198. try
  199. {
  200. await SaveImageToLocation(source, path, cancellationToken).ConfigureAwait(false);
  201. return path;
  202. }
  203. catch (UnauthorizedAccessException)
  204. {
  205. var retry = !string.IsNullOrWhiteSpace(retryPath) &&
  206. !string.Equals(path, retryPath, StringComparison.OrdinalIgnoreCase);
  207. if (retry)
  208. {
  209. _logger.LogError("UnauthorizedAccessException - Access to path {0} is denied. Will retry saving to {1}", path, retryPath);
  210. }
  211. else
  212. {
  213. throw;
  214. }
  215. }
  216. catch (IOException ex)
  217. {
  218. var retry = !string.IsNullOrWhiteSpace(retryPath) &&
  219. !string.Equals(path, retryPath, StringComparison.OrdinalIgnoreCase);
  220. if (retry)
  221. {
  222. _logger.LogError(ex, "IOException saving to {0}. Will retry saving to {1}", path, retryPath);
  223. }
  224. else
  225. {
  226. throw;
  227. }
  228. }
  229. await SaveImageToLocation(source, retryPath, cancellationToken).ConfigureAwait(false);
  230. return retryPath;
  231. }
  232. /// <summary>
  233. /// Saves the image to location.
  234. /// </summary>
  235. /// <param name="source">The source.</param>
  236. /// <param name="path">The path.</param>
  237. /// <param name="cancellationToken">The cancellation token.</param>
  238. /// <returns>Task.</returns>
  239. private async Task SaveImageToLocation(Stream source, string path, CancellationToken cancellationToken)
  240. {
  241. _logger.LogDebug("Saving image to {0}", path);
  242. var parentFolder = Path.GetDirectoryName(path);
  243. try
  244. {
  245. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  246. _libraryMonitor.ReportFileSystemChangeBeginning(parentFolder);
  247. Directory.CreateDirectory(Path.GetDirectoryName(path));
  248. _fileSystem.SetAttributes(path, false, false);
  249. var fileStreamOptions = AsyncFile.WriteOptions;
  250. fileStreamOptions.Mode = FileMode.Create;
  251. if (source.CanSeek)
  252. {
  253. fileStreamOptions.PreallocationSize = source.Length;
  254. }
  255. var fs = new FileStream(path, fileStreamOptions);
  256. await using (fs.ConfigureAwait(false))
  257. {
  258. await source.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
  259. }
  260. if (_config.Configuration.SaveMetadataHidden)
  261. {
  262. SetHidden(path, true);
  263. }
  264. }
  265. finally
  266. {
  267. _libraryMonitor.ReportFileSystemChangeComplete(path, false);
  268. _libraryMonitor.ReportFileSystemChangeComplete(parentFolder, false);
  269. }
  270. }
  271. private void SetHidden(string path, bool hidden)
  272. {
  273. try
  274. {
  275. _fileSystem.SetHidden(path, hidden);
  276. }
  277. catch (Exception ex)
  278. {
  279. _logger.LogError(ex, "Error setting hidden attribute on {0}", path);
  280. }
  281. }
  282. /// <summary>
  283. /// Gets the save paths.
  284. /// </summary>
  285. /// <param name="item">The item.</param>
  286. /// <param name="type">The type.</param>
  287. /// <param name="imageIndex">Index of the image.</param>
  288. /// <param name="mimeType">Type of the MIME.</param>
  289. /// <param name="saveLocally">if set to <c>true</c> [save locally].</param>
  290. /// <returns>IEnumerable{System.String}.</returns>
  291. private string[] GetSavePaths(BaseItem item, ImageType type, int? imageIndex, string mimeType, bool saveLocally)
  292. {
  293. if (!saveLocally || (_config.Configuration.ImageSavingConvention == ImageSavingConvention.Legacy))
  294. {
  295. return new[] { GetStandardSavePath(item, type, imageIndex, mimeType, saveLocally) };
  296. }
  297. return GetCompatibleSavePaths(item, type, imageIndex, mimeType);
  298. }
  299. /// <summary>
  300. /// Gets the current image path.
  301. /// </summary>
  302. /// <param name="item">The item.</param>
  303. /// <param name="type">The type.</param>
  304. /// <param name="imageIndex">Index of the image.</param>
  305. /// <returns>System.String.</returns>
  306. /// <exception cref="ArgumentNullException">
  307. /// imageIndex
  308. /// or
  309. /// imageIndex.
  310. /// </exception>
  311. private ItemImageInfo GetCurrentImage(BaseItem item, ImageType type, int imageIndex)
  312. {
  313. return item.GetImageInfo(type, imageIndex);
  314. }
  315. /// <summary>
  316. /// Sets the image path.
  317. /// </summary>
  318. /// <param name="item">The item.</param>
  319. /// <param name="type">The type.</param>
  320. /// <param name="imageIndex">Index of the image.</param>
  321. /// <param name="path">The path.</param>
  322. /// <exception cref="ArgumentNullException">imageIndex
  323. /// or
  324. /// imageIndex.
  325. /// </exception>
  326. private void SetImagePath(BaseItem item, ImageType type, int? imageIndex, string path)
  327. {
  328. item.SetImagePath(type, imageIndex ?? 0, _fileSystem.GetFileInfo(path));
  329. }
  330. /// <summary>
  331. /// Gets the save path.
  332. /// </summary>
  333. /// <param name="item">The item.</param>
  334. /// <param name="type">The type.</param>
  335. /// <param name="imageIndex">Index of the image.</param>
  336. /// <param name="mimeType">Type of the MIME.</param>
  337. /// <param name="saveLocally">if set to <c>true</c> [save locally].</param>
  338. /// <returns>System.String.</returns>
  339. /// <exception cref="ArgumentNullException">
  340. /// imageIndex
  341. /// or
  342. /// imageIndex.
  343. /// </exception>
  344. private string GetStandardSavePath(BaseItem item, ImageType type, int? imageIndex, string mimeType, bool saveLocally)
  345. {
  346. var season = item as Season;
  347. var extension = MimeTypes.ToExtension(mimeType);
  348. if (string.IsNullOrWhiteSpace(extension))
  349. {
  350. throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unable to determine image file extension from mime type {0}", mimeType));
  351. }
  352. if (string.Equals(extension, ".jpeg", StringComparison.OrdinalIgnoreCase))
  353. {
  354. extension = ".jpg";
  355. }
  356. extension = extension.ToLowerInvariant();
  357. if (type == ImageType.Primary && saveLocally)
  358. {
  359. if (season is not null && season.IndexNumber.HasValue)
  360. {
  361. var seriesFolder = season.SeriesPath;
  362. var seasonMarker = season.IndexNumber.Value == 0
  363. ? "-specials"
  364. : season.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  365. var imageFilename = "season" + seasonMarker + "-poster" + extension;
  366. return Path.Combine(seriesFolder, imageFilename);
  367. }
  368. }
  369. if (type == ImageType.Backdrop && saveLocally)
  370. {
  371. if (season is not null
  372. && season.IndexNumber.HasValue
  373. && (imageIndex is null || imageIndex == 0))
  374. {
  375. var seriesFolder = season.SeriesPath;
  376. var seasonMarker = season.IndexNumber.Value == 0
  377. ? "-specials"
  378. : season.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  379. var imageFilename = "season" + seasonMarker + "-fanart" + extension;
  380. return Path.Combine(seriesFolder, imageFilename);
  381. }
  382. }
  383. if (type == ImageType.Thumb && saveLocally)
  384. {
  385. if (season is not null && season.IndexNumber.HasValue)
  386. {
  387. var seriesFolder = season.SeriesPath;
  388. var seasonMarker = season.IndexNumber.Value == 0
  389. ? "-specials"
  390. : season.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  391. var imageFilename = "season" + seasonMarker + "-landscape" + extension;
  392. return Path.Combine(seriesFolder, imageFilename);
  393. }
  394. if (item.IsInMixedFolder)
  395. {
  396. return GetSavePathForItemInMixedFolder(item, type, "landscape", extension);
  397. }
  398. return Path.Combine(item.ContainingFolderPath, "landscape" + extension);
  399. }
  400. if (type == ImageType.Banner && saveLocally)
  401. {
  402. if (season is not null && season.IndexNumber.HasValue)
  403. {
  404. var seriesFolder = season.SeriesPath;
  405. var seasonMarker = season.IndexNumber.Value == 0
  406. ? "-specials"
  407. : season.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  408. var imageFilename = "season" + seasonMarker + "-banner" + extension;
  409. return Path.Combine(seriesFolder, imageFilename);
  410. }
  411. }
  412. string filename;
  413. var folderName = item is MusicAlbum ||
  414. item is MusicArtist ||
  415. item is PhotoAlbum ||
  416. item is Person ||
  417. (saveLocally && _config.Configuration.ImageSavingConvention == ImageSavingConvention.Legacy) ?
  418. "folder" :
  419. "poster";
  420. switch (type)
  421. {
  422. case ImageType.Art:
  423. filename = "clearart";
  424. break;
  425. case ImageType.BoxRear:
  426. filename = "back";
  427. break;
  428. case ImageType.Thumb:
  429. filename = "landscape";
  430. break;
  431. case ImageType.Disc:
  432. filename = item is MusicAlbum ? "cdart" : "disc";
  433. break;
  434. case ImageType.Primary:
  435. filename = saveLocally && item is Episode ? Path.GetFileNameWithoutExtension(item.Path) : folderName;
  436. break;
  437. case ImageType.Backdrop:
  438. filename = GetBackdropSaveFilename(item.GetImages(type), "backdrop", "backdrop", imageIndex);
  439. break;
  440. default:
  441. filename = type.ToString().ToLowerInvariant();
  442. break;
  443. }
  444. string path = null;
  445. if (saveLocally)
  446. {
  447. if (type == ImageType.Primary && item is Episode)
  448. {
  449. path = Path.Combine(Path.GetDirectoryName(item.Path), filename + "-thumb" + extension);
  450. }
  451. else if (item.IsInMixedFolder)
  452. {
  453. path = GetSavePathForItemInMixedFolder(item, type, filename, extension);
  454. }
  455. if (string.IsNullOrEmpty(path))
  456. {
  457. path = Path.Combine(item.ContainingFolderPath, filename + extension);
  458. }
  459. }
  460. // None of the save local conditions passed, so store it in our internal folders
  461. if (string.IsNullOrEmpty(path))
  462. {
  463. if (string.IsNullOrEmpty(filename))
  464. {
  465. filename = folderName;
  466. }
  467. path = Path.Combine(item.GetInternalMetadataPath(), filename + extension);
  468. }
  469. return path;
  470. }
  471. private string GetBackdropSaveFilename(IEnumerable<ItemImageInfo> images, string zeroIndexFilename, string numberedIndexPrefix, int? index)
  472. {
  473. if (index.HasValue && index.Value == 0)
  474. {
  475. return zeroIndexFilename;
  476. }
  477. var filenames = images.Select(i => Path.GetFileNameWithoutExtension(i.Path)).ToList();
  478. var current = 1;
  479. while (filenames.Contains(numberedIndexPrefix + current.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase))
  480. {
  481. current++;
  482. }
  483. return numberedIndexPrefix + current.ToString(CultureInfo.InvariantCulture);
  484. }
  485. /// <summary>
  486. /// Gets the compatible save paths.
  487. /// </summary>
  488. /// <param name="item">The item.</param>
  489. /// <param name="type">The type.</param>
  490. /// <param name="imageIndex">Index of the image.</param>
  491. /// <param name="mimeType">Type of the MIME.</param>
  492. /// <returns>IEnumerable{System.String}.</returns>
  493. /// <exception cref="ArgumentNullException">imageIndex.</exception>
  494. private string[] GetCompatibleSavePaths(BaseItem item, ImageType type, int? imageIndex, string mimeType)
  495. {
  496. var season = item as Season;
  497. var extension = MimeTypes.ToExtension(mimeType);
  498. // Backdrop paths
  499. if (type == ImageType.Backdrop)
  500. {
  501. if (!imageIndex.HasValue)
  502. {
  503. throw new ArgumentNullException(nameof(imageIndex));
  504. }
  505. if (imageIndex.Value == 0)
  506. {
  507. if (item.IsInMixedFolder)
  508. {
  509. return new[] { GetSavePathForItemInMixedFolder(item, type, "fanart", extension) };
  510. }
  511. if (season is not null && season.IndexNumber.HasValue)
  512. {
  513. var seriesFolder = season.SeriesPath;
  514. var seasonMarker = season.IndexNumber.Value == 0
  515. ? "-specials"
  516. : season.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  517. var imageFilename = "season" + seasonMarker + "-fanart" + extension;
  518. return new[] { Path.Combine(seriesFolder, imageFilename) };
  519. }
  520. return new[]
  521. {
  522. Path.Combine(item.ContainingFolderPath, "fanart" + extension)
  523. };
  524. }
  525. var outputIndex = imageIndex.Value;
  526. if (item.IsInMixedFolder)
  527. {
  528. return new[] { GetSavePathForItemInMixedFolder(item, type, "fanart" + outputIndex.ToString(CultureInfo.InvariantCulture), extension) };
  529. }
  530. var extraFanartFilename = GetBackdropSaveFilename(item.GetImages(ImageType.Backdrop), "fanart", "fanart", outputIndex);
  531. var list = new List<string>
  532. {
  533. Path.Combine(item.ContainingFolderPath, "extrafanart", extraFanartFilename + extension)
  534. };
  535. if (EnableExtraThumbsDuplication)
  536. {
  537. list.Add(Path.Combine(item.ContainingFolderPath, "extrathumbs", "thumb" + outputIndex.ToString(CultureInfo.InvariantCulture) + extension));
  538. }
  539. return list.ToArray();
  540. }
  541. if (type == ImageType.Primary)
  542. {
  543. if (season is not null && season.IndexNumber.HasValue)
  544. {
  545. var seriesFolder = season.SeriesPath;
  546. var seasonMarker = season.IndexNumber.Value == 0
  547. ? "-specials"
  548. : season.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  549. var imageFilename = "season" + seasonMarker + "-poster" + extension;
  550. return new[] { Path.Combine(seriesFolder, imageFilename) };
  551. }
  552. if (item is Episode)
  553. {
  554. var seasonFolder = Path.GetDirectoryName(item.Path);
  555. var imageFilename = Path.GetFileNameWithoutExtension(item.Path) + "-thumb" + extension;
  556. return new[] { Path.Combine(seasonFolder, imageFilename) };
  557. }
  558. if (item.IsInMixedFolder || item is MusicVideo)
  559. {
  560. return new[] { GetSavePathForItemInMixedFolder(item, type, string.Empty, extension) };
  561. }
  562. if (item is MusicAlbum || item is MusicArtist)
  563. {
  564. return new[] { Path.Combine(item.ContainingFolderPath, "folder" + extension) };
  565. }
  566. return new[] { Path.Combine(item.ContainingFolderPath, "poster" + extension) };
  567. }
  568. // All other paths are the same
  569. return new[] { GetStandardSavePath(item, type, imageIndex, mimeType, true) };
  570. }
  571. /// <summary>
  572. /// Gets the save path for item in mixed folder.
  573. /// </summary>
  574. /// <param name="item">The item.</param>
  575. /// <param name="type">The type.</param>
  576. /// <param name="imageFilename">The image filename.</param>
  577. /// <param name="extension">The extension.</param>
  578. /// <returns>System.String.</returns>
  579. private string GetSavePathForItemInMixedFolder(BaseItem item, ImageType type, string imageFilename, string extension)
  580. {
  581. if (type == ImageType.Primary)
  582. {
  583. imageFilename = "poster";
  584. }
  585. var folder = Path.GetDirectoryName(item.Path);
  586. return Path.Combine(folder, Path.GetFileNameWithoutExtension(item.Path) + "-" + imageFilename + extension);
  587. }
  588. }
  589. }