2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Extensions;
|
|
|
|
|
|
using LANCommander.Server.Models;
|
|
|
|
|
|
using LANCommander.Server.Services;
|
2023-08-15 00:05:37 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Controllers
|
2023-08-15 00:05:37 -05:00
|
|
|
|
{
|
2024-03-17 20:18:17 -05:00
|
|
|
|
[Authorize]
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public class DownloadController : BaseController
|
2023-08-15 00:05:37 -05:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ArchiveService ArchiveService;
|
2023-12-27 17:23:33 -06:00
|
|
|
|
private readonly GameSaveService GameSaveService;
|
2024-07-31 18:37:51 -05:00
|
|
|
|
private readonly UpdateService UpdateService;
|
2023-08-15 00:05:37 -05:00
|
|
|
|
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public DownloadController(
|
|
|
|
|
|
ILogger<DownloadController> logger,
|
|
|
|
|
|
ArchiveService archiveService,
|
|
|
|
|
|
GameSaveService gameSaveService,
|
|
|
|
|
|
UpdateService updateService) : base(logger)
|
2023-08-15 00:05:37 -05:00
|
|
|
|
{
|
|
|
|
|
|
ArchiveService = archiveService;
|
2024-03-17 20:18:17 -05:00
|
|
|
|
GameSaveService = gameSaveService;
|
2024-07-31 18:37:51 -05:00
|
|
|
|
UpdateService = updateService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
[Authorize(Roles = RoleService.AdministratorRoleName)]
|
2025-02-16 02:12:13 -06:00
|
|
|
|
[HttpGet("/Download/Archive/{id}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> ArchiveAsync(Guid id)
|
2023-08-15 00:05:37 -05:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var archive = await ArchiveService.GetAsync(id);
|
2023-08-15 00:05:37 -05:00
|
|
|
|
|
|
|
|
|
|
if (archive == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var filename = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2023-08-15 00:05:37 -05:00
|
|
|
|
|
|
|
|
|
|
if (!System.IO.File.Exists(filename))
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
2023-11-29 17:08:33 -06:00
|
|
|
|
string name = "";
|
|
|
|
|
|
|
|
|
|
|
|
if (archive.GameId != null && archive.GameId != Guid.Empty)
|
|
|
|
|
|
name = $"{archive.Game.Title.SanitizeFilename()}.zip";
|
|
|
|
|
|
else if (archive.RedistributableId != null && archive.RedistributableId != Guid.Empty)
|
|
|
|
|
|
name = $"{archive.Redistributable.Name.SanitizeFilename()}.zip";
|
|
|
|
|
|
|
|
|
|
|
|
return File(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", name);
|
2023-08-15 00:05:37 -05:00
|
|
|
|
}
|
2023-12-27 17:23:33 -06:00
|
|
|
|
|
2025-02-16 02:12:13 -06:00
|
|
|
|
[HttpGet("/Download/Save/{id}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> SaveAsync(Guid id)
|
2023-12-27 17:23:33 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var save = await GameSaveService.GetAsync(id);
|
2023-12-27 17:23:33 -06:00
|
|
|
|
|
2024-10-16 02:06:02 -05:00
|
|
|
|
if (User == null || User.Identity?.Name != save.User?.UserName && !User.IsInRole(RoleService.AdministratorRoleName))
|
2023-12-27 17:23:33 -06:00
|
|
|
|
return Unauthorized();
|
|
|
|
|
|
|
|
|
|
|
|
if (save == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
var filename = GameSaveService.GetSavePath(save);
|
|
|
|
|
|
|
|
|
|
|
|
if (!System.IO.File.Exists(filename))
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return File(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read), "application/zip", $"{save.User?.UserName} - {(save.Game == null ? "Unknown" : save.Game?.Title)} - {save.CreatedOn.ToString("MM-dd-yyyy.hh-mm")}.zip");
|
|
|
|
|
|
}
|
2023-08-15 00:05:37 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|