2025-04-11 02:31:29 -05:00
|
|
|
using AutoMapper;
|
2025-04-10 19:45:41 -05:00
|
|
|
using LANCommander.SDK.Enums;
|
2025-03-30 21:05:48 -05:00
|
|
|
using LANCommander.SDK.Models.Manifest;
|
2025-07-27 17:51:50 -05:00
|
|
|
using LANCommander.Server.ImportExport.Exceptions;
|
|
|
|
|
using LANCommander.Server.ImportExport.Models;
|
|
|
|
|
using LANCommander.Server.Services;
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-07-27 17:51:50 -05:00
|
|
|
namespace LANCommander.Server.ImportExport.Importers;
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-04-10 19:45:41 -05:00
|
|
|
public class ScriptImporter(
|
2025-04-11 02:31:29 -05:00
|
|
|
IMapper mapper,
|
2025-07-19 17:39:55 -05:00
|
|
|
ScriptService scriptService) : BaseImporter<Script, Data.Models.Script>
|
2025-03-29 01:06:24 -05:00
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<ImportItemInfo> GetImportInfoAsync(Script record)
|
2025-04-09 19:44:07 -05:00
|
|
|
{
|
|
|
|
|
return new ImportItemInfo
|
|
|
|
|
{
|
2025-08-01 02:27:06 -05:00
|
|
|
Type = ImportExportRecordType.Script,
|
2025-04-09 19:44:07 -05:00
|
|
|
Name = record.Name,
|
2025-07-19 17:39:55 -05:00
|
|
|
Size = ImportContext.Archive.Entries.FirstOrDefault(e => e.Key == $"Scripts/{record.Id}")?.Size ?? 0,
|
2025-04-09 19:44:07 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override bool CanImport(Script record) =>
|
|
|
|
|
ImportContext.DataRecord is Data.Models.Game
|
2025-04-09 19:44:07 -05:00
|
|
|
||
|
2025-07-19 17:39:55 -05:00
|
|
|
ImportContext.DataRecord is Data.Models.Redistributable
|
2025-04-09 19:44:07 -05:00
|
|
|
||
|
2025-07-19 17:39:55 -05:00
|
|
|
ImportContext.DataRecord is Data.Models.Server;
|
2025-04-09 19:44:07 -05:00
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<Data.Models.Script> AddAsync(Script record)
|
2025-03-29 01:06:24 -05:00
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
var archiveEntry = ImportContext.Archive.Entries.FirstOrDefault(e => e.Key == $"Scripts/{record.Id}");
|
2025-03-29 01:06:24 -05:00
|
|
|
|
|
|
|
|
if (archiveEntry == null)
|
|
|
|
|
throw new ImportSkippedException<Script>(record, "Matching script file does not exist in import archive");
|
|
|
|
|
|
|
|
|
|
Data.Models.Script script = null;
|
|
|
|
|
string path = "";
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var newScript = new Data.Models.Script
|
|
|
|
|
{
|
|
|
|
|
CreatedOn = record.CreatedOn,
|
|
|
|
|
Name = record.Name,
|
|
|
|
|
Description = record.Description,
|
|
|
|
|
RequiresAdmin = record.RequiresAdmin,
|
|
|
|
|
Type = record.Type,
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
if (ImportContext.DataRecord is Data.Models.Game game)
|
2025-03-29 01:06:24 -05:00
|
|
|
newScript.Game = game;
|
2025-07-19 17:39:55 -05:00
|
|
|
else if (ImportContext.DataRecord is Data.Models.Redistributable redistributable)
|
2025-03-29 01:06:24 -05:00
|
|
|
newScript.Redistributable = redistributable;
|
2025-07-19 17:39:55 -05:00
|
|
|
else if (ImportContext.DataRecord is Data.Models.Server server)
|
2025-03-29 01:06:24 -05:00
|
|
|
newScript.Server = server;
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(archiveEntry.OpenEntryStream()))
|
|
|
|
|
{
|
|
|
|
|
newScript.Contents = await streamReader.ReadToEndAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 19:44:07 -05:00
|
|
|
script = await scriptService.AddAsync(newScript);
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
return script;
|
2025-03-29 01:06:24 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (script != null)
|
2025-04-09 19:44:07 -05:00
|
|
|
await scriptService.DeleteAsync(script);
|
2025-03-29 01:06:24 -05:00
|
|
|
|
|
|
|
|
throw new ImportSkippedException<Script>(record, "An unknown error occured while importing script", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<Data.Models.Script> UpdateAsync(Script record)
|
2025-03-29 01:06:24 -05:00
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
var archiveEntry = ImportContext.Archive.Entries.FirstOrDefault(e => e.Key == $"Scripts/{record.Id}");
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-04-09 19:44:07 -05:00
|
|
|
Data.Models.Script existing = null;
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
if (ImportContext.DataRecord is Data.Models.Game game)
|
2025-04-09 19:44:07 -05:00
|
|
|
existing = await scriptService.FirstOrDefaultAsync(s => s.Type == record.Type && s.Name == record.Name && s.GameId == game.Id);
|
2025-07-19 17:39:55 -05:00
|
|
|
else if (ImportContext.DataRecord is Data.Models.Redistributable redistributable)
|
2025-04-09 19:44:07 -05:00
|
|
|
existing = await scriptService.FirstOrDefaultAsync(s => s.Type == record.Type && s.Name == record.Name && s.RedistributableId == redistributable.Id);
|
2025-07-19 17:39:55 -05:00
|
|
|
else if (ImportContext.DataRecord is Data.Models.Server server)
|
2025-04-09 19:44:07 -05:00
|
|
|
existing = await scriptService.FirstOrDefaultAsync(s => s.Type == record.Type && s.Name == record.Name && s.ServerId == server.Id);
|
2025-03-29 01:06:24 -05:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
existing.CreatedOn = record.CreatedOn;
|
|
|
|
|
existing.Name = record.Name;
|
|
|
|
|
existing.Description = record.Description;
|
|
|
|
|
existing.RequiresAdmin = record.RequiresAdmin;
|
|
|
|
|
existing.Type = record.Type;
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(archiveEntry.OpenEntryStream()))
|
|
|
|
|
{
|
|
|
|
|
existing.Contents = await streamReader.ReadToEndAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 19:44:07 -05:00
|
|
|
existing = await scriptService.UpdateAsync(existing);
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
return existing;
|
2025-03-29 01:06:24 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ImportSkippedException<Script>(record, "An unknown error occured while importing script", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
public override async Task<bool> ExistsAsync(Script record)
|
2025-03-29 01:06:24 -05:00
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
if (ImportContext.DataRecord is Data.Models.Game game)
|
2025-04-09 19:44:07 -05:00
|
|
|
return await scriptService.ExistsAsync(s => s.Type == record.Type && s.Name == record.Name && s.GameId == game.Id);
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
if (ImportContext.DataRecord is Data.Models.Redistributable redistributable)
|
2025-04-09 19:44:07 -05:00
|
|
|
return await scriptService.ExistsAsync(s => s.Type == record.Type && s.Name == record.Name && s.RedistributableId == redistributable.Id);
|
2025-03-29 01:06:24 -05:00
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
if (ImportContext.DataRecord is Data.Models.Server server)
|
2025-04-09 19:44:07 -05:00
|
|
|
return await scriptService.ExistsAsync(s => s.Type == record.Type && s.Name == record.Name && s.ServerId == server.Id);
|
|
|
|
|
|
|
|
|
|
return false;
|
2025-03-29 01:06:24 -05:00
|
|
|
}
|
|
|
|
|
}
|