|
|
@@ -1,6 +1,4 @@
|
|
|
|
using LiteCharms.Features.S3.Abstractions;
|
|
|
|
using LiteCharms.Features.S3.Abstractions;
|
|
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
|
|
using static LiteCharms.Features.S3.Constants;
|
|
|
|
using static LiteCharms.Features.S3.Constants;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ShopAdmin.Components;
|
|
|
|
namespace ShopAdmin.Components;
|
|
|
@@ -16,6 +14,8 @@ public partial class CreateProduct([FromKeyedServices(BookshopBucketName)] IS3Se
|
|
|
|
|
|
|
|
|
|
|
|
private const long MaxAllowedFileSize = 1024 * 1024 * 5;
|
|
|
|
private const long MaxAllowedFileSize = 1024 * 1024 * 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Func<string, string> GetFileKeyFromUrl = url => url.Split('/').Last();
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
protected override void OnInitialized()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
base.OnInitialized();
|
|
|
|
base.OnInitialized();
|
|
|
@@ -26,15 +26,11 @@ public partial class CreateProduct([FromKeyedServices(BookshopBucketName)] IS3Se
|
|
|
|
ProductModel.Thumbnails = [.. Enumerable.Repeat(string.Empty, 5)];
|
|
|
|
ProductModel.Thumbnails = [.. Enumerable.Repeat(string.Empty, 5)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Your saving logic goes here when the ledger button is clicked
|
|
|
|
|
|
|
|
public Task HandleValidSubmit() => Task.CompletedTask;
|
|
|
|
public Task HandleValidSubmit() => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
|
|
// Checks if a valid URL asset exists at the specified position
|
|
|
|
public bool HasAssetAt(int index) => (ProductModel?.Thumbnails) != null && index < ProductModel.Thumbnails.Count &&
|
|
|
|
public bool HasAssetAt(int index) => ProductModel?.Thumbnails == null || index >= ProductModel.Thumbnails.Count
|
|
|
|
!string.IsNullOrWhiteSpace(ProductModel.Thumbnails[index]);
|
|
|
|
? false
|
|
|
|
|
|
|
|
: !string.IsNullOrWhiteSpace(ProductModel.Thumbnails[index]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handles uploading the primary image node
|
|
|
|
|
|
|
|
private async Task HandleMainImageUpload(InputFileChangeEventArgs e)
|
|
|
|
private async Task HandleMainImageUpload(InputFileChangeEventArgs e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
@@ -47,7 +43,9 @@ public partial class CreateProduct([FromKeyedServices(BookshopBucketName)] IS3Se
|
|
|
|
|
|
|
|
|
|
|
|
await file.OpenReadStream(MaxAllowedFileSize).CopyToAsync(stream, cancellationToken);
|
|
|
|
await file.OpenReadStream(MaxAllowedFileSize).CopyToAsync(stream, cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await s3Service.UploadFileAsync(file.Name,stream,
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = await s3Service.UploadFileAsync(file.Name, stream,
|
|
|
|
MimeTypes.GetMimeType(file.Name), cancellationToken);
|
|
|
|
MimeTypes.GetMimeType(file.Name), cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
if (result.IsSuccess)
|
|
|
@@ -65,20 +63,20 @@ public partial class CreateProduct([FromKeyedServices(BookshopBucketName)] IS3Se
|
|
|
|
|
|
|
|
|
|
|
|
public void SetPreviewActive(string? url)
|
|
|
|
public void SetPreviewActive(string? url)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
|
|
if (string.IsNullOrWhiteSpace(url)) return;
|
|
|
|
{
|
|
|
|
|
|
|
|
ActivePreviewUrl = url;
|
|
|
|
ActivePreviewUrl = url;
|
|
|
|
StateHasChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
StateHasChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ClosePreviewDrawer()
|
|
|
|
public void ClosePreviewDrawer()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ActivePreviewUrl = null;
|
|
|
|
ActivePreviewUrl = null;
|
|
|
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
|
|
|
StateHasChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handles uploading a thumbnail image into its specific slot index
|
|
|
|
|
|
|
|
private async Task HandleThumbnailUpload(InputFileChangeEventArgs e, int index)
|
|
|
|
private async Task HandleThumbnailUpload(InputFileChangeEventArgs e, int index)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
@@ -91,7 +89,9 @@ public partial class CreateProduct([FromKeyedServices(BookshopBucketName)] IS3Se
|
|
|
|
|
|
|
|
|
|
|
|
await file.OpenReadStream(MaxAllowedFileSize, cancellationToken).CopyToAsync(stream, cancellationToken);
|
|
|
|
await file.OpenReadStream(MaxAllowedFileSize, cancellationToken).CopyToAsync(stream, cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await s3Service.UploadFileAsync(file.Name, stream,
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = await s3Service.UploadFileAsync(file.Name, stream,
|
|
|
|
MimeTypes.GetMimeType(file.Name), cancellationToken);
|
|
|
|
MimeTypes.GetMimeType(file.Name), cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (result.IsSuccess && index < ProductModel.Thumbnails.Count)
|
|
|
|
if (result.IsSuccess && index < ProductModel.Thumbnails.Count)
|
|
|
@@ -107,25 +107,42 @@ public partial class CreateProduct([FromKeyedServices(BookshopBucketName)] IS3Se
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ClearMainImage()
|
|
|
|
public async Task ClearMainImage()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (ActivePreviewUrl == ProductModel.ImageUrl)
|
|
|
|
if (string.IsNullOrEmpty(ProductModel.ImageUrl)) return;
|
|
|
|
{
|
|
|
|
|
|
|
|
ActivePreviewUrl = null;
|
|
|
|
var targetUrl = ProductModel.ImageUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ActivePreviewUrl == targetUrl) ActivePreviewUrl = null;
|
|
|
|
|
|
|
|
|
|
|
|
ProductModel.ImageUrl = null;
|
|
|
|
ProductModel.ImageUrl = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = await s3Service.DeleteFileAsync(GetFileKeyFromUrl(targetUrl));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
|
|
|
Console.WriteLine($"[S3 Orphan Cleanup Failure]: {result.Errors[0].Message}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void RemoveThumbnailAt(int index)
|
|
|
|
public async Task RemoveThumbnailAt(int index)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (index >= 0 && index < ProductModel.Thumbnails.Count)
|
|
|
|
if (index < 0 || index >= ProductModel.Thumbnails.Count) return;
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ActivePreviewUrl == ProductModel.Thumbnails[index])
|
|
|
|
var targetUrl = ProductModel.Thumbnails[index];
|
|
|
|
{
|
|
|
|
|
|
|
|
ActivePreviewUrl = null;
|
|
|
|
if (string.IsNullOrEmpty(targetUrl)) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductModel.Thumbnails[index] = string.Empty;
|
|
|
|
if (ActivePreviewUrl == targetUrl) ActivePreviewUrl = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProductModel.Thumbnails[index] = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = await s3Service.DeleteFileAsync(GetFileKeyFromUrl(targetUrl));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (result.IsFailed)
|
|
|
|
|
|
|
|
Console.WriteLine($"[S3 Thumbnail Cleanup Failure]: {result.Errors[0].Message}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|