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, '_'));
|
||||
}
|
||||
}
|
||||
52
SteUp.Maui/Core/Services/ImageThumb.cs
Normal file
52
SteUp.Maui/Core/Services/ImageThumb.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SteUp.Maui.Core.Services;
|
||||
|
||||
public static class ImageThumb
|
||||
{
|
||||
public static async Task CreateThumbnailAsync(
|
||||
string inputPath,
|
||||
string outputPath,
|
||||
int maxSide = 320,
|
||||
int quality = 70,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
// Leggi bytes (meglio in async)
|
||||
var data = await File.ReadAllBytesAsync(inputPath, ct);
|
||||
|
||||
using var codec = SKCodec.Create(new SKMemoryStream(data));
|
||||
if (codec is null)
|
||||
throw new InvalidOperationException("Formato immagine non supportato o file corrotto.");
|
||||
|
||||
// Decodifica
|
||||
var info = codec.Info;
|
||||
using var bitmap = SKBitmap.Decode(codec);
|
||||
if (bitmap is null)
|
||||
throw new InvalidOperationException("Impossibile decodificare l'immagine.");
|
||||
|
||||
// Calcola resize mantenendo aspect ratio
|
||||
var w = bitmap.Width;
|
||||
var h = bitmap.Height;
|
||||
|
||||
if (w <= 0 || h <= 0) throw new InvalidOperationException("Dimensioni immagine non valide.");
|
||||
|
||||
var scale = (float)maxSide / Math.Max(w, h);
|
||||
if (scale > 1f) scale = 1f; // non ingrandire
|
||||
|
||||
var newW = Math.Max(1, (int)Math.Round(w * scale));
|
||||
var newH = Math.Max(1, (int)Math.Round(h * scale));
|
||||
|
||||
using var resized = bitmap.Resize(new SKImageInfo(newW, newH), SKFilterQuality.Medium);
|
||||
if (resized is null)
|
||||
throw new InvalidOperationException("Resize fallito.");
|
||||
|
||||
using var image = SKImage.FromBitmap(resized);
|
||||
using var encoded = image.Encode(SKEncodedImageFormat.Jpeg, quality);
|
||||
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!);
|
||||
|
||||
await using var fs = File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
encoded.SaveTo(fs);
|
||||
await fs.FlushAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
SafeAreaEdges="All"
|
||||
BackgroundColor="{DynamicResource PageBackgroundColor}">
|
||||
|
||||
<BlazorWebView HostPage="wwwroot/index.html">
|
||||
<BlazorWebView x:Name="BlazorWebView" HostPage="wwwroot/index.html">
|
||||
<BlazorWebView.RootComponents>
|
||||
<RootComponent Selector="#app" ComponentType="{x:Type shared:Components.Routes}" />
|
||||
</BlazorWebView.RootComponents>
|
||||
|
||||
@@ -1,10 +1,59 @@
|
||||
namespace SteUp.Maui
|
||||
namespace SteUp.Maui;
|
||||
|
||||
public partial class MainPage : ContentPage
|
||||
{
|
||||
public partial class MainPage : ContentPage
|
||||
private static readonly string AttachedDir =
|
||||
Path.Combine(FileSystem.CacheDirectory, "attached");
|
||||
|
||||
private const string Prefix = "https://localfiles/attached/";
|
||||
|
||||
public MainPage()
|
||||
{
|
||||
public MainPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
InitializeComponent();
|
||||
|
||||
Directory.CreateDirectory(AttachedDir);
|
||||
|
||||
BlazorWebView.WebResourceRequested += BlazorWebView_WebResourceRequested;
|
||||
}
|
||||
}
|
||||
|
||||
private static void BlazorWebView_WebResourceRequested(object? sender, WebViewWebResourceRequestedEventArgs e)
|
||||
{
|
||||
var uri = e.Uri.ToString();
|
||||
if (string.IsNullOrWhiteSpace(uri) ||
|
||||
!uri.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase))
|
||||
return;
|
||||
|
||||
var fileName = uri[Prefix.Length..];
|
||||
|
||||
fileName = fileName.Replace("\\", "/");
|
||||
if (fileName.Contains("..") || fileName.Contains('/'))
|
||||
{
|
||||
e.Handled = true;
|
||||
e.SetResponse(400, "Bad Request");
|
||||
return;
|
||||
}
|
||||
|
||||
var fullPath = Path.Combine(AttachedDir, fileName);
|
||||
|
||||
if (!File.Exists(fullPath))
|
||||
{
|
||||
e.Handled = true;
|
||||
e.SetResponse(404, "Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
e.SetResponse(200, "OK", GetContentType(fullPath), File.OpenRead(fullPath));
|
||||
}
|
||||
|
||||
private static string GetContentType(string path)
|
||||
{
|
||||
return Path.GetExtension(path).ToLowerInvariant() switch
|
||||
{
|
||||
".png" => "image/png",
|
||||
".jpg" or ".jpeg" => "image/jpeg",
|
||||
".webp" => "image/webp",
|
||||
_ => "application/octet-stream"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="it.integry.SteUp">
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/appicon"
|
||||
@@ -10,4 +10,19 @@
|
||||
<!-- Rete -->
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<!-- Fotocamera -->
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
|
||||
<!-- Storage / Media -->
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<!-- Android 10+ -->
|
||||
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
|
||||
|
||||
<!-- Android 13+ -->
|
||||
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
|
||||
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
|
||||
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
|
||||
</manifest>
|
||||
|
||||
@@ -121,6 +121,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="10.0.40" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.3" />
|
||||
<PackageReference Include="Sentry.Maui" Version="6.1.0" />
|
||||
<PackageReference Include="SkiaSharp" Version="3.119.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user