From 0d83281a601c2975b7d70bb8c9a9a73b32340c61 Mon Sep 17 00:00:00 2001 From: Kieran Date: Mon, 13 Jun 2022 16:11:58 +0100 Subject: [PATCH] Include legacy uploader format from metadata --- VoidCat/Controllers/UploadController.cs | 2 +- .../Services/Abstractions/IUserUploadsStore.cs | 4 ++-- VoidCat/Services/Files/FileStorageStartup.cs | 4 ++-- VoidCat/Services/Migrations/MigrateToPostgres.cs | 15 +++++++++++++-- ...UserUploadStore.cs => CacheUserUploadStore.cs} | 10 +++++----- VoidCat/Services/Users/PostgresUserUploadStore.cs | 4 ++-- 6 files changed, 25 insertions(+), 14 deletions(-) rename VoidCat/Services/Users/{UserUploadStore.cs => CacheUserUploadStore.cs} (83%) diff --git a/VoidCat/Controllers/UploadController.cs b/VoidCat/Controllers/UploadController.cs index ba369fb..fb87ac5 100644 --- a/VoidCat/Controllers/UploadController.cs +++ b/VoidCat/Controllers/UploadController.cs @@ -83,7 +83,7 @@ namespace VoidCat.Controllers // attach file upload to user if (uid.HasValue) { - await _userUploads.AddFile(uid!.Value, vf); + await _userUploads.AddFile(uid!.Value, vf.Id); } if (cli) diff --git a/VoidCat/Services/Abstractions/IUserUploadsStore.cs b/VoidCat/Services/Abstractions/IUserUploadsStore.cs index c1d5b4f..14af491 100644 --- a/VoidCat/Services/Abstractions/IUserUploadsStore.cs +++ b/VoidCat/Services/Abstractions/IUserUploadsStore.cs @@ -19,9 +19,9 @@ public interface IUserUploadsStore /// Assign a file upload to a user /// /// - /// + /// /// - ValueTask AddFile(Guid user, PrivateVoidFile voidFile); + ValueTask AddFile(Guid user, Guid file); /// /// Get the uploader of a single file diff --git a/VoidCat/Services/Files/FileStorageStartup.cs b/VoidCat/Services/Files/FileStorageStartup.cs index e1454a9..2a79380 100644 --- a/VoidCat/Services/Files/FileStorageStartup.cs +++ b/VoidCat/Services/Files/FileStorageStartup.cs @@ -12,7 +12,7 @@ public static class FileStorageStartup if (settings.CloudStorage != default) { - services.AddTransient(); + services.AddTransient(); // cloud storage if (settings.CloudStorage.S3 != default) @@ -29,7 +29,7 @@ public static class FileStorageStartup } else { - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); } diff --git a/VoidCat/Services/Migrations/MigrateToPostgres.cs b/VoidCat/Services/Migrations/MigrateToPostgres.cs index 68fa7d2..b3faac8 100644 --- a/VoidCat/Services/Migrations/MigrateToPostgres.cs +++ b/VoidCat/Services/Migrations/MigrateToPostgres.cs @@ -15,9 +15,10 @@ public class MigrateToPostgres : IMigration private readonly ICache _cache; private readonly IPaywallStore _paywallStore; private readonly IUserStore _userStore; + private readonly IUserUploadsStore _userUploads; public MigrateToPostgres(VoidSettings settings, ILogger logger, IFileMetadataStore fileMetadata, - ICache cache, IPaywallStore paywallStore, IUserStore userStore) + ICache cache, IPaywallStore paywallStore, IUserStore userStore, IUserUploadsStore userUploads) { _logger = logger; _settings = settings; @@ -25,6 +26,7 @@ public class MigrateToPostgres : IMigration _cache = cache; _paywallStore = paywallStore; _userStore = userStore; + _userUploads = userUploads; } /// @@ -56,13 +58,17 @@ public class MigrateToPostgres : IMigration var localDiskMetaStore = new LocalDiskFileMetadataStore(_settings); - var files = await localDiskMetaStore.ListFiles(new(0, int.MaxValue)); + var files = await localDiskMetaStore.ListFiles(new(0, int.MaxValue)); await foreach (var file in files.Results) { try { file.MimeType ??= "application/octet-stream"; await _fileMetadata.Set(file.Id, file); + if (file.Uploader.HasValue) + { + await _userUploads.AddFile(file.Uploader.Value, file.Id); + } await localDiskMetaStore.Delete(file.Id); _logger.LogInformation("Migrated file metadata for {File}", file.Id); } @@ -134,4 +140,9 @@ public class MigrateToPostgres : IMigration public string? PasswordHash { get; set; } public string? Password { get; set; } } + + private record UploaderSecretVoidFileMeta : SecretVoidFileMeta + { + public Guid? Uploader { get; set; } + } } \ No newline at end of file diff --git a/VoidCat/Services/Users/UserUploadStore.cs b/VoidCat/Services/Users/CacheUserUploadStore.cs similarity index 83% rename from VoidCat/Services/Users/UserUploadStore.cs rename to VoidCat/Services/Users/CacheUserUploadStore.cs index 41c1fbf..cfc4956 100644 --- a/VoidCat/Services/Users/UserUploadStore.cs +++ b/VoidCat/Services/Users/CacheUserUploadStore.cs @@ -4,11 +4,11 @@ using VoidCat.Services.Abstractions; namespace VoidCat.Services.Users; /// -public class UserUploadStore : IUserUploadsStore +public class CacheUserUploadStore : IUserUploadsStore { private readonly ICache _cache; - public UserUploadStore(ICache cache) + public CacheUserUploadStore(ICache cache) { _cache = cache; } @@ -43,10 +43,10 @@ public class UserUploadStore : IUserUploadsStore } /// - public async ValueTask AddFile(Guid user, PrivateVoidFile voidFile) + public async ValueTask AddFile(Guid user, Guid file) { - await _cache.AddToList(MapKey(user), voidFile.Id.ToString()); - await _cache.Set(MapUploader(voidFile.Id), user); + await _cache.AddToList(MapKey(user), file.ToString()); + await _cache.Set(MapUploader(file), user); } /// diff --git a/VoidCat/Services/Users/PostgresUserUploadStore.cs b/VoidCat/Services/Users/PostgresUserUploadStore.cs index b1478e2..420f0f5 100644 --- a/VoidCat/Services/Users/PostgresUserUploadStore.cs +++ b/VoidCat/Services/Users/PostgresUserUploadStore.cs @@ -60,12 +60,12 @@ and uf.""File"" = f.""Id"""; } /// - public async ValueTask AddFile(Guid user, PrivateVoidFile voidFile) + public async ValueTask AddFile(Guid user, Guid file) { await using var conn = await _connection.Get(); await conn.ExecuteAsync(@"insert into ""UserFiles""(""File"", ""User"") values(:file, :user)", new { - file = voidFile.Id, + file, user }); }