Gestiti allegati nel form
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
using SteUp.Shared.Core.Dto;
|
||||
using SteUp.Shared.Core.Helpers;
|
||||
using SteUp.Shared.Core.Interface.System;
|
||||
|
||||
namespace SteUp.Maui.Core.Services;
|
||||
|
||||
public class AttachedService : IAttachedService
|
||||
{
|
||||
private static string AttachedRoot =>
|
||||
Path.Combine(FileSystem.CacheDirectory, "attached");
|
||||
|
||||
public async Task<AttachedDto?> SelectImageFromCamera()
|
||||
{
|
||||
var cameraPerm = await Permissions.RequestAsync<Permissions.Camera>();
|
||||
@@ -18,6 +22,7 @@ public class AttachedService : IAttachedService
|
||||
try
|
||||
{
|
||||
result = await MediaPicker.Default.CapturePhotoAsync();
|
||||
result?.FileName = $"img_{DateTime.Now:ddMMyyy_hhmmss}{result.FileName[result.FileName.IndexOf('.')..]}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -29,17 +34,16 @@ public class AttachedService : IAttachedService
|
||||
return result is null ? null : await ConvertToDto(result, AttachedDto.TypeAttached.Image);
|
||||
}
|
||||
|
||||
public async Task<AttachedDto?> SelectImageFromGallery()
|
||||
public async Task<List<AttachedDto>?> SelectImageFromGallery()
|
||||
{
|
||||
List<FileResult>? resultList;
|
||||
var storagePerm = await Permissions.RequestAsync<Permissions.StorageRead>();
|
||||
if (storagePerm != PermissionStatus.Granted)
|
||||
return null;
|
||||
|
||||
FileResult? result;
|
||||
|
||||
try
|
||||
{
|
||||
result = await MediaPicker.Default.PickPhotoAsync();
|
||||
resultList = await MediaPicker.Default.PickPhotosAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -48,7 +52,15 @@ public class AttachedService : IAttachedService
|
||||
return null;
|
||||
}
|
||||
|
||||
return result is null ? null : await ConvertToDto(result, AttachedDto.TypeAttached.Image);
|
||||
if (resultList.IsNullOrEmpty()) return null;
|
||||
|
||||
List<AttachedDto> returnList = [];
|
||||
foreach (var fileResult in resultList)
|
||||
{
|
||||
returnList.Add(await ConvertToDto(fileResult, AttachedDto.TypeAttached.Image));
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
|
||||
private static async Task<AttachedDto> ConvertToDto(FileResult file, AttachedDto.TypeAttached type)
|
||||
@@ -86,6 +98,15 @@ public class AttachedService : IAttachedService
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public Task CleanTempStorageAsync(CancellationToken ct = default)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
if (Directory.Exists(AttachedRoot))
|
||||
Directory.Delete(AttachedRoot, true);
|
||||
}, ct);
|
||||
}
|
||||
|
||||
public Task OpenFile(string fileName, string filePath)
|
||||
{
|
||||
#if IOS
|
||||
@@ -98,4 +119,31 @@ public class AttachedService : IAttachedService
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
public async Task<(string originalUrl, string thumbUrl)> SaveAndCreateThumbAsync(
|
||||
byte[] bytes, string fileName, CancellationToken ct = default)
|
||||
{
|
||||
Directory.CreateDirectory(AttachedRoot);
|
||||
|
||||
var id = Guid.NewGuid().ToString("N");
|
||||
var safeName = SanitizeFileName(fileName);
|
||||
|
||||
var originalFile = $"{id}_{safeName}";
|
||||
var thumbFile = $"{id}_thumb.jpg";
|
||||
|
||||
var originalPath = Path.Combine(AttachedRoot, originalFile);
|
||||
await File.WriteAllBytesAsync(originalPath, bytes, ct);
|
||||
|
||||
var thumbPath = Path.Combine(AttachedRoot, thumbFile);
|
||||
await ImageThumb.CreateThumbnailAsync(originalPath, thumbPath, maxSide: 320, quality: 70, ct);
|
||||
|
||||
return ($"https://localfiles/attached/{originalFile}",
|
||||
$"https://localfiles/attached/{thumbFile}");
|
||||
}
|
||||
|
||||
private static string SanitizeFileName(string fileName)
|
||||
{
|
||||
var name = Path.GetFileName(fileName);
|
||||
return Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, '_'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user