LANCommander/LANCommander.Server/Controllers/DownloadController.cs

73 lines
2.7 KiB
C#
Raw Normal View History

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
{
[Authorize]
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;
private readonly UpdateService UpdateService;
2023-08-15 00:05:37 -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;
GameSaveService = gameSaveService;
UpdateService = updateService;
}
[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
{
var archive = await ArchiveService.GetAsync(id);
2023-08-15 00:05:37 -05:00
if (archive == null)
return NotFound();
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
{
var save = await GameSaveService.GetAsync(id);
2023-12-27 17:23:33 -06: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
}
}