@@ -1,4 +1,5 @@
using K4os.Compression.LZ4.Legacy ;
using Blake3 ;
using K4os.Compression.LZ4.Legacy ;
using LightlessSync.API.Dto.Files ;
using LightlessSync.API.Routes ;
using LightlessSync.API.SignalR ;
@@ -208,11 +209,13 @@ public class ServerFilesController : ControllerBase
[RequestSizeLimit(200 * 1024 * 1024)]
public async Task < IActionResult > UploadFile ( string hash , CancellationToken requestAborted )
{
using var dbContext = await _lightlessDbContext . CreateDbContextAsync ( ) ;
await using var dbContext = await _lightlessDbContext . CreateDbContextAsync ( ) ;
_logger . LogInformation ( "{user}|{file}: Uploading" , LightlessUser , hash ) ;
hash = hash . ToUpperInvariant ( ) ;
if ( hash . Length = = 40 )
{
hash = hash . ToUpperInvariant ( ) ;
}
var existingFile = await dbContext . Files . SingleOrDefaultAsync ( f = > f . Hash = = hash ) ;
if ( existingFile ! = null ) return Ok ( ) ;
@@ -263,10 +266,13 @@ public class ServerFilesController : ControllerBase
[RequestSizeLimit(200 * 1024 * 1024)]
public async Task < IActionResult > UploadFileMunged ( string hash , CancellationToken requestAborted )
{
using var dbContext = await _lightlessDbContext . CreateDbContextAsync ( ) ;
await using var dbContext = await _lightlessDbContext . CreateDbContextAsync ( ) ;
_logger . LogInformation ( "{user}|{file}: Uploading munged" , LightlessUser , hash ) ;
hash = hash . ToUpperInvariant ( ) ;
if ( hash . Length = = 40 )
{
hash = hash . ToUpperInvariant ( ) ;
}
var existingFile = await dbContext . Files . SingleOrDefaultAsync ( f = > f . Hash = = hash ) ;
if ( existingFile ! = null ) return Ok ( ) ;
@@ -319,20 +325,26 @@ public class ServerFilesController : ControllerBase
private async Task StoreData ( string hash , LightlessDbContext dbContext , MemoryStream compressedFileStream )
{
var decompressedData = LZ4Wrapper . Unwrap ( compressedFileStream . ToArray ( ) ) ;
// reset streams
compressedFileStream . Seek ( 0 , SeekOrigin . Begin ) ;
// compute hash to verify
var hashString = BitConverter . ToString ( SHA1 . HashData ( decompressedData ) )
. Replace ( "-" , "" , StringComparison . Ordinal ) . ToUpperInvariant ( ) ;
if ( ! string . Equals ( hashString , hash , StringComparison . Ordinal ) )
throw new InvalidOperationException ( $"{LightlessUser}|{hash}: Hash does not match file, computed: {hashString}, expected: {hash}" ) ;
bool valid ;
// save file
var path = FilePathUtil . GetFilePath ( _basePath , hash ) ;
using var fileStream = new FileStream ( path , FileMode . Create ) ;
await compressedFileStream . CopyToAsync ( fileStream ) . ConfigureAwait ( fal se) ;
_logger . LogDebug ( "{user}|{file}: Uploaded file saved to {path}" , LightlessUser , hash , path ) ;
if ( hash . Length = = 40 )
{
var sha1Hex = Convert . ToHexString ( SHA1 . HashData ( decompressedData ) ) ;
valid = string . Equals ( sha1Hex , hash , StringComparison . OrdinalIgnoreCa se ) ;
}
else
{
var blakeHash = Hasher . Hash ( decompressedData ) ;
var blakeHex = Convert . ToHexString ( blakeHash . AsSpan ( ) ) ;
valid = string . Equals ( blakeHex , hash , StringComparison . OrdinalIgnoreCase ) ;
}
if ( ! valid )
throw new InvalidOperationException (
$"{LightlessUser}|{hash}: Hash does not match file, computed mismatch."
) ;
// update on db
await dbContext . Files . AddAsync ( new FileCache ( )