Does this work?
This commit is contained in:
@@ -20,13 +20,14 @@ public class ShardServerFilesController : ControllerBase
|
|||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public async Task<IActionResult> DownloadFileDirect(string hash, [FromQuery] long expires, [FromQuery] string signature)
|
public async Task<IActionResult> DownloadFileDirect(string hash, [FromQuery] long expires, [FromQuery] string signature)
|
||||||
{
|
{
|
||||||
var result = await _cdnDownloadsService.GetDownloadAsync(hash, expires, signature).ConfigureAwait(false);
|
var result = _cdnDownloadsService.GetDownloadWithCacheCheck(hash, expires, signature);
|
||||||
|
|
||||||
return result.Status switch
|
return result.Status switch
|
||||||
{
|
{
|
||||||
CDNDownloadsService.ResultStatus.Disabled => NotFound(),
|
CDNDownloadsService.ResultStatus.Disabled => NotFound(),
|
||||||
CDNDownloadsService.ResultStatus.Unauthorized => Unauthorized(),
|
CDNDownloadsService.ResultStatus.Unauthorized => Unauthorized(),
|
||||||
CDNDownloadsService.ResultStatus.NotFound => NotFound(),
|
CDNDownloadsService.ResultStatus.NotFound => NotFound(),
|
||||||
|
CDNDownloadsService.ResultStatus.Downloading => StatusCode(503),
|
||||||
CDNDownloadsService.ResultStatus.Success => PhysicalFile(result.File!.FullName, "application/octet-stream"),
|
CDNDownloadsService.ResultStatus.Success => PhysicalFile(result.File!.FullName, "application/octet-stream"),
|
||||||
_ => NotFound()
|
_ => NotFound()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ public class CDNDownloadsService
|
|||||||
Disabled,
|
Disabled,
|
||||||
Unauthorized,
|
Unauthorized,
|
||||||
NotFound,
|
NotFound,
|
||||||
Success
|
Success,
|
||||||
|
Downloading
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly record struct Result(ResultStatus Status, FileInfo? File);
|
public readonly record struct Result(ResultStatus Status, FileInfo? File);
|
||||||
@@ -53,4 +54,32 @@ public class CDNDownloadsService
|
|||||||
|
|
||||||
return new Result(ResultStatus.Success, fileInfo);
|
return new Result(ResultStatus.Success, fileInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Result GetDownloadWithCacheCheck(string hash, long expiresUnixSeconds, string signature)
|
||||||
|
{
|
||||||
|
if (!_cdnDownloadUrlService.DirectDownloadsEnabled)
|
||||||
|
{
|
||||||
|
return new Result(ResultStatus.Disabled, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(hash))
|
||||||
|
{
|
||||||
|
return new Result(ResultStatus.Unauthorized, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
hash = hash.ToUpperInvariant();
|
||||||
|
|
||||||
|
if (!_cdnDownloadUrlService.TryValidateSignature(hash, expiresUnixSeconds, signature))
|
||||||
|
{
|
||||||
|
return new Result(ResultStatus.Unauthorized, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileInfo = _cachedFileProvider.TryGetLocalFileInfo(hash);
|
||||||
|
if (fileInfo == null)
|
||||||
|
{
|
||||||
|
return new Result(ResultStatus.Downloading, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result(ResultStatus.Success, fileInfo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,4 +219,27 @@ public sealed class CachedFileProvider : IDisposable
|
|||||||
{
|
{
|
||||||
return hashes.Exists(_currentTransfers.Keys.Contains);
|
return hashes.Exists(_currentTransfers.Keys.Contains);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FileInfo? TryGetLocalFileInfo(string hash)
|
||||||
|
{
|
||||||
|
var fi = FilePathUtil.GetFileInfoForHash(_hotStoragePath, hash);
|
||||||
|
if (fi != null)
|
||||||
|
{
|
||||||
|
return GetLocalFilePath(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DownloadFileWhenRequired(hash).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Download failed for {hash}", hash);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user