Compute digest if missing

This commit is contained in:
Kieran 2022-06-13 18:49:28 +01:00
parent 7155c995c0
commit 5a424e4d16
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json; using System.Security.Cryptography;
using Newtonsoft.Json;
using VoidCat.Model; using VoidCat.Model;
using VoidCat.Services.Abstractions; using VoidCat.Services.Abstractions;
using VoidCat.Services.Files; using VoidCat.Services.Files;
@ -17,9 +18,11 @@ public class MigrateToPostgres : IMigration
private readonly IPaywallStore _paywallStore; private readonly IPaywallStore _paywallStore;
private readonly IUserStore _userStore; private readonly IUserStore _userStore;
private readonly IUserUploadsStore _userUploads; private readonly IUserUploadsStore _userUploads;
private readonly IFileStore _fileStore;
public MigrateToPostgres(VoidSettings settings, ILogger<MigrateToPostgres> logger, IFileMetadataStore fileMetadata, public MigrateToPostgres(VoidSettings settings, ILogger<MigrateToPostgres> logger, IFileMetadataStore fileMetadata,
ICache cache, IPaywallStore paywallStore, IUserStore userStore, IUserUploadsStore userUploads) ICache cache, IPaywallStore paywallStore, IUserStore userStore, IUserUploadsStore userUploads,
IFileStore fileStore)
{ {
_logger = logger; _logger = logger;
_settings = settings; _settings = settings;
@ -28,6 +31,7 @@ public class MigrateToPostgres : IMigration
_paywallStore = paywallStore; _paywallStore = paywallStore;
_userStore = userStore; _userStore = userStore;
_userUploads = userUploads; _userUploads = userUploads;
_fileStore = fileStore;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -64,12 +68,21 @@ public class MigrateToPostgres : IMigration
{ {
try try
{ {
if (string.IsNullOrEmpty(file.Digest))
{
var fs = await _fileStore.Open(new(file.Id, Enumerable.Empty<RangeRequest>()),
CancellationToken.None);
var hash = await SHA256.Create().ComputeHashAsync(fs);
file.Digest = hash.ToHex();
}
file.MimeType ??= "application/octet-stream"; file.MimeType ??= "application/octet-stream";
await _fileMetadata.Set(file.Id, file); await _fileMetadata.Set(file.Id, file);
if (file.Uploader.HasValue) if (file.Uploader.HasValue)
{ {
await _userUploads.AddFile(file.Uploader.Value, file.Id); await _userUploads.AddFile(file.Uploader.Value, file.Id);
} }
await localDiskMetaStore.Delete(file.Id); await localDiskMetaStore.Delete(file.Id);
_logger.LogInformation("Migrated file metadata for {File}", file.Id); _logger.LogInformation("Migrated file metadata for {File}", file.Id);
} }
@ -144,6 +157,8 @@ 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; }
} }