Gestite immagini allegate
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using SteUp.Shared.Core.Dto;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using SteUp.Shared.Core.Dto;
|
||||
using SteUp.Shared.Core.Entities;
|
||||
using SteUp.Shared.Core.Helpers;
|
||||
using SteUp.Shared.Core.Interface.System;
|
||||
|
||||
@@ -80,6 +82,80 @@ public class AttachedService : IAttachedService
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<AttachedDto> ConvertToDto(FileInfo file, AttachedDto.TypeAttached type)
|
||||
{
|
||||
var (origUrl, thumbUrl) = await SaveAndCreateThumbAsync(
|
||||
await File.ReadAllBytesAsync(file.FullName),
|
||||
file.Name
|
||||
);
|
||||
|
||||
return new AttachedDto
|
||||
{
|
||||
Name = file.Name,
|
||||
Path = file.FullName,
|
||||
TempPath = origUrl,
|
||||
ThumbPath = thumbUrl,
|
||||
Type = type,
|
||||
SavedOnAppData = true
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<List<AttachedDto>?> GetInspectionFiles(Ispezione ispezione)
|
||||
{
|
||||
var baseDir = FileSystem.AppDataDirectory;
|
||||
var inspectionDir = Path.Combine(baseDir, $"attached_{GetInspectionKey(ispezione)}");
|
||||
var directory = new DirectoryInfo(inspectionDir);
|
||||
|
||||
if (!directory.Exists) return null;
|
||||
|
||||
var fileList = directory.GetFiles().ToList();
|
||||
|
||||
var returnList = new List<AttachedDto>();
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
returnList.Add(await ConvertToDto(file, AttachedDto.TypeAttached.Image));
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
|
||||
public async Task<string?> SaveInspectionFile(Ispezione ispezione, byte[] file, string fileName,
|
||||
CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ispezione);
|
||||
ArgumentNullException.ThrowIfNull(file);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(fileName);
|
||||
|
||||
var baseDir = FileSystem.AppDataDirectory;
|
||||
var inspectionDir = Path.Combine(baseDir, $"attached_{GetInspectionKey(ispezione)}");
|
||||
if (!Directory.Exists(inspectionDir)) Directory.CreateDirectory(inspectionDir);
|
||||
|
||||
var filePath = Path.Combine(inspectionDir, fileName);
|
||||
await File.WriteAllBytesAsync(filePath, file, ct);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public bool RemoveInspectionFile(Ispezione ispezione, string fileName)
|
||||
{
|
||||
var baseDir = FileSystem.AppDataDirectory;
|
||||
var inspectionDir = Path.Combine(baseDir, $"attached_{GetInspectionKey(ispezione)}");
|
||||
|
||||
if (!Directory.Exists(inspectionDir)) return false;
|
||||
if (string.IsNullOrWhiteSpace(fileName)) return false;
|
||||
|
||||
var filePath = Path.Combine(inspectionDir, fileName);
|
||||
|
||||
if (!File.Exists(filePath)) return false;
|
||||
|
||||
File.Delete(filePath);
|
||||
|
||||
if (!Directory.EnumerateFileSystemEntries(inspectionDir).Any())
|
||||
Directory.Delete(inspectionDir);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<string> SaveToTempStorage(Stream file, string fileName, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(file);
|
||||
@@ -146,4 +222,7 @@ public class AttachedService : IAttachedService
|
||||
var name = Path.GetFileName(fileName);
|
||||
return Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, '_'));
|
||||
}
|
||||
|
||||
private static string GetInspectionKey(Ispezione ispezione) =>
|
||||
$"{ispezione.CodMdep}_{ispezione.Data:ddMMyyyy}_{ispezione.Rilevatore.ToLower()}";
|
||||
}
|
||||
Reference in New Issue
Block a user