Improve code clarity and test documentation
Refactored ImageProcessor to use explicit 'this.' member access for consistency and readability. Enhanced test files with XML documentation, explicit types, modern C# syntax, and clearer Moq setups. Enabled XML doc generation for test project. Updated assembly info and cache files as a result of build changes. No functional or behavioral changes.
This commit is contained in:
@@ -64,10 +64,10 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
IImageEncoder imageEncoder,
|
||||
IServerConfigurationManager config)
|
||||
{
|
||||
_logger = logger;
|
||||
_fileSystem = fileSystem;
|
||||
_imageEncoder = imageEncoder;
|
||||
_appPaths = appPaths;
|
||||
this._logger = logger;
|
||||
this._fileSystem = fileSystem;
|
||||
this._imageEncoder = imageEncoder;
|
||||
this._appPaths = appPaths;
|
||||
|
||||
var semaphoreCount = config.Configuration.ParallelImageEncodingLimit;
|
||||
if (semaphoreCount < 1)
|
||||
@@ -75,10 +75,10 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
semaphoreCount = Environment.ProcessorCount;
|
||||
}
|
||||
|
||||
_parallelEncodingLimit = new(semaphoreCount);
|
||||
this._parallelEncodingLimit = new(semaphoreCount);
|
||||
}
|
||||
|
||||
private string ResizedImageCachePath => Path.Combine(_appPaths.ImageCachePath, "resized-images");
|
||||
private string ResizedImageCachePath => Path.Combine(this._appPaths.ImageCachePath, "resized-images");
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyCollection<string> SupportedInputFormats =>
|
||||
@@ -113,11 +113,11 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsImageCollageCreation => _imageEncoder.SupportsImageCollageCreation;
|
||||
public bool SupportsImageCollageCreation => this._imageEncoder.SupportsImageCollageCreation;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats()
|
||||
=> _imageEncoder.SupportedOutputFormats;
|
||||
=> this._imageEncoder.SupportedOutputFormats;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<(string Path, string? MimeType, DateTime DateModified)> ProcessImage(ImageProcessingOptions options)
|
||||
@@ -134,12 +134,12 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
}
|
||||
|
||||
var mimeType = MimeTypes.GetMimeType(originalImagePath);
|
||||
if (!_imageEncoder.SupportsImageEncoding)
|
||||
if (!this._imageEncoder.SupportsImageEncoding)
|
||||
{
|
||||
return (originalImagePath, mimeType, dateModified);
|
||||
}
|
||||
|
||||
var supportedImageInfo = await GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false);
|
||||
var supportedImageInfo = await this.GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false);
|
||||
originalImagePath = supportedImageInfo.Path;
|
||||
|
||||
// Original file doesn't exist, or original file is gif.
|
||||
@@ -179,8 +179,8 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
|
||||
int quality = options.Quality;
|
||||
|
||||
ImageFormat outputFormat = GetOutputFormat(options.SupportedOutputFormats, requiresTransparency);
|
||||
string cacheFilePath = GetCacheFilePath(
|
||||
ImageFormat outputFormat = this.GetOutputFormat(options.SupportedOutputFormats, requiresTransparency);
|
||||
string cacheFilePath = this.GetCacheFilePath(
|
||||
originalImagePath,
|
||||
options.Width,
|
||||
options.Height,
|
||||
@@ -204,9 +204,9 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
string resultPath;
|
||||
|
||||
// Limit number of parallel (more precisely: concurrent) image encodings to prevent a high memory usage
|
||||
using (await _parallelEncodingLimit.LockAsync().ConfigureAwait(false))
|
||||
using (await this._parallelEncodingLimit.LockAsync().ConfigureAwait(false))
|
||||
{
|
||||
resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, orientation, quality, options, outputFormat);
|
||||
resultPath = this._imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, orientation, quality, options, outputFormat);
|
||||
}
|
||||
|
||||
if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))
|
||||
@@ -215,19 +215,19 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
return (cacheFilePath, outputFormat.GetMimeType(), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
|
||||
return (cacheFilePath, outputFormat.GetMimeType(), this._fileSystem.GetLastWriteTimeUtc(cacheFilePath));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// If it fails for whatever reason, return the original image
|
||||
_logger.LogError(ex, "Error encoding image");
|
||||
this._logger.LogError(ex, "Error encoding image");
|
||||
return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
|
||||
}
|
||||
}
|
||||
|
||||
private ImageFormat GetOutputFormat(IReadOnlyCollection<ImageFormat> clientSupportedFormats, bool requiresTransparency)
|
||||
{
|
||||
var serverFormats = GetSupportedImageOutputFormats();
|
||||
var serverFormats = this.GetSupportedImageOutputFormats();
|
||||
|
||||
// Client doesn't care about format, so start with webp if supported
|
||||
if (serverFormats.Contains(ImageFormat.Webp) && clientSupportedFormats.Contains(ImageFormat.Webp))
|
||||
@@ -354,7 +354,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
filename.Append(",v=");
|
||||
filename.Append(Version);
|
||||
|
||||
return GetCachePath(ResizedImageCachePath, filename.ToString(), format.GetExtension());
|
||||
return this.GetCachePath(this.ResizedImageCachePath, filename.ToString(), format.GetExtension());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -369,9 +369,9 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
}
|
||||
|
||||
string path = info.Path;
|
||||
_logger.LogDebug("Getting image size for item {ItemType} {Path}", item.GetType().Name, path);
|
||||
this._logger.LogDebug("Getting image size for item {ItemType} {Path}", item.GetType().Name, path);
|
||||
|
||||
ImageDimensions size = GetImageDimensions(path);
|
||||
ImageDimensions size = this.GetImageDimensions(path);
|
||||
info.Width = size.Width;
|
||||
info.Height = size.Height;
|
||||
|
||||
@@ -380,13 +380,13 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
public ImageDimensions GetImageDimensions(string path)
|
||||
=> _imageEncoder.GetImageSize(path);
|
||||
=> this._imageEncoder.GetImageSize(path);
|
||||
|
||||
/// <inheritdoc />
|
||||
public string GetImageBlurHash(string path)
|
||||
{
|
||||
var size = GetImageDimensions(path);
|
||||
return GetImageBlurHash(path, size);
|
||||
var size = this.GetImageDimensions(path);
|
||||
return this.GetImageBlurHash(path, size);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -406,7 +406,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
int xComp = Math.Min((int)xCompF + 1, 9);
|
||||
int yComp = Math.Min((int)yCompF + 1, 9);
|
||||
|
||||
return _imageEncoder.GetImageBlurHash(xComp, yComp, path);
|
||||
return this._imageEncoder.GetImageBlurHash(xComp, yComp, path);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -415,11 +415,11 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
|
||||
/// <inheritdoc />
|
||||
public string GetImageCacheTag(BaseItem item, ItemImageInfo image)
|
||||
=> GetImageCacheTag(item.Path, image.DateModified);
|
||||
=> this.GetImageCacheTag(item.Path, image.DateModified);
|
||||
|
||||
/// <inheritdoc />
|
||||
public string GetImageCacheTag(BaseItemDto item, ItemImageInfo image)
|
||||
=> GetImageCacheTag(item.Path, image.DateModified);
|
||||
=> this.GetImageCacheTag(item.Path, image.DateModified);
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? GetImageCacheTag(BaseItemDto item, ChapterInfo chapter)
|
||||
@@ -429,7 +429,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
return null;
|
||||
}
|
||||
|
||||
return GetImageCacheTag(item.Path, chapter.ImageDateModified);
|
||||
return this.GetImageCacheTag(item.Path, chapter.ImageDateModified);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -440,7 +440,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
return null;
|
||||
}
|
||||
|
||||
return GetImageCacheTag(item, new ItemImageInfo
|
||||
return this.GetImageCacheTag(item, new ItemImageInfo
|
||||
{
|
||||
Path = chapter.ImagePath,
|
||||
Type = ImageType.Chapter,
|
||||
@@ -456,7 +456,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
return null;
|
||||
}
|
||||
|
||||
return GetImageCacheTag(user.ProfileImage.Path, user.ProfileImage.LastModified);
|
||||
return this.GetImageCacheTag(user.ProfileImage.Path, user.ProfileImage.LastModified);
|
||||
}
|
||||
|
||||
private Task<(string Path, DateTime DateModified)> GetSupportedImage(string originalImagePath, DateTime dateModified)
|
||||
@@ -494,7 +494,7 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
|
||||
var filename = uniqueName.GetMD5() + fileExtension;
|
||||
|
||||
return GetCachePath(path, filename);
|
||||
return this.GetCachePath(path, filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -528,28 +528,28 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
||||
/// <inheritdoc />
|
||||
public void CreateImageCollage(ImageCollageOptions options, string? libraryName)
|
||||
{
|
||||
_logger.LogDebug("Creating image collage and saving to {Path}", options.OutputPath);
|
||||
this._logger.LogDebug("Creating image collage and saving to {Path}", options.OutputPath);
|
||||
|
||||
_imageEncoder.CreateImageCollage(options, libraryName);
|
||||
this._imageEncoder.CreateImageCollage(options, libraryName);
|
||||
|
||||
_logger.LogDebug("Completed creation of image collage and saved to {Path}", options.OutputPath);
|
||||
this._logger.LogDebug("Completed creation of image collage and saved to {Path}", options.OutputPath);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
if (this._disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_imageEncoder is IDisposable disposable)
|
||||
if (this._imageEncoder is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
_parallelEncodingLimit?.Dispose();
|
||||
this._parallelEncodingLimit?.Dispose();
|
||||
|
||||
_disposed = true;
|
||||
this._disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user