| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Net.Mime; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | using MediaBrowser.Common.Configuration; |
| | | 14 | | using MediaBrowser.Controller.Drawing; |
| | | 15 | | using MediaBrowser.Controller.Entities; |
| | | 16 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 17 | | using MediaBrowser.Controller.Library; |
| | | 18 | | using MediaBrowser.Controller.Playlists; |
| | | 19 | | using MediaBrowser.Controller.Providers; |
| | | 20 | | using MediaBrowser.Model.Entities; |
| | | 21 | | using MediaBrowser.Model.IO; |
| | | 22 | | using MediaBrowser.Model.Net; |
| | | 23 | | |
| | | 24 | | namespace Emby.Server.Implementations.Images |
| | | 25 | | { |
| | | 26 | | public abstract class BaseDynamicImageProvider<T> : IHasItemChangeMonitor, IForcedProvider, ICustomMetadataProvider< |
| | | 27 | | where T : BaseItem |
| | | 28 | | { |
| | | 29 | | protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths a |
| | | 30 | | { |
| | 210 | 31 | | ApplicationPaths = applicationPaths; |
| | 210 | 32 | | ProviderManager = providerManager; |
| | 210 | 33 | | FileSystem = fileSystem; |
| | 210 | 34 | | ImageProcessor = imageProcessor; |
| | 210 | 35 | | } |
| | | 36 | | |
| | | 37 | | protected IFileSystem FileSystem { get; } |
| | | 38 | | |
| | | 39 | | protected IProviderManager ProviderManager { get; } |
| | | 40 | | |
| | | 41 | | protected IApplicationPaths ApplicationPaths { get; } |
| | | 42 | | |
| | | 43 | | protected IImageProcessor ImageProcessor { get; set; } |
| | | 44 | | |
| | | 45 | | protected virtual IReadOnlyCollection<ImageType> SupportedImages { get; } |
| | 210 | 46 | | = [ImageType.Primary]; |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 0 | 49 | | public string Name => "Dynamic Image Provider"; |
| | | 50 | | |
| | 0 | 51 | | public int Order => 0; |
| | | 52 | | |
| | 0 | 53 | | protected virtual bool Supports(BaseItem item) => true; |
| | | 54 | | |
| | | 55 | | public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 56 | | { |
| | | 57 | | if (!Supports(item)) |
| | | 58 | | { |
| | | 59 | | return ItemUpdateType.None; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | var updateType = ItemUpdateType.None; |
| | | 63 | | |
| | | 64 | | if (SupportedImages.Contains(ImageType.Primary)) |
| | | 65 | | { |
| | | 66 | | var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait |
| | | 67 | | updateType |= primaryResult; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | if (SupportedImages.Contains(ImageType.Thumb)) |
| | | 71 | | { |
| | | 72 | | var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(fal |
| | | 73 | | updateType |= thumbResult; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | return updateType; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | protected Task<ItemUpdateType> FetchAsync(BaseItem item, ImageType imageType, MetadataRefreshOptions options, Ca |
| | | 80 | | { |
| | 55 | 81 | | var image = item.GetImageInfo(imageType, 0); |
| | | 82 | | |
| | 55 | 83 | | if (image is not null) |
| | | 84 | | { |
| | 0 | 85 | | if (!image.IsLocalFile) |
| | | 86 | | { |
| | 0 | 87 | | return Task.FromResult(ItemUpdateType.None); |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path)) |
| | | 91 | | { |
| | 0 | 92 | | return Task.FromResult(ItemUpdateType.None); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 55 | 96 | | var items = GetItemsWithImages(item); |
| | | 97 | | |
| | 55 | 98 | | return FetchToFileInternal(item, items, imageType, cancellationToken); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | protected async Task<ItemUpdateType> FetchToFileInternal( |
| | | 102 | | BaseItem item, |
| | | 103 | | IReadOnlyList<BaseItem> itemsWithImages, |
| | | 104 | | ImageType imageType, |
| | | 105 | | CancellationToken cancellationToken) |
| | | 106 | | { |
| | | 107 | | var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N", C |
| | | 108 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPathWithoutExtension)); |
| | | 109 | | string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0); |
| | | 110 | | |
| | | 111 | | if (string.IsNullOrEmpty(outputPath)) |
| | | 112 | | { |
| | | 113 | | return ItemUpdateType.None; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | var mimeType = MimeTypes.GetMimeType(outputPath); |
| | | 117 | | |
| | | 118 | | if (string.Equals(mimeType, MediaTypeNames.Application.Octet, StringComparison.OrdinalIgnoreCase)) |
| | | 119 | | { |
| | | 120 | | mimeType = MediaTypeNames.Image.Png; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).Confi |
| | | 124 | | |
| | | 125 | | return ItemUpdateType.ImageUpdate; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | protected abstract IReadOnlyList<BaseItem> GetItemsWithImages(BaseItem item); |
| | | 129 | | |
| | | 130 | | protected string CreateThumbCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath) |
| | | 131 | | { |
| | 0 | 132 | | return CreateCollage(primaryItem, items, outputPath, 640, 360); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | protected virtual IEnumerable<string> GetStripCollageImagePaths(BaseItem primaryItem, IEnumerable<BaseItem> item |
| | | 136 | | { |
| | 0 | 137 | | var useBackdrop = primaryItem is CollectionFolder || primaryItem is UserView; |
| | 0 | 138 | | return items |
| | 0 | 139 | | .Select(i => |
| | 0 | 140 | | { |
| | 0 | 141 | | // Use Backdrop instead of Primary image for Library images. |
| | 0 | 142 | | if (useBackdrop) |
| | 0 | 143 | | { |
| | 0 | 144 | | var backdrop = i.GetImageInfo(ImageType.Backdrop, 0); |
| | 0 | 145 | | if (backdrop is not null && backdrop.IsLocalFile) |
| | 0 | 146 | | { |
| | 0 | 147 | | return backdrop.Path; |
| | 0 | 148 | | } |
| | 0 | 149 | | } |
| | 0 | 150 | | |
| | 0 | 151 | | var image = i.GetImageInfo(ImageType.Primary, 0); |
| | 0 | 152 | | if (image is not null && image.IsLocalFile) |
| | 0 | 153 | | { |
| | 0 | 154 | | return image.Path; |
| | 0 | 155 | | } |
| | 0 | 156 | | |
| | 0 | 157 | | image = i.GetImageInfo(ImageType.Thumb, 0); |
| | 0 | 158 | | if (image is not null && image.IsLocalFile) |
| | 0 | 159 | | { |
| | 0 | 160 | | return image.Path; |
| | 0 | 161 | | } |
| | 0 | 162 | | |
| | 0 | 163 | | return null; |
| | 0 | 164 | | }) |
| | 0 | 165 | | .Where(i => !string.IsNullOrEmpty(i)); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | protected string CreatePosterCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath) |
| | | 169 | | { |
| | 0 | 170 | | return CreateCollage(primaryItem, items, outputPath, 400, 600); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | protected string CreateSquareCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath) |
| | | 174 | | { |
| | 0 | 175 | | return CreateCollage(primaryItem, items, outputPath, 600, 600); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | protected string CreateThumbCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath, int wi |
| | | 179 | | { |
| | 0 | 180 | | return CreateCollage(primaryItem, items, outputPath, width, height); |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | private string CreateCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath, int width, in |
| | | 184 | | { |
| | 0 | 185 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); |
| | | 186 | | |
| | 0 | 187 | | var options = new ImageCollageOptions |
| | 0 | 188 | | { |
| | 0 | 189 | | Height = height, |
| | 0 | 190 | | Width = width, |
| | 0 | 191 | | OutputPath = outputPath, |
| | 0 | 192 | | InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray() |
| | 0 | 193 | | }; |
| | | 194 | | |
| | 0 | 195 | | if (options.InputPaths.Count == 0) |
| | | 196 | | { |
| | 0 | 197 | | return null; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | if (!ImageProcessor.SupportsImageCollageCreation) |
| | | 201 | | { |
| | 0 | 202 | | return null; |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | ImageProcessor.CreateImageCollage(options, primaryItem.Name); |
| | 0 | 206 | | return outputPath; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | protected virtual string CreateImage( |
| | | 210 | | BaseItem item, |
| | | 211 | | IReadOnlyCollection<BaseItem> itemsWithImages, |
| | | 212 | | string outputPathWithoutExtension, |
| | | 213 | | ImageType imageType, |
| | | 214 | | int imageIndex) |
| | | 215 | | { |
| | 0 | 216 | | if (itemsWithImages.Count == 0) |
| | | 217 | | { |
| | 0 | 218 | | return null; |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png"); |
| | | 222 | | |
| | 0 | 223 | | if (imageType == ImageType.Thumb) |
| | | 224 | | { |
| | 0 | 225 | | return CreateThumbCollage(item, itemsWithImages, outputPath); |
| | | 226 | | } |
| | | 227 | | |
| | 0 | 228 | | if (imageType == ImageType.Primary) |
| | | 229 | | { |
| | 0 | 230 | | if (item is UserView |
| | 0 | 231 | | || item is Playlist |
| | 0 | 232 | | || item is MusicGenre |
| | 0 | 233 | | || item is Genre |
| | 0 | 234 | | || item is PhotoAlbum |
| | 0 | 235 | | || item is MusicArtist) |
| | | 236 | | { |
| | 0 | 237 | | return CreateSquareCollage(item, itemsWithImages, outputPath); |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | return CreatePosterCollage(item, itemsWithImages, outputPath); |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | throw new ArgumentException("Unexpected image type", nameof(imageType)); |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 247 | | { |
| | 19 | 248 | | if (!Supports(item)) |
| | | 249 | | { |
| | 0 | 250 | | return false; |
| | | 251 | | } |
| | | 252 | | |
| | 19 | 253 | | if (SupportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary)) |
| | | 254 | | { |
| | 19 | 255 | | return true; |
| | | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | if (SupportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb)) |
| | | 259 | | { |
| | 0 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | return false; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | protected bool HasChanged(BaseItem item, ImageType type) |
| | | 267 | | { |
| | 19 | 268 | | var image = item.GetImageInfo(type, 0); |
| | | 269 | | |
| | 19 | 270 | | if (image is not null) |
| | | 271 | | { |
| | 0 | 272 | | if (!image.IsLocalFile) |
| | | 273 | | { |
| | 0 | 274 | | return false; |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path)) |
| | | 278 | | { |
| | 0 | 279 | | return false; |
| | | 280 | | } |
| | | 281 | | |
| | 0 | 282 | | if (!HasChangedByDate(item, image)) |
| | | 283 | | { |
| | 0 | 284 | | return false; |
| | | 285 | | } |
| | | 286 | | } |
| | | 287 | | |
| | 19 | 288 | | return true; |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | protected virtual bool HasChangedByDate(BaseItem item, ItemImageInfo image) |
| | | 292 | | { |
| | 0 | 293 | | var path = image.Path; |
| | 0 | 294 | | if (!string.IsNullOrEmpty(path)) |
| | | 295 | | { |
| | 0 | 296 | | var modificationDate = FileSystem.GetLastWriteTimeUtc(path); |
| | 0 | 297 | | return image.DateModified != modificationDate; |
| | | 298 | | } |
| | | 299 | | |
| | 0 | 300 | | return false; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | protected string CreateSingleImage(IEnumerable<BaseItem> itemsWithImages, string outputPathWithoutExtension, Ima |
| | | 304 | | { |
| | 43 | 305 | | var image = itemsWithImages |
| | 43 | 306 | | .Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.Get |
| | 43 | 307 | | .Select(i => i.GetImagePath(imageType)) |
| | 43 | 308 | | .FirstOrDefault(); |
| | | 309 | | |
| | 43 | 310 | | if (string.IsNullOrEmpty(image)) |
| | | 311 | | { |
| | 43 | 312 | | return null; |
| | | 313 | | } |
| | | 314 | | |
| | 0 | 315 | | var ext = Path.GetExtension(image); |
| | | 316 | | |
| | 0 | 317 | | var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext); |
| | 0 | 318 | | File.Copy(image, outputPath, true); |
| | | 319 | | |
| | 0 | 320 | | return outputPath; |
| | | 321 | | } |
| | | 322 | | } |
| | | 323 | | } |