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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<IActionResult> Launcher()
|
|
|
|
|
|
{
|
|
|
|
|
|
var version = UpdateService.GetCurrentVersion();
|
|
|
|
|
|
var settings = SettingService.GetSettings();
|
2024-08-04 17:53:19 -05:00
|
|
|
|
var fileName = $"LANCommander.Launcher-Windows-x64-v{version}.zip";
|
2024-07-31 18:37:51 -05:00
|
|
|
|
var path = Path.Combine(settings.Launcher.StoragePath, fileName);
|
|
|
|
|
|
|
|
|
|
|
|
if (!System.IO.File.Exists(path) || !settings.Launcher.HostUpdates)
|
|
|
|
|
|
{
|
|
|
|
|
|
var release = await UpdateService.GetRelease(version);
|
|
|
|
|
|
var asset = release.Assets.FirstOrDefault(a => a.Name == fileName);
|
|
|
|
|
|
|
|
|
|
|
|
if (asset != null)
|
|
|
|
|
|
return Redirect(asset.BrowserDownloadUrl);
|
|
|
|
|
|
else
|
|
|
|
|
|
return Redirect(release.HtmlUrl);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return File(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", fileName);
|
2023-08-15 00:05:37 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-17 20:18:17 -05:00
|
|
|
|
[Authorize(Roles = "Administrator")]
|
2023-10-18 18:17:47 -05:00
|
|
|
|
public async Task<IActionResult> Archive(Guid id)
|
2023-08-15 00:05:37 -05:00
|
|
|
|
{
|
|
|
|
|
|
var archive = await ArchiveService.Get(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (archive == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
2023-09-12 19:41:25 -05:00
|
|
|
|
var filename = Path.Combine(Settings.Archives.StoragePath, archive.ObjectKey);
|
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
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<IActionResult> Save(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var save = await GameSaveService.Get(id);
|
|
|
|
|
|
|
2024-06-27 00:54:00 -05:00
|
|
|
|
if (User == null || User.Identity?.Name != save.User?.UserName && !User.IsInRole("Administrator"))
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|