Merge of modifications from previous web presentation implementation

This commit is contained in:
BinaryAlley committed 2024-09-26 20:01:59 +03:00
1 parent 193861c9c5
commit 4c8517bb08
10 files changed
+2453 -39

No files matched your search

@@ -105,7 +105,7 @@ internal class FileProviderService : IFileProviderService
// check if the user has access permissions to the provided path
if (!_fileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadContents))
return Errors.Permission.UnauthorizedAccess;
return System.IO.File.ReadAllBytes(path.Path);
return File.ReadAllBytes(path.Path);
}
/// <summary>
@@ -9,6 +9,7 @@ using System;
using System.IO.Abstractions;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -43,18 +44,20 @@ public class FileTypeService : IFileTypeService
/// Determines if <paramref name="file"/> is of type image or not, and returns its type.
/// </summary>
/// <param name="file">The file to determine if it is an image or not.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing the type of image or an error.</returns>
public async Task<ErrorOr<ImageType>> GetImageTypeAsync(File file)
public async Task<ErrorOr<ImageType>> GetImageTypeAsync(File file, CancellationToken cancellationToken)
{
return await GetImageTypeAsync(file.Id).ConfigureAwait(false);
return await GetImageTypeAsync(file.Id, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Determines if a file identified by <paramref name="path"/> is of type image or not, and returns its type.
/// </summary>
/// <param name="path">The path of the file to determine if it is an image or not.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing the type of image or an error.</returns>
public async Task<ErrorOr<ImageType>> GetImageTypeAsync(FileSystemPathId path)
public async Task<ErrorOr<ImageType>> GetImageTypeAsync(FileSystemPathId path, CancellationToken cancellationToken)
{
// check if the user has access permissions to the provided path
if (!_fileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadContents))
@@ -65,7 +68,7 @@ public class FileTypeService : IFileTypeService
if (stream.Length < BUFFER_SIZE)
return ImageType.None;
// read only the first bytes of the file, equal to the buffer size
await stream.ReadAsync(buffer).ConfigureAwait(false);
await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
byte[] firstBytes = buffer[..BUFFER_SIZE].ToArray();
// check if its a known image type, based on header bytes
ImageType type = IdentifyHeader(firstBytes);
@@ -1,8 +1,9 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Enums.PhotoLibrary;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -18,14 +19,16 @@ public interface IFileTypeService
/// Determines if <paramref name="file"/> is of type image or not, and returns its type.
/// </summary>
/// <param name="file">The file to determine if it is an image or not.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing the type of image or an error.</returns>
Task<ErrorOr<ImageType>> GetImageTypeAsync(File file);
Task<ErrorOr<ImageType>> GetImageTypeAsync(File file, CancellationToken cancellationToken);
/// <summary>
/// Determines if a file identified by <paramref name="path"/> is of type image or not, and returns its type.
/// </summary>
/// <param name="path">The path of the file to determine if it is an image or not.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing the type of image or an error.</returns>
Task<ErrorOr<ImageType>> GetImageTypeAsync(FileSystemPathId path);
Task<ErrorOr<ImageType>> GetImageTypeAsync(FileSystemPathId path, CancellationToken cancellationToken);
#endregion
}
}
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -17,15 +18,17 @@ public interface IThumbnailService
/// </summary>
/// <param name="path">String representation of the file path.</param>
/// <param name="quality">The quality of the thumbnail to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of bytes representing the thumbnail of the file at the specified path or an error.</returns>
Task<ErrorOr<Thumbnail>> GetThumbnailAsync(string path, int quality);
Task<ErrorOr<Thumbnail>> GetThumbnailAsync(string path, int quality, CancellationToken cancellationToken);
/// <summary>
/// Gets the thumbnail of a file at the specified path.
/// </summary>
/// <param name="path">The path object.</param>
/// <param name="quality">The quality of the thumbnail to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of bytes representing the thumbnail of the file at the specified path or an error.</returns>
Task<ErrorOr<Thumbnail>> GetThumbnailAsync(FileSystemPathId path, int quality);
Task<ErrorOr<Thumbnail>> GetThumbnailAsync(FileSystemPathId path, int quality, CancellationToken cancellationToken);
#endregion
}
}
@@ -10,6 +10,7 @@ using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -41,13 +42,14 @@ public class ThumbnailService : IThumbnailService
/// </summary>
/// <param name="path">String representation of the file path.</param>
/// <param name="quality">The quality of the thumbnail to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of bytes representing the thumbnail of the file at the specified path or an error.</returns>
public async Task<ErrorOr<Thumbnail>> GetThumbnailAsync(string path, int quality)
public async Task<ErrorOr<Thumbnail>> GetThumbnailAsync(string path, int quality, CancellationToken cancellationToken)
{
ErrorOr<FileSystemPathId> fileSystemPathIdResult = FileSystemPathId.Create(path);
if (fileSystemPathIdResult.IsError)
return fileSystemPathIdResult.Errors;
return await GetThumbnailAsync(fileSystemPathIdResult.Value, quality);
return await GetThumbnailAsync(fileSystemPathIdResult.Value, quality, cancellationToken);
}
/// <summary>
@@ -55,11 +57,12 @@ public class ThumbnailService : IThumbnailService
/// </summary>
/// <param name="path">The path of the file for which to get the thumbnail.</param>
/// <param name="quality">The quality of the thumbnail to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of bytes representing the thumbnail of the file at the specified path or an error.</returns>
public async Task<ErrorOr<Thumbnail>> GetThumbnailAsync(FileSystemPathId path, int quality)
public async Task<ErrorOr<Thumbnail>> GetThumbnailAsync(FileSystemPathId path, int quality, CancellationToken cancellationToken)
{
// first, get the type of the image file
ErrorOr<ImageType> imageTypeResult = await _environmentContext.FileTypeService.GetImageTypeAsync(path);
ErrorOr<ImageType> imageTypeResult = await _environmentContext.FileTypeService.GetImageTypeAsync(path, cancellationToken);
if (imageTypeResult.IsError)
return imageTypeResult.Errors;
if (imageTypeResult.Value != ImageType.None)
@@ -74,7 +77,7 @@ public class ThumbnailService : IThumbnailService
if (imageTypeResult.Value is ImageType.JPEG or ImageType.JPEG_CANON or ImageType.JPEG2000
or ImageType.JPEG_UNKNOWN or ImageType.PNG or ImageType.BMP
or ImageType.WEBP or ImageType.GIF or ImageType.TIFF or ImageType.TGA)
adjustedImage = await AdjustImageResolutionAsync(fileContents, imageTypeResult.Value, quality);
adjustedImage = await AdjustImageResolutionAsync(fileContents, imageTypeResult.Value, quality, cancellationToken);
return new Thumbnail(imageTypeResult.Value, adjustedImage ?? fileContents);
}
else
@@ -87,8 +90,9 @@ public class ThumbnailService : IThumbnailService
/// <param name="imageBytes">The byte array of the source image.</param>
/// <param name="imageType">The format of the image whose quality is adjusted.</param>
/// <param name="quality">A value between 1 (lowest quality) to 100 (highest quality).</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>A byte array representing the processed image.</returns>
private static async Task<byte[]> AdjustImageQualityAsync(byte[] imageBytes, ImageType imageType, int quality)
private static async Task<byte[]> AdjustImageQualityAsync(byte[] imageBytes, ImageType imageType, int quality, CancellationToken cancellationToken)
{
using MemoryStream inputMemoryStream = new(imageBytes);
using MemoryStream outputMemoryStream = new();
@@ -99,24 +103,24 @@ public class ThumbnailService : IThumbnailService
case ImageType.JPEG_CANON:
case ImageType.JPEG_UNKNOWN:
{
using Image image = await Image.LoadAsync(inputMemoryStream);
await image.SaveAsync(outputMemoryStream, new JpegEncoder() { Quality = quality });
using Image image = await Image.LoadAsync(inputMemoryStream, cancellationToken);
await image.SaveAsync(outputMemoryStream, new JpegEncoder() { Quality = quality }, cancellationToken);
break;
}
case ImageType.PNG:
{
using Image image = await Image.LoadAsync(inputMemoryStream);
using Image image = await Image.LoadAsync(inputMemoryStream, cancellationToken);
// PNG is lossless; quality adjustments don't apply in the same way - we can adjust compression
int compressionLevel = MapRange(quality, 1, 100, 1, 9);
PngEncoder encoder = new()
{ CompressionLevel = (PngCompressionLevel)compressionLevel };
await image.SaveAsync(outputMemoryStream, encoder);
await image.SaveAsync(outputMemoryStream, encoder, cancellationToken);
break;
}
case ImageType.BMP:
{
using Image image = await Image.LoadAsync(inputMemoryStream);
await image.SaveAsync(outputMemoryStream, new PngEncoder()); // convert BMP to PNG for compression
using Image image = await Image.LoadAsync(inputMemoryStream, cancellationToken);
await image.SaveAsync(outputMemoryStream, new PngEncoder(), cancellationToken); // convert BMP to PNG for compression
break;
}
default:
@@ -131,12 +135,13 @@ public class ThumbnailService : IThumbnailService
/// <param name="imageBytes">The byte array of the source image.</param>
/// <param name="imageType">The format of the image whose quality is adjusted.</param>
/// <param name="quality">A value between 1 (lowest quality) to 100 (highest quality).</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>The bytes of the adjusted image.</returns>
private static async Task<byte[]> AdjustImageResolutionAsync(byte[] imageBytes, ImageType imageType, int quality)
private static async Task<byte[]> AdjustImageResolutionAsync(byte[] imageBytes, ImageType imageType, int quality, CancellationToken cancellationToken)
{
using MemoryStream inputMemoryStream = new(imageBytes);
using MemoryStream outputMemoryStream = new();
Image image = await Image.LoadAsync(inputMemoryStream);
Image image = await Image.LoadAsync(inputMemoryStream, cancellationToken);
double ratio = quality / 100.0;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
@@ -163,7 +168,7 @@ public class ThumbnailService : IThumbnailService
case ImageType.JPEG2000:
case ImageType.JPEG_CANON:
case ImageType.JPEG_UNKNOWN:
await image.SaveAsync(outputMemoryStream, image.Metadata.DecodedImageFormat!);
await image.SaveAsync(outputMemoryStream, image.Metadata.DecodedImageFormat!, cancellationToken);
break;
default:
return imageBytes; // for unsupported formats, just return the original bytes