optimize SendFiles
This commit is contained in:
@@ -103,36 +103,49 @@ namespace MareSynchronosServer.Hubs
|
|||||||
[HubMethodName(Api.InvokeFileSendFiles)]
|
[HubMethodName(Api.InvokeFileSendFiles)]
|
||||||
public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
|
public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
|
||||||
{
|
{
|
||||||
fileListHashes = fileListHashes.Where(f => !string.IsNullOrEmpty(f)).Distinct().ToList();
|
var userSentHashes = new HashSet<string>(fileListHashes.Distinct());
|
||||||
_logger.LogInformation($"User {AuthenticatedUserId} sending {fileListHashes.Count} files");
|
_logger.LogInformation($"User {AuthenticatedUserId} sending files: {userSentHashes.Count}");
|
||||||
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => fileListHashes.Contains(f.Hash)).ToListAsync();
|
var coveredFiles = new Dictionary<string, UploadFileDto>();
|
||||||
var filesToUpload = new List<UploadFileDto>();
|
// Todo: Check if a select can directly transform to hashset
|
||||||
filesToUpload.AddRange(forbiddenFiles.Select(f => new UploadFileDto()
|
var forbiddenFiles = await _dbContext.ForbiddenUploadEntries.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f);
|
||||||
|
var existingFiles = await _dbContext.Files.AsNoTracking().Where(f => userSentHashes.Contains(f.Hash)).ToDictionaryAsync(f => f.Hash, f => f);
|
||||||
|
var uploader = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId);
|
||||||
|
|
||||||
|
foreach (var file in userSentHashes)
|
||||||
{
|
{
|
||||||
ForbiddenBy = f.ForbiddenBy,
|
// Skip empty file hashes, duplicate file hashes, forbidden file hashes and existing file hashes
|
||||||
Hash = f.Hash,
|
if (string.IsNullOrEmpty(file)) { continue; }
|
||||||
|
if (coveredFiles.ContainsKey(file)) { continue; }
|
||||||
|
if (forbiddenFiles.ContainsKey(file))
|
||||||
|
{
|
||||||
|
coveredFiles[file] = new UploadFileDto()
|
||||||
|
{
|
||||||
|
ForbiddenBy = forbiddenFiles[file].ForbiddenBy,
|
||||||
|
Hash = file,
|
||||||
IsForbidden = true
|
IsForbidden = true
|
||||||
}));
|
};
|
||||||
fileListHashes.RemoveAll(f => filesToUpload.Any(u => u.Hash == f));
|
|
||||||
var existingFiles = await _dbContext.Files.AsNoTracking().Where(f => fileListHashes.Contains(f.Hash)).ToListAsync();
|
continue;
|
||||||
foreach (var file in fileListHashes.Where(f => existingFiles.All(e => e.Hash != f) && filesToUpload.All(u => u.Hash != f)))
|
}
|
||||||
{
|
if (existingFiles.ContainsKey(file)) { continue; }
|
||||||
|
|
||||||
_logger.LogInformation("User " + AuthenticatedUserId + " needs upload: " + file);
|
_logger.LogInformation("User " + AuthenticatedUserId + " needs upload: " + file);
|
||||||
var userId = AuthenticatedUserId;
|
var userId = AuthenticatedUserId;
|
||||||
await _dbContext.Files.AddAsync(new FileCache()
|
await _dbContext.Files.AddAsync(new FileCache()
|
||||||
{
|
{
|
||||||
Hash = file,
|
Hash = file,
|
||||||
Uploaded = false,
|
Uploaded = false,
|
||||||
Uploader = _dbContext.Users.Single(u => u.UID == userId)
|
Uploader = uploader
|
||||||
});
|
});
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
filesToUpload.Add(new UploadFileDto
|
|
||||||
{
|
|
||||||
Hash = file
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return filesToUpload;
|
coveredFiles[file] = new UploadFileDto()
|
||||||
|
{
|
||||||
|
Hash = file,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
//Save bulk
|
||||||
|
await _dbContext.SaveChangesAsync();
|
||||||
|
return coveredFiles.Values.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
|
||||||
|
|||||||
Reference in New Issue
Block a user