using SteUp.Shared.Core.Dto; using SteUp.Shared.Core.Interface.System; namespace SteUp.Maui.Core.Services; public class AttachedService : IAttachedService { public async Task SelectImageFromCamera() { var cameraPerm = await Permissions.RequestAsync(); var storagePerm = await Permissions.RequestAsync(); if (cameraPerm != PermissionStatus.Granted || storagePerm != PermissionStatus.Granted) return null; FileResult? result; try { result = await MediaPicker.Default.CapturePhotoAsync(); } catch (Exception ex) { Console.WriteLine($"Errore cattura foto: {ex.Message}"); SentrySdk.CaptureException(ex); return null; } return result is null ? null : await ConvertToDto(result, AttachedDto.TypeAttached.Image); } public async Task SelectImageFromGallery() { var storagePerm = await Permissions.RequestAsync(); if (storagePerm != PermissionStatus.Granted) return null; FileResult? result; try { result = await MediaPicker.Default.PickPhotoAsync(); } catch (Exception ex) { Console.WriteLine($"Errore selezione galleria: {ex.Message}"); SentrySdk.CaptureException(ex); return null; } return result is null ? null : await ConvertToDto(result, AttachedDto.TypeAttached.Image); } private static async Task ConvertToDto(FileResult file, AttachedDto.TypeAttached type) { var stream = await file.OpenReadAsync(); using var ms = new MemoryStream(); await stream.CopyToAsync(ms); return new AttachedDto { Name = file.FileName, Path = file.FullPath, MimeType = file.ContentType, DimensionBytes = ms.Length, FileBytes = ms.ToArray(), Type = type }; } public async Task SaveToTempStorage(Stream file, string fileName, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(file); if (file.CanSeek) file.Position = 0; fileName = Path.GetFileName(fileName); var dir = FileSystem.CacheDirectory; var filePath = Path.Combine(dir, fileName); await using var fileStream = File.Create(filePath); await file.CopyToAsync(fileStream, ct); return filePath; } public Task OpenFile(string fileName, string filePath) { #if IOS throw new NotImplementedException(); #else return Launcher.OpenAsync(new OpenFileRequest { Title = "Apri file", File = new ReadOnlyFile(filePath) }); #endif } }