[backend/drive] Fix DriveService.StoreFile edge cases and make the code flow more clear/readable

This commit is contained in:
Laura Hausmann 2024-05-02 16:23:08 +02:00
parent 87ade8a97c
commit 9a662f2bea
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -152,10 +152,12 @@ public class DriveService(
string? thumbnailFilename = null; string? thumbnailFilename = null;
string? webpublicFilename = null; string? webpublicFilename = null;
if (shouldStore && isImage && isReasonableSize) if (shouldStore)
{
if (isImage && isReasonableSize)
{ {
var genWebp = user.Host == null; var genWebp = user.Host == null;
var res = await imageProcessor.ProcessImage(data, request, shouldStore, genWebp); var res = await imageProcessor.ProcessImage(data, request, true, genWebp);
blurhash = res?.Blurhash; blurhash = res?.Blurhash;
thumbnailFilename = res?.RenderThumbnail != null ? GenerateWebpFilename("thumbnail-") : null; thumbnailFilename = res?.RenderThumbnail != null ? GenerateWebpFilename("thumbnail-") : null;
@ -167,6 +169,7 @@ public class DriveService(
throw new Exception("Local storage path cannot be null"); throw new Exception("Local storage path cannot be null");
var path = Path.Combine(pathBase, filename); var path = Path.Combine(pathBase, filename);
data.Seek(0, SeekOrigin.Begin);
await using var writer = File.OpenWrite(path); await using var writer = File.OpenWrite(path);
await data.CopyToAsync(writer); await data.CopyToAsync(writer);
url = $"https://{instanceConfig.Value.WebDomain}/files/{filename}"; url = $"https://{instanceConfig.Value.WebDomain}/files/{filename}";
@ -241,6 +244,27 @@ public class DriveService(
} }
} }
else else
{
//TODO: check against file size limit
if (storedInternal)
{
var pathBase = storageConfig.Value.Local?.Path ??
throw new Exception("Local storage path cannot be null");
var path = Path.Combine(pathBase, filename);
await using var writer = File.OpenWrite(path);
await data.CopyToAsync(writer);
url = $"https://{instanceConfig.Value.WebDomain}/files/{filename}";
}
else
{
data.Seek(0, SeekOrigin.Begin);
await storageSvc.UploadFileAsync(filename, data);
url = storageSvc.GetFilePublicUrl(filename).AbsoluteUri;
}
}
}
else
{ {
url = request.Uri ?? throw new Exception("Uri must not be null at this stage"); url = request.Uri ?? throw new Exception("Uri must not be null at this stage");
} }
@ -253,7 +277,7 @@ public class DriveService(
UserHost = user.Host, UserHost = user.Host,
Sha256 = digest, Sha256 = digest,
Size = (int)data.Length, Size = (int)data.Length,
IsLink = !shouldStore && user.Host != null, IsLink = !shouldStore,
AccessKey = filename, AccessKey = filename,
IsSensitive = request.IsSensitive, IsSensitive = request.IsSensitive,
StoredInternal = storedInternal, StoredInternal = storedInternal,