using LightlessSync.API.Routes; using LightlessSyncStaticFilesServer.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace LightlessSyncStaticFilesServer.Controllers; [Route(LightlessFiles.ServerFiles)] public class ShardServerFilesController : ControllerBase { private readonly CDNDownloadsService _cdnDownloadsService; public ShardServerFilesController(ILogger logger, CDNDownloadsService cdnDownloadsService) : base(logger) { _cdnDownloadsService = cdnDownloadsService; } [HttpGet(LightlessFiles.ServerFiles_DirectDownload + "/{hash}")] [AllowAnonymous] public async Task DownloadFileDirect(string hash, [FromQuery] long expires, [FromQuery] string signature) { var result = await _cdnDownloadsService.GetDownloadAsync(hash, expires, signature, HttpContext.RequestAborted).ConfigureAwait(false); return result.Status switch { CDNDownloadsService.ResultStatus.Disabled => NotFound(), CDNDownloadsService.ResultStatus.Unauthorized => Unauthorized(), CDNDownloadsService.ResultStatus.NotFound => NotFound(), CDNDownloadsService.ResultStatus.Success => BuildDirectDownloadResult(result), _ => NotFound() }; } private IActionResult BuildDirectDownloadResult(CDNDownloadsService.Result result) { if (result.Stream != null) { if (result.ContentLength.HasValue) { Response.ContentLength = result.ContentLength.Value; } return new FileStreamResult(result.Stream, "application/octet-stream"); } return PhysicalFile(result.File!.FullName, "application/octet-stream"); } }