ImageSaver.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System.Globalization;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Entities.TV;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Model.Entities;
  9. using System;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Server.Implementations.Providers
  15. {
  16. /// <summary>
  17. /// Class ImageSaver
  18. /// </summary>
  19. public class ImageSaver
  20. {
  21. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  22. /// <summary>
  23. /// The _config
  24. /// </summary>
  25. private readonly IServerConfigurationManager _config;
  26. /// <summary>
  27. /// The remote image cache
  28. /// </summary>
  29. private readonly FileSystemRepository _remoteImageCache;
  30. /// <summary>
  31. /// The _directory watchers
  32. /// </summary>
  33. private readonly IDirectoryWatchers _directoryWatchers;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ImageSaver"/> class.
  36. /// </summary>
  37. /// <param name="config">The config.</param>
  38. /// <param name="directoryWatchers">The directory watchers.</param>
  39. public ImageSaver(IServerConfigurationManager config, IDirectoryWatchers directoryWatchers)
  40. {
  41. _config = config;
  42. _directoryWatchers = directoryWatchers;
  43. _remoteImageCache = new FileSystemRepository(config.ApplicationPaths.DownloadedImagesDataPath);
  44. }
  45. /// <summary>
  46. /// Saves the image.
  47. /// </summary>
  48. /// <param name="item">The item.</param>
  49. /// <param name="source">The source.</param>
  50. /// <param name="mimeType">Type of the MIME.</param>
  51. /// <param name="type">The type.</param>
  52. /// <param name="imageIndex">Index of the image.</param>
  53. /// <param name="cancellationToken">The cancellation token.</param>
  54. /// <returns>Task.</returns>
  55. public async Task SaveImage(BaseItem item, Stream source, string mimeType, ImageType type, int? imageIndex, CancellationToken cancellationToken)
  56. {
  57. if (string.IsNullOrEmpty(mimeType))
  58. {
  59. throw new ArgumentNullException("mimeType");
  60. }
  61. var saveLocally = _config.Configuration.SaveLocalMeta;
  62. if (item is IItemByName)
  63. {
  64. saveLocally = true;
  65. }
  66. else if (item is User)
  67. {
  68. saveLocally = true;
  69. }
  70. else if (item is Audio || item.Parent == null || string.IsNullOrEmpty(item.MetaLocation))
  71. {
  72. saveLocally = false;
  73. }
  74. if (type != ImageType.Primary)
  75. {
  76. if (item is Episode)
  77. {
  78. saveLocally = false;
  79. }
  80. }
  81. if (item.LocationType == LocationType.Remote || item.LocationType == LocationType.Virtual)
  82. {
  83. saveLocally = false;
  84. }
  85. var path = GetSavePath(item, type, imageIndex, mimeType, saveLocally);
  86. var currentPath = GetCurrentImagePath(item, type, imageIndex);
  87. try
  88. {
  89. _directoryWatchers.TemporarilyIgnore(path);
  90. using (source)
  91. {
  92. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  93. {
  94. await source.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  95. }
  96. }
  97. SetImagePath(item, type, imageIndex, path);
  98. if (!string.IsNullOrEmpty(currentPath) && !string.Equals(path, currentPath, StringComparison.OrdinalIgnoreCase))
  99. {
  100. File.Delete(currentPath);
  101. }
  102. }
  103. finally
  104. {
  105. _directoryWatchers.RemoveTempIgnore(path);
  106. }
  107. }
  108. private string GetCurrentImagePath(BaseItem item, ImageType type, int? imageIndex)
  109. {
  110. switch (type)
  111. {
  112. case ImageType.Screenshot:
  113. if (!imageIndex.HasValue)
  114. {
  115. throw new ArgumentNullException("imageIndex");
  116. }
  117. return item.ScreenshotImagePaths.Count > imageIndex.Value ? item.ScreenshotImagePaths[imageIndex.Value] : null;
  118. case ImageType.Backdrop:
  119. if (!imageIndex.HasValue)
  120. {
  121. throw new ArgumentNullException("imageIndex");
  122. }
  123. return item.BackdropImagePaths.Count > imageIndex.Value ? item.BackdropImagePaths[imageIndex.Value] : null;
  124. default:
  125. return item.GetImage(type);
  126. }
  127. }
  128. private void SetImagePath(BaseItem item, ImageType type, int? imageIndex, string path)
  129. {
  130. switch (type)
  131. {
  132. case ImageType.Screenshot:
  133. if (!imageIndex.HasValue)
  134. {
  135. throw new ArgumentNullException("imageIndex");
  136. }
  137. if (item.ScreenshotImagePaths.Count > imageIndex.Value)
  138. {
  139. item.ScreenshotImagePaths[imageIndex.Value] = path;
  140. }
  141. else
  142. {
  143. item.ScreenshotImagePaths.Add(path);
  144. }
  145. break;
  146. case ImageType.Backdrop:
  147. if (!imageIndex.HasValue)
  148. {
  149. throw new ArgumentNullException("imageIndex");
  150. }
  151. if (item.BackdropImagePaths.Count > imageIndex.Value)
  152. {
  153. item.BackdropImagePaths[imageIndex.Value] = path;
  154. }
  155. else
  156. {
  157. item.BackdropImagePaths.Add(path);
  158. }
  159. break;
  160. default:
  161. item.SetImage(type, path);
  162. break;
  163. }
  164. }
  165. /// <summary>
  166. /// Gets the save path.
  167. /// </summary>
  168. /// <param name="item">The item.</param>
  169. /// <param name="type">The type.</param>
  170. /// <param name="imageIndex">Index of the image.</param>
  171. /// <param name="mimeType">Type of the MIME.</param>
  172. /// <param name="saveLocally">if set to <c>true</c> [save locally].</param>
  173. /// <returns>System.String.</returns>
  174. /// <exception cref="System.ArgumentNullException">
  175. /// imageIndex
  176. /// or
  177. /// imageIndex
  178. /// </exception>
  179. private string GetSavePath(BaseItem item, ImageType type, int? imageIndex, string mimeType, bool saveLocally)
  180. {
  181. string filename;
  182. switch (type)
  183. {
  184. case ImageType.Art:
  185. filename = "clearart";
  186. break;
  187. case ImageType.Primary:
  188. filename = item is Episode ? Path.GetFileNameWithoutExtension(item.Path) : "folder";
  189. break;
  190. case ImageType.Backdrop:
  191. if (!imageIndex.HasValue)
  192. {
  193. throw new ArgumentNullException("imageIndex");
  194. }
  195. filename = imageIndex.Value == 0 ? "backdrop" : "backdrop" + imageIndex.Value.ToString(UsCulture);
  196. break;
  197. case ImageType.Screenshot:
  198. if (!imageIndex.HasValue)
  199. {
  200. throw new ArgumentNullException("imageIndex");
  201. }
  202. filename = imageIndex.Value == 0 ? "screenshot" : "screenshot" + imageIndex.Value.ToString(UsCulture);
  203. break;
  204. default:
  205. filename = type.ToString().ToLower();
  206. break;
  207. }
  208. var extension = mimeType.Split('/').Last();
  209. if (string.Equals(extension, "jpeg", StringComparison.OrdinalIgnoreCase))
  210. {
  211. extension = "jpg";
  212. }
  213. filename += "." + extension.ToLower();
  214. string path = null;
  215. if (saveLocally)
  216. {
  217. if (!(item is Episode))
  218. {
  219. var video = item as Video;
  220. if (video != null && video.IsInMixedFolder)
  221. {
  222. var folder = Path.GetDirectoryName(video.Path);
  223. path = Path.Combine(folder, Path.GetFileNameWithoutExtension(video.Path) + "-" + filename);
  224. }
  225. }
  226. if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(item.MetaLocation))
  227. {
  228. path = Path.Combine(item.MetaLocation, filename);
  229. }
  230. }
  231. if (string.IsNullOrEmpty(path))
  232. {
  233. path = _remoteImageCache.GetResourcePath(item.GetType().FullName + item.Id, filename);
  234. }
  235. var parentPath = Path.GetDirectoryName(path);
  236. if (!Directory.Exists(parentPath))
  237. {
  238. Directory.CreateDirectory(parentPath);
  239. }
  240. return path;
  241. }
  242. }
  243. }