| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Net.Mime; |
| | | 7 | | using System.Reflection.Metadata.Ecma335; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using AsyncKeyedLock; |
| | | 12 | | using Jellyfin.Database.Implementations.Entities; |
| | | 13 | | using MediaBrowser.Common.Extensions; |
| | | 14 | | using MediaBrowser.Controller; |
| | | 15 | | using MediaBrowser.Controller.Configuration; |
| | | 16 | | using MediaBrowser.Controller.Drawing; |
| | | 17 | | using MediaBrowser.Controller.Entities; |
| | | 18 | | using MediaBrowser.Model.Drawing; |
| | | 19 | | using MediaBrowser.Model.Dto; |
| | | 20 | | using MediaBrowser.Model.Entities; |
| | | 21 | | using MediaBrowser.Model.IO; |
| | | 22 | | using MediaBrowser.Model.Net; |
| | | 23 | | using Microsoft.Extensions.Logging; |
| | | 24 | | using Photo = MediaBrowser.Controller.Entities.Photo; |
| | | 25 | | |
| | | 26 | | namespace Jellyfin.Drawing; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Class ImageProcessor. |
| | | 30 | | /// </summary> |
| | | 31 | | public sealed class ImageProcessor : IImageProcessor, IDisposable |
| | | 32 | | { |
| | | 33 | | // Increment this when there's a change requiring caches to be invalidated |
| | | 34 | | private const char Version = '3'; |
| | | 35 | | |
| | 0 | 36 | | private static readonly HashSet<string> _transparentImageTypes |
| | 0 | 37 | | = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".webp", ".gif", ".svg" }; |
| | | 38 | | |
| | | 39 | | private readonly ILogger<ImageProcessor> _logger; |
| | | 40 | | private readonly IFileSystem _fileSystem; |
| | | 41 | | private readonly IServerApplicationPaths _appPaths; |
| | | 42 | | private readonly IImageEncoder _imageEncoder; |
| | | 43 | | |
| | | 44 | | private readonly AsyncNonKeyedLocker _parallelEncodingLimit; |
| | | 45 | | |
| | | 46 | | private bool _disposed; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="ImageProcessor"/> class. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="logger">The logger.</param> |
| | | 52 | | /// <param name="appPaths">The server application paths.</param> |
| | | 53 | | /// <param name="fileSystem">The filesystem.</param> |
| | | 54 | | /// <param name="imageEncoder">The image encoder.</param> |
| | | 55 | | /// <param name="config">The configuration.</param> |
| | | 56 | | public ImageProcessor( |
| | | 57 | | ILogger<ImageProcessor> logger, |
| | | 58 | | IServerApplicationPaths appPaths, |
| | | 59 | | IFileSystem fileSystem, |
| | | 60 | | IImageEncoder imageEncoder, |
| | | 61 | | IServerConfigurationManager config) |
| | | 62 | | { |
| | 21 | 63 | | _logger = logger; |
| | 21 | 64 | | _fileSystem = fileSystem; |
| | 21 | 65 | | _imageEncoder = imageEncoder; |
| | 21 | 66 | | _appPaths = appPaths; |
| | | 67 | | |
| | 21 | 68 | | var semaphoreCount = config.Configuration.ParallelImageEncodingLimit; |
| | 21 | 69 | | if (semaphoreCount < 1) |
| | | 70 | | { |
| | 21 | 71 | | semaphoreCount = Environment.ProcessorCount; |
| | | 72 | | } |
| | | 73 | | |
| | 21 | 74 | | _parallelEncodingLimit = new(semaphoreCount); |
| | 21 | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | private string ResizedImageCachePath => Path.Combine(_appPaths.ImageCachePath, "resized-images"); |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public IReadOnlyCollection<string> SupportedInputFormats => |
| | 0 | 81 | | new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| | 0 | 82 | | { |
| | 0 | 83 | | "tiff", |
| | 0 | 84 | | "tif", |
| | 0 | 85 | | "jpeg", |
| | 0 | 86 | | "jpg", |
| | 0 | 87 | | "png", |
| | 0 | 88 | | "aiff", |
| | 0 | 89 | | "cr2", |
| | 0 | 90 | | "crw", |
| | 0 | 91 | | "nef", |
| | 0 | 92 | | "orf", |
| | 0 | 93 | | "pef", |
| | 0 | 94 | | "arw", |
| | 0 | 95 | | "webp", |
| | 0 | 96 | | "gif", |
| | 0 | 97 | | "bmp", |
| | 0 | 98 | | "erf", |
| | 0 | 99 | | "raf", |
| | 0 | 100 | | "rw2", |
| | 0 | 101 | | "nrw", |
| | 0 | 102 | | "dng", |
| | 0 | 103 | | "ico", |
| | 0 | 104 | | "astc", |
| | 0 | 105 | | "ktx", |
| | 0 | 106 | | "pkm", |
| | 0 | 107 | | "wbmp", |
| | 0 | 108 | | "avif" |
| | 0 | 109 | | }; |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | 0 | 112 | | public bool SupportsImageCollageCreation => _imageEncoder.SupportsImageCollageCreation; |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | | 115 | | public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats() |
| | 0 | 116 | | => _imageEncoder.SupportedOutputFormats; |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public async Task<(string Path, string? MimeType, DateTime DateModified)> ProcessImage(ImageProcessingOptions option |
| | | 120 | | { |
| | | 121 | | ItemImageInfo originalImage = options.Image; |
| | | 122 | | BaseItem item = options.Item; |
| | | 123 | | |
| | | 124 | | string originalImagePath = originalImage.Path; |
| | | 125 | | DateTime dateModified = originalImage.DateModified; |
| | | 126 | | ImageDimensions? originalImageSize = null; |
| | | 127 | | if (originalImage.Width > 0 && originalImage.Height > 0) |
| | | 128 | | { |
| | | 129 | | originalImageSize = new ImageDimensions(originalImage.Width, originalImage.Height); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | var mimeType = MimeTypes.GetMimeType(originalImagePath); |
| | | 133 | | if (!_imageEncoder.SupportsImageEncoding) |
| | | 134 | | { |
| | | 135 | | return (originalImagePath, mimeType, dateModified); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | var supportedImageInfo = await GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false); |
| | | 139 | | originalImagePath = supportedImageInfo.Path; |
| | | 140 | | |
| | | 141 | | // Original file doesn't exist, or original file is gif. |
| | | 142 | | if (!File.Exists(originalImagePath) || string.Equals(mimeType, MediaTypeNames.Image.Gif, StringComparison.Ordina |
| | | 143 | | { |
| | | 144 | | return (originalImagePath, mimeType, dateModified); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | dateModified = supportedImageInfo.DateModified; |
| | | 148 | | bool requiresTransparency = _transparentImageTypes.Contains(Path.GetExtension(originalImagePath)); |
| | | 149 | | |
| | | 150 | | bool autoOrient = false; |
| | | 151 | | ImageOrientation? orientation = null; |
| | | 152 | | if (item is Photo photo) |
| | | 153 | | { |
| | | 154 | | if (photo.Orientation.HasValue) |
| | | 155 | | { |
| | | 156 | | if (photo.Orientation.Value != ImageOrientation.TopLeft) |
| | | 157 | | { |
| | | 158 | | autoOrient = true; |
| | | 159 | | orientation = photo.Orientation; |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | else |
| | | 163 | | { |
| | | 164 | | // Orientation unknown, so do it |
| | | 165 | | autoOrient = true; |
| | | 166 | | orientation = photo.Orientation; |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | if (options.HasDefaultOptions(originalImagePath, originalImageSize) && (!autoOrient || !options.RequiresAutoOrie |
| | | 171 | | { |
| | | 172 | | // Just spit out the original file if all the options are default |
| | | 173 | | return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | int quality = options.Quality; |
| | | 177 | | |
| | | 178 | | ImageFormat outputFormat = GetOutputFormat(options.SupportedOutputFormats, requiresTransparency); |
| | | 179 | | string cacheFilePath = GetCacheFilePath( |
| | | 180 | | originalImagePath, |
| | | 181 | | options.Width, |
| | | 182 | | options.Height, |
| | | 183 | | options.MaxWidth, |
| | | 184 | | options.MaxHeight, |
| | | 185 | | options.FillWidth, |
| | | 186 | | options.FillHeight, |
| | | 187 | | quality, |
| | | 188 | | dateModified, |
| | | 189 | | outputFormat, |
| | | 190 | | options.PercentPlayed, |
| | | 191 | | options.UnplayedCount, |
| | | 192 | | options.Blur, |
| | | 193 | | options.BackgroundColor, |
| | | 194 | | options.ForegroundLayer); |
| | | 195 | | |
| | | 196 | | try |
| | | 197 | | { |
| | | 198 | | if (!File.Exists(cacheFilePath)) |
| | | 199 | | { |
| | | 200 | | string resultPath; |
| | | 201 | | |
| | | 202 | | // Limit number of parallel (more precisely: concurrent) image encodings to prevent a high memory usage |
| | | 203 | | using (await _parallelEncodingLimit.LockAsync().ConfigureAwait(false)) |
| | | 204 | | { |
| | | 205 | | resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, o |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase)) |
| | | 209 | | { |
| | | 210 | | return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | return (cacheFilePath, outputFormat.GetMimeType(), _fileSystem.GetLastWriteTimeUtc(cacheFilePath)); |
| | | 215 | | } |
| | | 216 | | catch (Exception ex) |
| | | 217 | | { |
| | | 218 | | // If it fails for whatever reason, return the original image |
| | | 219 | | _logger.LogError(ex, "Error encoding image"); |
| | | 220 | | return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | private ImageFormat GetOutputFormat(IReadOnlyCollection<ImageFormat> clientSupportedFormats, bool requiresTransparen |
| | | 225 | | { |
| | 0 | 226 | | var serverFormats = GetSupportedImageOutputFormats(); |
| | | 227 | | |
| | | 228 | | // Client doesn't care about format, so start with webp if supported |
| | 0 | 229 | | if (serverFormats.Contains(ImageFormat.Webp) && clientSupportedFormats.Contains(ImageFormat.Webp)) |
| | | 230 | | { |
| | 0 | 231 | | return ImageFormat.Webp; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | // If transparency is needed and webp isn't supported, than png is the only option |
| | 0 | 235 | | if (requiresTransparency && clientSupportedFormats.Contains(ImageFormat.Png)) |
| | | 236 | | { |
| | 0 | 237 | | return ImageFormat.Png; |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | foreach (var format in clientSupportedFormats) |
| | | 241 | | { |
| | 0 | 242 | | if (serverFormats.Contains(format)) |
| | | 243 | | { |
| | 0 | 244 | | return format; |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | // We should never actually get here |
| | 0 | 249 | | return ImageFormat.Jpg; |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Gets the cache file path based on a set of parameters. |
| | | 254 | | /// </summary> |
| | | 255 | | private string GetCacheFilePath( |
| | | 256 | | string originalPath, |
| | | 257 | | int? width, |
| | | 258 | | int? height, |
| | | 259 | | int? maxWidth, |
| | | 260 | | int? maxHeight, |
| | | 261 | | int? fillWidth, |
| | | 262 | | int? fillHeight, |
| | | 263 | | int quality, |
| | | 264 | | DateTime dateModified, |
| | | 265 | | ImageFormat format, |
| | | 266 | | double percentPlayed, |
| | | 267 | | int? unwatchedCount, |
| | | 268 | | int? blur, |
| | | 269 | | string backgroundColor, |
| | | 270 | | string foregroundLayer) |
| | | 271 | | { |
| | 0 | 272 | | var filename = new StringBuilder(256); |
| | 0 | 273 | | filename.Append(originalPath); |
| | | 274 | | |
| | 0 | 275 | | filename.Append(",quality="); |
| | 0 | 276 | | filename.Append(quality); |
| | | 277 | | |
| | 0 | 278 | | filename.Append(",datemodified="); |
| | 0 | 279 | | filename.Append(dateModified.Ticks); |
| | | 280 | | |
| | 0 | 281 | | filename.Append(",f="); |
| | 0 | 282 | | filename.Append(format); |
| | | 283 | | |
| | 0 | 284 | | if (width.HasValue) |
| | | 285 | | { |
| | 0 | 286 | | filename.Append(",width="); |
| | 0 | 287 | | filename.Append(width.Value); |
| | | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | if (height.HasValue) |
| | | 291 | | { |
| | 0 | 292 | | filename.Append(",height="); |
| | 0 | 293 | | filename.Append(height.Value); |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | if (maxWidth.HasValue) |
| | | 297 | | { |
| | 0 | 298 | | filename.Append(",maxwidth="); |
| | 0 | 299 | | filename.Append(maxWidth.Value); |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | if (maxHeight.HasValue) |
| | | 303 | | { |
| | 0 | 304 | | filename.Append(",maxheight="); |
| | 0 | 305 | | filename.Append(maxHeight.Value); |
| | | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | if (fillWidth.HasValue) |
| | | 309 | | { |
| | 0 | 310 | | filename.Append(",fillwidth="); |
| | 0 | 311 | | filename.Append(fillWidth.Value); |
| | | 312 | | } |
| | | 313 | | |
| | 0 | 314 | | if (fillHeight.HasValue) |
| | | 315 | | { |
| | 0 | 316 | | filename.Append(",fillheight="); |
| | 0 | 317 | | filename.Append(fillHeight.Value); |
| | | 318 | | } |
| | | 319 | | |
| | 0 | 320 | | if (percentPlayed > 0) |
| | | 321 | | { |
| | 0 | 322 | | filename.Append(",p="); |
| | 0 | 323 | | filename.Append(percentPlayed); |
| | | 324 | | } |
| | | 325 | | |
| | 0 | 326 | | if (unwatchedCount.HasValue) |
| | | 327 | | { |
| | 0 | 328 | | filename.Append(",p="); |
| | 0 | 329 | | filename.Append(unwatchedCount.Value); |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | if (blur.HasValue) |
| | | 333 | | { |
| | 0 | 334 | | filename.Append(",blur="); |
| | 0 | 335 | | filename.Append(blur.Value); |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | if (!string.IsNullOrEmpty(backgroundColor)) |
| | | 339 | | { |
| | 0 | 340 | | filename.Append(",b="); |
| | 0 | 341 | | filename.Append(backgroundColor); |
| | | 342 | | } |
| | | 343 | | |
| | 0 | 344 | | if (!string.IsNullOrEmpty(foregroundLayer)) |
| | | 345 | | { |
| | 0 | 346 | | filename.Append(",fl="); |
| | 0 | 347 | | filename.Append(foregroundLayer); |
| | | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | filename.Append(",v="); |
| | 0 | 351 | | filename.Append(Version); |
| | | 352 | | |
| | 0 | 353 | | return GetCachePath(ResizedImageCachePath, filename.ToString(), format.GetExtension()); |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | /// <inheritdoc /> |
| | | 357 | | public ImageDimensions GetImageDimensions(BaseItem item, ItemImageInfo info) |
| | | 358 | | { |
| | 0 | 359 | | int width = info.Width; |
| | 0 | 360 | | int height = info.Height; |
| | | 361 | | |
| | 0 | 362 | | if (height > 0 && width > 0) |
| | | 363 | | { |
| | 0 | 364 | | return new ImageDimensions(width, height); |
| | | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | string path = info.Path; |
| | 0 | 368 | | _logger.LogDebug("Getting image size for item {ItemType} {Path}", item.GetType().Name, path); |
| | | 369 | | |
| | 0 | 370 | | ImageDimensions size = GetImageDimensions(path); |
| | 0 | 371 | | info.Width = size.Width; |
| | 0 | 372 | | info.Height = size.Height; |
| | | 373 | | |
| | 0 | 374 | | return size; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <inheritdoc /> |
| | | 378 | | public ImageDimensions GetImageDimensions(string path) |
| | 0 | 379 | | => _imageEncoder.GetImageSize(path); |
| | | 380 | | |
| | | 381 | | /// <inheritdoc /> |
| | | 382 | | public string GetImageBlurHash(string path) |
| | | 383 | | { |
| | 0 | 384 | | var size = GetImageDimensions(path); |
| | 0 | 385 | | return GetImageBlurHash(path, size); |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | /// <inheritdoc /> |
| | | 389 | | public string GetImageBlurHash(string path, ImageDimensions imageDimensions) |
| | | 390 | | { |
| | 0 | 391 | | if (imageDimensions.Width <= 0 || imageDimensions.Height <= 0) |
| | | 392 | | { |
| | 0 | 393 | | return string.Empty; |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | // We want tiles to be as close to square as possible, and to *mostly* keep under 16 tiles for performance. |
| | | 397 | | // One tile is (width / xComp) x (height / yComp) pixels, which means that ideally yComp = xComp * height / widt |
| | | 398 | | // See more at https://github.com/woltapp/blurhash/#how-do-i-pick-the-number-of-x-and-y-components |
| | 0 | 399 | | float xCompF = MathF.Sqrt(16.0f * imageDimensions.Width / imageDimensions.Height); |
| | 0 | 400 | | float yCompF = xCompF * imageDimensions.Height / imageDimensions.Width; |
| | | 401 | | |
| | 0 | 402 | | int xComp = Math.Min((int)xCompF + 1, 9); |
| | 0 | 403 | | int yComp = Math.Min((int)yCompF + 1, 9); |
| | | 404 | | |
| | 0 | 405 | | return _imageEncoder.GetImageBlurHash(xComp, yComp, path); |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | /// <inheritdoc /> |
| | | 409 | | public string GetImageCacheTag(string baseItemPath, DateTime imageDateModified) |
| | 0 | 410 | | => (baseItemPath + imageDateModified.Ticks).GetMD5().ToString("N", CultureInfo.InvariantCulture); |
| | | 411 | | |
| | | 412 | | /// <inheritdoc /> |
| | | 413 | | public string GetImageCacheTag(BaseItem item, ItemImageInfo image) |
| | 0 | 414 | | => GetImageCacheTag(item.Path, image.DateModified); |
| | | 415 | | |
| | | 416 | | /// <inheritdoc /> |
| | | 417 | | public string GetImageCacheTag(BaseItemDto item, ItemImageInfo image) |
| | 0 | 418 | | => GetImageCacheTag(item.Path, image.DateModified); |
| | | 419 | | |
| | | 420 | | /// <inheritdoc /> |
| | | 421 | | public string? GetImageCacheTag(BaseItemDto item, ChapterInfo chapter) |
| | | 422 | | { |
| | 0 | 423 | | if (chapter.ImagePath is null) |
| | | 424 | | { |
| | 0 | 425 | | return null; |
| | | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | return GetImageCacheTag(item.Path, chapter.ImageDateModified); |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <inheritdoc /> |
| | | 432 | | public string? GetImageCacheTag(BaseItem item, ChapterInfo chapter) |
| | | 433 | | { |
| | 0 | 434 | | if (chapter.ImagePath is null) |
| | | 435 | | { |
| | 0 | 436 | | return null; |
| | | 437 | | } |
| | | 438 | | |
| | 0 | 439 | | return GetImageCacheTag(item, new ItemImageInfo |
| | 0 | 440 | | { |
| | 0 | 441 | | Path = chapter.ImagePath, |
| | 0 | 442 | | Type = ImageType.Chapter, |
| | 0 | 443 | | DateModified = chapter.ImageDateModified |
| | 0 | 444 | | }); |
| | | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <inheritdoc /> |
| | | 448 | | public string? GetImageCacheTag(User user) |
| | | 449 | | { |
| | 0 | 450 | | if (user.ProfileImage is null) |
| | | 451 | | { |
| | 0 | 452 | | return null; |
| | | 453 | | } |
| | | 454 | | |
| | 0 | 455 | | return GetImageCacheTag(user.ProfileImage.Path, user.ProfileImage.LastModified); |
| | | 456 | | } |
| | | 457 | | |
| | | 458 | | private Task<(string Path, DateTime DateModified)> GetSupportedImage(string originalImagePath, DateTime dateModified |
| | | 459 | | { |
| | 0 | 460 | | var inputFormat = Path.GetExtension(originalImagePath.AsSpan()).TrimStart('.').ToString(); |
| | | 461 | | |
| | | 462 | | // These are just jpg files renamed as tbn |
| | 0 | 463 | | if (string.Equals(inputFormat, "tbn", StringComparison.OrdinalIgnoreCase)) |
| | | 464 | | { |
| | 0 | 465 | | return Task.FromResult((originalImagePath, dateModified)); |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | return Task.FromResult((originalImagePath, dateModified)); |
| | | 469 | | } |
| | | 470 | | |
| | | 471 | | /// <summary> |
| | | 472 | | /// Gets the cache path. |
| | | 473 | | /// </summary> |
| | | 474 | | /// <param name="path">The path.</param> |
| | | 475 | | /// <param name="uniqueName">Name of the unique.</param> |
| | | 476 | | /// <param name="fileExtension">The file extension.</param> |
| | | 477 | | /// <returns>System.String.</returns> |
| | | 478 | | /// <exception cref="ArgumentNullException"> |
| | | 479 | | /// path |
| | | 480 | | /// or |
| | | 481 | | /// uniqueName |
| | | 482 | | /// or |
| | | 483 | | /// fileExtension. |
| | | 484 | | /// </exception> |
| | | 485 | | public string GetCachePath(string path, string uniqueName, string fileExtension) |
| | | 486 | | { |
| | 0 | 487 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | 0 | 488 | | ArgumentException.ThrowIfNullOrEmpty(uniqueName); |
| | 0 | 489 | | ArgumentException.ThrowIfNullOrEmpty(fileExtension); |
| | | 490 | | |
| | 0 | 491 | | var filename = uniqueName.GetMD5() + fileExtension; |
| | | 492 | | |
| | 0 | 493 | | return GetCachePath(path, filename); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <summary> |
| | | 497 | | /// Gets the cache path. |
| | | 498 | | /// </summary> |
| | | 499 | | /// <param name="path">The path.</param> |
| | | 500 | | /// <param name="filename">The filename.</param> |
| | | 501 | | /// <returns>System.String.</returns> |
| | | 502 | | /// <exception cref="ArgumentNullException"> |
| | | 503 | | /// path |
| | | 504 | | /// or |
| | | 505 | | /// filename. |
| | | 506 | | /// </exception> |
| | | 507 | | public string GetCachePath(ReadOnlySpan<char> path, ReadOnlySpan<char> filename) |
| | | 508 | | { |
| | 0 | 509 | | if (path.IsEmpty) |
| | | 510 | | { |
| | 0 | 511 | | throw new ArgumentException("Path can't be empty.", nameof(path)); |
| | | 512 | | } |
| | | 513 | | |
| | 0 | 514 | | if (filename.IsEmpty) |
| | | 515 | | { |
| | 0 | 516 | | throw new ArgumentException("Filename can't be empty.", nameof(filename)); |
| | | 517 | | } |
| | | 518 | | |
| | 0 | 519 | | var prefix = filename.Slice(0, 1); |
| | | 520 | | |
| | 0 | 521 | | return Path.Join(path, prefix, filename); |
| | | 522 | | } |
| | | 523 | | |
| | | 524 | | /// <inheritdoc /> |
| | | 525 | | public void CreateImageCollage(ImageCollageOptions options, string? libraryName) |
| | | 526 | | { |
| | 0 | 527 | | _logger.LogDebug("Creating image collage and saving to {Path}", options.OutputPath); |
| | | 528 | | |
| | 0 | 529 | | _imageEncoder.CreateImageCollage(options, libraryName); |
| | | 530 | | |
| | 0 | 531 | | _logger.LogDebug("Completed creation of image collage and saved to {Path}", options.OutputPath); |
| | 0 | 532 | | } |
| | | 533 | | |
| | | 534 | | /// <inheritdoc /> |
| | | 535 | | public void Dispose() |
| | | 536 | | { |
| | 21 | 537 | | if (_disposed) |
| | | 538 | | { |
| | 0 | 539 | | return; |
| | | 540 | | } |
| | | 541 | | |
| | 21 | 542 | | if (_imageEncoder is IDisposable disposable) |
| | | 543 | | { |
| | 0 | 544 | | disposable.Dispose(); |
| | | 545 | | } |
| | | 546 | | |
| | 21 | 547 | | _parallelEncodingLimit?.Dispose(); |
| | | 548 | | |
| | 21 | 549 | | _disposed = true; |
| | 21 | 550 | | } |
| | | 551 | | } |