Gestiti salvataggi rest
This commit is contained in:
@@ -15,6 +15,7 @@ using SteUp.Shared.Core.Interface.IntegryApi;
|
||||
using SteUp.Shared.Core.Interface.LocalDb;
|
||||
using SteUp.Shared.Core.Interface.System;
|
||||
using SteUp.Shared.Core.Interface.System.Network;
|
||||
using SteUp.Shared.Core.Messages.Ispezione;
|
||||
using SteUp.Shared.Core.Messages.Scanner;
|
||||
using SteUp.Shared.Core.Messages.Scheda;
|
||||
using SteUp.Shared.Core.Services;
|
||||
@@ -60,6 +61,7 @@ public static class CoreModule
|
||||
builder.Services.AddSingleton<IMessenger, WeakReferenceMessenger>();
|
||||
builder.Services.AddSingleton<NewSchedaService>();
|
||||
builder.Services.AddSingleton<OnScannerService>();
|
||||
builder.Services.AddSingleton<CompleteInspectionService>();
|
||||
}
|
||||
|
||||
public void RegisterDbServices()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using SteUp.Shared.Core.Dto;
|
||||
using SteUp.Shared.Core.Dto;
|
||||
using SteUp.Shared.Core.Entities;
|
||||
using SteUp.Shared.Core.Helpers;
|
||||
using SteUp.Shared.Core.Interface.System;
|
||||
@@ -82,10 +81,10 @@ public class AttachedService : IAttachedService
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<AttachedDto> ConvertToDto(FileInfo file, AttachedDto.TypeAttached type)
|
||||
private async Task<AttachedDto> ConvertToDto(FileInfo file, AttachedDto.TypeAttached type, bool isFromToUpload)
|
||||
{
|
||||
var (origUrl, thumbUrl) = await SaveAndCreateThumbAsync(
|
||||
await File.ReadAllBytesAsync(file.FullName),
|
||||
await File.ReadAllBytesAsync(file.FullName),
|
||||
file.Name
|
||||
);
|
||||
|
||||
@@ -96,64 +95,202 @@ public class AttachedService : IAttachedService
|
||||
TempPath = origUrl,
|
||||
ThumbPath = thumbUrl,
|
||||
Type = type,
|
||||
SavedOnAppData = true
|
||||
SavedOnAppData = true,
|
||||
ToUpload = isFromToUpload
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<List<AttachedDto>?> GetInspectionFiles(Ispezione ispezione)
|
||||
private const string ToUploadFolderName = "toUpload";
|
||||
|
||||
private string GetInspectionBaseDir(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;
|
||||
return Path.Combine(baseDir, $"attached_{GetInspectionKey(ispezione)}");
|
||||
}
|
||||
|
||||
public async Task<string?> SaveInspectionFile(Ispezione ispezione, byte[] file, string fileName,
|
||||
private string GetInspectionToUploadDir(Ispezione ispezione)
|
||||
=> Path.Combine(GetInspectionBaseDir(ispezione), ToUploadFolderName);
|
||||
|
||||
private string GetInspectionFinalDir(Ispezione ispezione)
|
||||
=> GetInspectionBaseDir(ispezione);
|
||||
|
||||
/// <summary>
|
||||
/// Ritorna i file dell'ispezione filtrati per nome.
|
||||
/// Per default include sia "final" sia "toUpload" (utile per UI).
|
||||
/// </summary>
|
||||
public async Task<List<AttachedDto>?> GetInspectionFiles(
|
||||
Ispezione ispezione,
|
||||
List<string> fileNameFilter,
|
||||
bool includeToUpload,
|
||||
CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ispezione);
|
||||
ArgumentNullException.ThrowIfNull(fileNameFilter);
|
||||
|
||||
var baseDir = GetInspectionBaseDir(ispezione);
|
||||
if (!Directory.Exists(baseDir)) return null;
|
||||
|
||||
var result = new List<AttachedDto>();
|
||||
|
||||
var finalDir = GetInspectionFinalDir(ispezione);
|
||||
if (Directory.Exists(finalDir))
|
||||
{
|
||||
var finalFiles = new DirectoryInfo(finalDir)
|
||||
.GetFiles("*", SearchOption.TopDirectoryOnly);
|
||||
|
||||
foreach (var file in finalFiles)
|
||||
{
|
||||
if (file.Directory?.Name == ToUploadFolderName)
|
||||
continue;
|
||||
|
||||
if (!fileNameFilter.Contains(file.Name))
|
||||
continue;
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
result.Add(await ConvertToDto(
|
||||
file,
|
||||
AttachedDto.TypeAttached.Image,
|
||||
isFromToUpload: false));
|
||||
}
|
||||
}
|
||||
|
||||
if (!includeToUpload) return result;
|
||||
|
||||
var toUploadDir = GetInspectionToUploadDir(ispezione);
|
||||
if (!Directory.Exists(toUploadDir)) return result;
|
||||
|
||||
var toUploadFiles = new DirectoryInfo(toUploadDir)
|
||||
.GetFiles("*", SearchOption.TopDirectoryOnly);
|
||||
|
||||
foreach (var file in toUploadFiles)
|
||||
{
|
||||
if (!fileNameFilter.Contains(file.Name))
|
||||
continue;
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
result.Add(await ConvertToDto(
|
||||
file,
|
||||
AttachedDto.TypeAttached.Image,
|
||||
isFromToUpload: true));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Salva SEMPRE in /toUpload.
|
||||
/// </summary>
|
||||
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 toUploadDir = GetInspectionToUploadDir(ispezione);
|
||||
Directory.CreateDirectory(toUploadDir);
|
||||
|
||||
var filePath = Path.Combine(inspectionDir, fileName);
|
||||
var filePath = Path.Combine(toUploadDir, fileName);
|
||||
await File.WriteAllBytesAsync(filePath, file, ct);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public bool RemoveInspectionFile(Ispezione ispezione, string fileName)
|
||||
public Task<bool> MoveInspectionFileFromToUploadToFinal(
|
||||
Ispezione ispezione,
|
||||
string fileName,
|
||||
bool overwrite,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var baseDir = FileSystem.AppDataDirectory;
|
||||
var inspectionDir = Path.Combine(baseDir, $"attached_{GetInspectionKey(ispezione)}");
|
||||
ArgumentNullException.ThrowIfNull(ispezione);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(fileName);
|
||||
|
||||
if (!Directory.Exists(inspectionDir)) return false;
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
var toUploadDir = GetInspectionToUploadDir(ispezione);
|
||||
var finalDir = GetInspectionFinalDir(ispezione);
|
||||
|
||||
if (!Directory.Exists(toUploadDir)) return Task.FromResult(false);
|
||||
|
||||
var sourcePath = Path.Combine(toUploadDir, fileName);
|
||||
if (!File.Exists(sourcePath)) return Task.FromResult(false);
|
||||
|
||||
Directory.CreateDirectory(finalDir);
|
||||
|
||||
var destPath = Path.Combine(finalDir, fileName);
|
||||
|
||||
if (File.Exists(destPath))
|
||||
{
|
||||
if (!overwrite) return Task.FromResult(false);
|
||||
File.Delete(destPath);
|
||||
}
|
||||
|
||||
File.Move(sourcePath, destPath);
|
||||
|
||||
// Pulizia: se /toUpload resta vuota la elimino
|
||||
CleanupDirectoriesIfEmpty(ispezione);
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rimuove un file cercandolo prima in /toUpload e poi in final (o viceversa).
|
||||
/// Default: prova a cancellare ovunque.
|
||||
/// </summary>
|
||||
public bool RemoveInspectionFile(
|
||||
Ispezione ispezione,
|
||||
string fileName,
|
||||
bool removeAlsoFromFinal,
|
||||
bool removeAlsoFromToUpload)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ispezione);
|
||||
if (string.IsNullOrWhiteSpace(fileName)) return false;
|
||||
|
||||
var filePath = Path.Combine(inspectionDir, fileName);
|
||||
var removed = false;
|
||||
|
||||
if (!File.Exists(filePath)) return false;
|
||||
|
||||
File.Delete(filePath);
|
||||
if (removeAlsoFromToUpload)
|
||||
{
|
||||
var toUploadPath = Path.Combine(GetInspectionToUploadDir(ispezione), fileName);
|
||||
if (File.Exists(toUploadPath))
|
||||
{
|
||||
File.Delete(toUploadPath);
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Directory.EnumerateFileSystemEntries(inspectionDir).Any())
|
||||
Directory.Delete(inspectionDir);
|
||||
if (removeAlsoFromFinal)
|
||||
{
|
||||
var finalPath = Path.Combine(GetInspectionFinalDir(ispezione), fileName);
|
||||
if (File.Exists(finalPath))
|
||||
{
|
||||
File.Delete(finalPath);
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
if (removed)
|
||||
CleanupDirectoriesIfEmpty(ispezione);
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
private void CleanupDirectoriesIfEmpty(Ispezione ispezione)
|
||||
{
|
||||
var baseDir = GetInspectionBaseDir(ispezione);
|
||||
var toUploadDir = GetInspectionToUploadDir(ispezione);
|
||||
|
||||
// 1) se /toUpload esiste e vuota => delete
|
||||
if (Directory.Exists(toUploadDir) && !Directory.EnumerateFileSystemEntries(toUploadDir).Any())
|
||||
Directory.Delete(toUploadDir);
|
||||
|
||||
// 2) se base dir vuota (attenzione: dopo delete toUpload) => delete
|
||||
if (Directory.Exists(baseDir) && !Directory.EnumerateFileSystemEntries(baseDir).Any())
|
||||
Directory.Delete(baseDir);
|
||||
}
|
||||
|
||||
public async Task<string> SaveToTempStorage(Stream file, string fileName, CancellationToken ct = default)
|
||||
|
||||
@@ -123,6 +123,8 @@ public partial class HoneywellScannerService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
|
||||
service.OnScanFailed?.Invoke(ex);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user