2023-12-20 17:30:27 -06:00
|
|
|
|
using AutoMapper;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Extensions;
|
2025-07-27 17:51:50 -05:00
|
|
|
|
using LANCommander.Server.ImportExport;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Models;
|
|
|
|
|
|
using LANCommander.Server.Services;
|
2023-10-24 19:11:50 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-10-24 19:11:50 -05:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Controllers.Api
|
2023-10-24 19:11:50 -05:00
|
|
|
|
{
|
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Bearer")]
|
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public class RedistributablesController : BaseApiController
|
2023-10-24 19:11:50 -05:00
|
|
|
|
{
|
2023-12-20 17:30:27 -06:00
|
|
|
|
private readonly IMapper Mapper;
|
2023-10-24 19:11:50 -05:00
|
|
|
|
private readonly RedistributableService RedistributableService;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
private readonly StorageLocationService StorageLocationService;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
private readonly ArchiveService ArchiveService;
|
2025-07-19 17:39:55 -05:00
|
|
|
|
private readonly ImportContext ImportContext;
|
2023-10-24 19:11:50 -05:00
|
|
|
|
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public RedistributablesController(
|
|
|
|
|
|
ILogger<RedistributablesController> logger,
|
|
|
|
|
|
IMapper mapper,
|
2024-10-01 17:56:58 -05:00
|
|
|
|
RedistributableService redistributableService,
|
2024-10-11 01:09:12 -05:00
|
|
|
|
StorageLocationService storageLocationService,
|
2025-07-19 17:39:55 -05:00
|
|
|
|
ArchiveService archiveService,
|
|
|
|
|
|
ImportContext importContext) : base(logger)
|
2023-10-24 19:11:50 -05:00
|
|
|
|
{
|
2023-12-20 17:30:27 -06:00
|
|
|
|
Mapper = mapper;
|
2023-10-24 19:11:50 -05:00
|
|
|
|
RedistributableService = redistributableService;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
StorageLocationService = storageLocationService;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
ArchiveService = archiveService;
|
2025-07-19 17:39:55 -05:00
|
|
|
|
ImportContext = importContext;
|
2023-10-24 19:11:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<ActionResult<IEnumerable<SDK.Models.Redistributable>>> GetAsync()
|
2023-10-24 19:11:50 -05:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return Ok(Mapper.Map<IEnumerable<SDK.Models.Redistributable>>(await RedistributableService.GetAsync()));
|
2023-10-24 19:11:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<ActionResult<SDK.Models.Redistributable>> GetAsync(Guid id)
|
2023-10-24 19:11:50 -05:00
|
|
|
|
{
|
2024-12-20 11:34:59 -06:00
|
|
|
|
var redistributable = await RedistributableService
|
|
|
|
|
|
.Include(r => r.Archives)
|
|
|
|
|
|
.Include(r => r.Scripts)
|
|
|
|
|
|
.GetAsync(id);
|
2024-09-16 20:27:54 -05:00
|
|
|
|
|
|
|
|
|
|
if (redistributable == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(Mapper.Map<SDK.Models.Redistributable>(redistributable));
|
2023-10-24 19:11:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/Download")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> DownloadAsync(Guid id)
|
2023-10-24 19:11:50 -05:00
|
|
|
|
{
|
2025-02-03 17:28:15 -06:00
|
|
|
|
var redistributable = await RedistributableService
|
|
|
|
|
|
.Include(r => r.Archives)
|
|
|
|
|
|
.GetAsync(id);
|
2023-10-24 19:11:50 -05:00
|
|
|
|
|
|
|
|
|
|
if (redistributable == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
if (redistributable.Archives == null || redistributable.Archives.Count == 0)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
var archive = redistributable.Archives.OrderByDescending(a => a.CreatedOn).First();
|
|
|
|
|
|
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var filename = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2023-10-24 19:11:50 -05:00
|
|
|
|
|
|
|
|
|
|
if (!System.IO.File.Exists(filename))
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return File(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", $"{redistributable.Name.SanitizeFilename()}.zip");
|
|
|
|
|
|
}
|
2024-10-01 17:56:06 -05:00
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
[Authorize(Roles = RoleService.AdministratorRoleName)]
|
2024-10-01 17:56:06 -05:00
|
|
|
|
[HttpPost("Import/{objectKey}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> ImportAsync(Guid objectKey)
|
2024-10-01 17:56:06 -05:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-19 17:39:55 -05:00
|
|
|
|
var uploadedPath = await ArchiveService.GetArchiveFileLocationAsync(objectKey.ToString());
|
2024-10-01 17:56:06 -05:00
|
|
|
|
|
2025-07-19 17:39:55 -05:00
|
|
|
|
var result = await ImportContext.InitializeImportAsync(uploadedPath);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
2024-10-01 17:56:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Could not import redistributable from upload");
|
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
[Authorize(Roles = RoleService.AdministratorRoleName)]
|
2024-10-01 17:56:58 -05:00
|
|
|
|
[HttpPost("UploadArchive")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> UploadArchiveAsync(SDK.Models.UploadArchiveRequest request)
|
2024-10-01 17:56:58 -05:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var storageLocation = await StorageLocationService.FirstOrDefaultAsync(l => request.StorageLocationId.HasValue ? l.Id == request.StorageLocationId.Value : l.Default);
|
|
|
|
|
|
var archive = await ArchiveService.FirstOrDefaultAsync(a => a.RedistributableId == request.Id && a.Version == request.Version);
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var archivePath = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
|
|
|
|
|
if (archive != null)
|
|
|
|
|
|
{
|
2024-10-11 01:09:12 -05:00
|
|
|
|
System.IO.File.Delete(archivePath);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
|
|
|
|
|
archive.ObjectKey = request.ObjectKey.ToString();
|
|
|
|
|
|
archive.Changelog = request.Changelog;
|
|
|
|
|
|
archive.CompressedSize = new System.IO.FileInfo(archivePath).Length;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
archive.StorageLocation = storageLocation;
|
2024-10-01 17:56:58 -05:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
archive = await ArchiveService.UpdateAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
archive = new Archive()
|
|
|
|
|
|
{
|
|
|
|
|
|
ObjectKey = request.ObjectKey.ToString(),
|
|
|
|
|
|
Changelog = request.Changelog,
|
|
|
|
|
|
RedistributableId = request.Id,
|
|
|
|
|
|
CompressedSize = new System.IO.FileInfo(archivePath).Length,
|
2024-10-11 01:09:12 -05:00
|
|
|
|
StorageLocation = storageLocation,
|
2024-10-01 17:56:58 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
await ArchiveService.AddAsync(archive);
|
2024-10-01 17:56:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Could not upload redistributable archive");
|
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-24 19:11:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|