LANCommander/LANCommander.Server.Services/ImportService.cs

53 lines
1.7 KiB
C#
Raw Permalink Normal View History

using LANCommander.Server.Data.Models;
using LANCommander.Server.Services.Importers;
using Microsoft.Extensions.Logging;
using System.IO.Compression;
using LANCommander.SDK.Enums;
namespace LANCommander.Server.Services;
2025-02-16 10:08:05 -06:00
public class ImportService<T>(
ILogger<ImportService<T>> logger,
IImporter<T> importer,
StorageLocationService storageLocationService,
ArchiveService archiveService) : BaseService(logger)
2025-02-16 10:08:05 -06:00
where T : class, IBaseModel
{
2025-02-16 10:08:05 -06:00
public async Task<T> ImportFromUploadArchiveAsync(Guid objectKey)
{
var importArchive = await archiveService.FirstOrDefaultAsync(a => a.ObjectKey == objectKey.ToString());
var importArchivePath = await archiveService.GetArchiveFileLocationAsync(importArchive);
T entity;
using (var importZip = ZipFile.OpenRead(importArchivePath))
{
entity = await importer.ImportAsync(objectKey, importZip);
}
2025-02-16 10:08:05 -06:00
await archiveService.DeleteAsync(importArchive);
return entity;
}
2025-02-16 10:08:05 -06:00
public async Task<T> ImportFromLocalFileAsync(string localFilePath)
{
2025-02-16 10:08:05 -06:00
Guid objectKey = Guid.NewGuid();
var storageLocation =
await storageLocationService.FirstOrDefaultAsync(l => l.Default && l.Type == StorageLocationType.Archive);
var importArchive = await archiveService.AddAsync(new Archive
{
ObjectKey = objectKey.ToString(),
Version = DateTime.UtcNow.ToString(),
StorageLocation = storageLocation,
});
var importArchivePath = await archiveService.GetArchiveFileLocationAsync(importArchive);
2025-02-16 10:08:05 -06:00
File.Copy(localFilePath, importArchivePath, true);
return await ImportFromUploadArchiveAsync(objectKey);
}
}