Nullable columns

This commit is contained in:
Kieran 2022-06-13 19:07:14 +01:00
parent 5a424e4d16
commit 7ea99de160
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
5 changed files with 11 additions and 23 deletions

View File

@ -59,7 +59,7 @@ public record VoidFileMeta : IVoidFileMeta
/// <summary> /// <summary>
/// SHA-256 hash of the file /// SHA-256 hash of the file
/// </summary> /// </summary>
public string? Digest { get; init; } public string? Digest { get; set; }
/// <summary> /// <summary>
/// Url to download the file /// Url to download the file

View File

@ -43,8 +43,8 @@ public class S3FileStore : StreamFileStore, IFileStore
} }
}; };
var r = await _client.PutObjectAsync(req, cts); await _client.PutObjectAsync(req, cts);
return await HandleCompletedUpload(payload, r.ChecksumSHA256, payload.Meta.Size); return HandleCompletedUpload(payload, payload.Meta.Size);
} }
public async ValueTask Egress(EgressRequest request, Stream outStream, CancellationToken cts) public async ValueTask Egress(EgressRequest request, Stream outStream, CancellationToken cts)

View File

@ -1,5 +1,4 @@
using System.Buffers; using System.Buffers;
using System.Security.Cryptography;
using VoidCat.Model; using VoidCat.Model;
using VoidCat.Model.Exceptions; using VoidCat.Model.Exceptions;
using VoidCat.Services.Abstractions; using VoidCat.Services.Abstractions;
@ -45,16 +44,11 @@ public abstract class StreamFileStore
} }
} }
var (total, hash) = await IngressInternal(id, payload.InStream, outStream, cts); var total = await IngressInternal(id, payload.InStream, outStream, cts);
if (payload.Hash != null && !hash.Equals(payload.Hash, StringComparison.InvariantCultureIgnoreCase)) return HandleCompletedUpload(payload, total);
{
throw new CryptographicException("Invalid file hash");
}
return await HandleCompletedUpload(payload, hash, total);
} }
protected async Task<PrivateVoidFile> HandleCompletedUpload(IngressPayload payload, string hash, ulong totalSize) protected PrivateVoidFile HandleCompletedUpload(IngressPayload payload, ulong totalSize)
{ {
var meta = payload.Meta; var meta = payload.Meta;
if (payload.IsAppend) if (payload.IsAppend)
@ -68,7 +62,6 @@ public abstract class StreamFileStore
{ {
meta = meta! with meta = meta! with
{ {
Digest = hash,
Uploaded = DateTimeOffset.UtcNow, Uploaded = DateTimeOffset.UtcNow,
EditSecret = Guid.NewGuid(), EditSecret = Guid.NewGuid(),
Size = totalSize Size = totalSize
@ -84,13 +77,12 @@ public abstract class StreamFileStore
return vf; return vf;
} }
private async Task<(ulong, string)> IngressInternal(Guid id, Stream ingress, Stream outStream, private async Task<ulong> IngressInternal(Guid id, Stream ingress, Stream outStream,
CancellationToken cts) CancellationToken cts)
{ {
using var buffer = MemoryPool<byte>.Shared.Rent(BufferSize); using var buffer = MemoryPool<byte>.Shared.Rent(BufferSize);
var total = 0UL; var total = 0UL;
int readLength = 0, offset = 0; int readLength, offset = 0;
var sha = SHA256.Create();
while ((readLength = await ingress.ReadAsync(buffer.Memory[offset..], cts)) > 0 || offset != 0) while ((readLength = await ingress.ReadAsync(buffer.Memory[offset..], cts)) > 0 || offset != 0)
{ {
if (readLength != 0 && offset + readLength < buffer.Memory.Length) if (readLength != 0 && offset + readLength < buffer.Memory.Length)
@ -104,13 +96,11 @@ public abstract class StreamFileStore
var buf = buffer.Memory[..totalRead]; var buf = buffer.Memory[..totalRead];
await outStream.WriteAsync(buf, cts); await outStream.WriteAsync(buf, cts);
await _stats.TrackIngress(id, (ulong) buf.Length); await _stats.TrackIngress(id, (ulong) buf.Length);
sha.TransformBlock(buf.ToArray(), 0, buf.Length, null, 0);
total += (ulong) buf.Length; total += (ulong) buf.Length;
offset = 0; offset = 0;
} }
sha.TransformFinalBlock(Array.Empty<byte>(), 0, 0); return total;
return (total, sha.Hash!.ToHex());
} }
protected async Task EgressFull(Guid id, Stream inStream, Stream outStream, protected async Task EgressFull(Guid id, Stream inStream, Stream outStream,

View File

@ -21,12 +21,12 @@ public class Init : Migration
Create.Table("Files") Create.Table("Files")
.WithColumn("Id").AsGuid().PrimaryKey() .WithColumn("Id").AsGuid().PrimaryKey()
.WithColumn("Name").AsString() .WithColumn("Name").AsString().Nullable()
.WithColumn("Size").AsInt64() .WithColumn("Size").AsInt64()
.WithColumn("Uploaded").AsDateTimeOffset().Indexed().WithDefault(SystemMethods.CurrentUTCDateTime) .WithColumn("Uploaded").AsDateTimeOffset().Indexed().WithDefault(SystemMethods.CurrentUTCDateTime)
.WithColumn("Description").AsString().Nullable() .WithColumn("Description").AsString().Nullable()
.WithColumn("MimeType").AsString().WithDefaultValue("application/octet-stream") .WithColumn("MimeType").AsString().WithDefaultValue("application/octet-stream")
.WithColumn("Digest").AsString() .WithColumn("Digest").AsString().Nullable()
.WithColumn("EditSecret").AsGuid(); .WithColumn("EditSecret").AsGuid();
Create.Table("UserFiles") Create.Table("UserFiles")

View File

@ -157,8 +157,6 @@ public class MigrateToPostgres : IMigration
private record UploaderSecretVoidFileMeta : SecretVoidFileMeta private record UploaderSecretVoidFileMeta : SecretVoidFileMeta
{ {
public new string? Digest { get; set; }
[JsonConverter(typeof(Base58GuidConverter))] [JsonConverter(typeof(Base58GuidConverter))]
public Guid? Uploader { get; set; } public Guid? Uploader { get; set; }
} }