Include legacy uploader format from metadata

This commit is contained in:
Kieran
2022-06-13 16:11:58 +01:00
parent 863f14e25f
commit 0d83281a60
6 changed files with 25 additions and 14 deletions

View File

@@ -83,7 +83,7 @@ namespace VoidCat.Controllers
// attach file upload to user // attach file upload to user
if (uid.HasValue) if (uid.HasValue)
{ {
await _userUploads.AddFile(uid!.Value, vf); await _userUploads.AddFile(uid!.Value, vf.Id);
} }
if (cli) if (cli)

View File

@@ -19,9 +19,9 @@ public interface IUserUploadsStore
/// Assign a file upload to a user /// Assign a file upload to a user
/// </summary> /// </summary>
/// <param name="user"></param> /// <param name="user"></param>
/// <param name="voidFile"></param> /// <param name="file"></param>
/// <returns></returns> /// <returns></returns>
ValueTask AddFile(Guid user, PrivateVoidFile voidFile); ValueTask AddFile(Guid user, Guid file);
/// <summary> /// <summary>
/// Get the uploader of a single file /// Get the uploader of a single file

View File

@@ -12,7 +12,7 @@ public static class FileStorageStartup
if (settings.CloudStorage != default) if (settings.CloudStorage != default)
{ {
services.AddTransient<IUserUploadsStore, UserUploadStore>(); services.AddTransient<IUserUploadsStore, CacheUserUploadStore>();
// cloud storage // cloud storage
if (settings.CloudStorage.S3 != default) if (settings.CloudStorage.S3 != default)
@@ -29,7 +29,7 @@ public static class FileStorageStartup
} }
else else
{ {
services.AddTransient<IUserUploadsStore, UserUploadStore>(); services.AddTransient<IUserUploadsStore, CacheUserUploadStore>();
services.AddTransient<IFileStore, LocalDiskFileStore>(); services.AddTransient<IFileStore, LocalDiskFileStore>();
services.AddTransient<IFileMetadataStore, LocalDiskFileMetadataStore>(); services.AddTransient<IFileMetadataStore, LocalDiskFileMetadataStore>();
} }

View File

@@ -15,9 +15,10 @@ public class MigrateToPostgres : IMigration
private readonly ICache _cache; private readonly ICache _cache;
private readonly IPaywallStore _paywallStore; private readonly IPaywallStore _paywallStore;
private readonly IUserStore _userStore; private readonly IUserStore _userStore;
private readonly IUserUploadsStore _userUploads;
public MigrateToPostgres(VoidSettings settings, ILogger<MigrateToPostgres> logger, IFileMetadataStore fileMetadata, public MigrateToPostgres(VoidSettings settings, ILogger<MigrateToPostgres> logger, IFileMetadataStore fileMetadata,
ICache cache, IPaywallStore paywallStore, IUserStore userStore) ICache cache, IPaywallStore paywallStore, IUserStore userStore, IUserUploadsStore userUploads)
{ {
_logger = logger; _logger = logger;
_settings = settings; _settings = settings;
@@ -25,6 +26,7 @@ public class MigrateToPostgres : IMigration
_cache = cache; _cache = cache;
_paywallStore = paywallStore; _paywallStore = paywallStore;
_userStore = userStore; _userStore = userStore;
_userUploads = userUploads;
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -56,13 +58,17 @@ public class MigrateToPostgres : IMigration
var localDiskMetaStore = var localDiskMetaStore =
new LocalDiskFileMetadataStore(_settings); new LocalDiskFileMetadataStore(_settings);
var files = await localDiskMetaStore.ListFiles<SecretVoidFileMeta>(new(0, int.MaxValue)); var files = await localDiskMetaStore.ListFiles<UploaderSecretVoidFileMeta>(new(0, int.MaxValue));
await foreach (var file in files.Results) await foreach (var file in files.Results)
{ {
try try
{ {
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)
{
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);
} }
@@ -134,4 +140,9 @@ public class MigrateToPostgres : IMigration
public string? PasswordHash { get; set; } public string? PasswordHash { get; set; }
public string? Password { get; set; } public string? Password { get; set; }
} }
private record UploaderSecretVoidFileMeta : SecretVoidFileMeta
{
public Guid? Uploader { get; set; }
}
} }

View File

@@ -4,11 +4,11 @@ using VoidCat.Services.Abstractions;
namespace VoidCat.Services.Users; namespace VoidCat.Services.Users;
/// <inheritdoc /> /// <inheritdoc />
public class UserUploadStore : IUserUploadsStore public class CacheUserUploadStore : IUserUploadsStore
{ {
private readonly ICache _cache; private readonly ICache _cache;
public UserUploadStore(ICache cache) public CacheUserUploadStore(ICache cache)
{ {
_cache = cache; _cache = cache;
} }
@@ -43,10 +43,10 @@ public class UserUploadStore : IUserUploadsStore
} }
/// <inheritdoc /> /// <inheritdoc />
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.AddToList(MapKey(user), file.ToString());
await _cache.Set(MapUploader(voidFile.Id), user); await _cache.Set(MapUploader(file), user);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -60,12 +60,12 @@ and uf.""File"" = f.""Id""";
} }
/// <inheritdoc /> /// <inheritdoc />
public async ValueTask AddFile(Guid user, PrivateVoidFile voidFile) public async ValueTask AddFile(Guid user, Guid file)
{ {
await using var conn = await _connection.Get(); await using var conn = await _connection.Get();
await conn.ExecuteAsync(@"insert into ""UserFiles""(""File"", ""User"") values(:file, :user)", new await conn.ExecuteAsync(@"insert into ""UserFiles""(""File"", ""User"") values(:file, :user)", new
{ {
file = voidFile.Id, file,
user user
}); });
} }