2022-02-27 13:54:25 +00:00
|
|
|
using VoidCat.Model;
|
2022-09-08 09:41:31 +00:00
|
|
|
using VoidCat.Model.User;
|
2022-02-27 13:54:25 +00:00
|
|
|
using VoidCat.Services.Abstractions;
|
|
|
|
|
|
|
|
namespace VoidCat.Services.Files;
|
|
|
|
|
2022-09-06 21:32:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Main interface for getting file info to serve to clients.
|
|
|
|
/// This interface should wrap all stores and return the combined result
|
|
|
|
/// </summary>
|
|
|
|
public sealed class FileInfoManager
|
2022-02-27 13:54:25 +00:00
|
|
|
{
|
|
|
|
private readonly IFileMetadataStore _metadataStore;
|
2022-09-07 14:52:40 +00:00
|
|
|
private readonly IPaymentStore _paymentStore;
|
2022-02-27 13:54:25 +00:00
|
|
|
private readonly IStatsReporter _statsReporter;
|
|
|
|
private readonly IUserStore _userStore;
|
2022-03-07 13:38:28 +00:00
|
|
|
private readonly IVirusScanStore _virusScanStore;
|
2022-06-10 20:42:36 +00:00
|
|
|
private readonly IUserUploadsStore _userUploadsStore;
|
2022-02-27 13:54:25 +00:00
|
|
|
|
2022-09-07 14:52:40 +00:00
|
|
|
public FileInfoManager(IFileMetadataStore metadataStore, IPaymentStore paymentStore, IStatsReporter statsReporter,
|
2022-06-10 20:42:36 +00:00
|
|
|
IUserStore userStore, IVirusScanStore virusScanStore, IUserUploadsStore userUploadsStore)
|
2022-02-27 13:54:25 +00:00
|
|
|
{
|
|
|
|
_metadataStore = metadataStore;
|
2022-09-07 14:52:40 +00:00
|
|
|
_paymentStore = paymentStore;
|
2022-02-27 13:54:25 +00:00
|
|
|
_statsReporter = statsReporter;
|
|
|
|
_userStore = userStore;
|
2022-03-07 13:38:28 +00:00
|
|
|
_virusScanStore = virusScanStore;
|
2022-06-10 20:42:36 +00:00
|
|
|
_userUploadsStore = userUploadsStore;
|
2022-02-27 13:54:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 21:32:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get all metadata for a single file
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id"></param>
|
|
|
|
/// <returns></returns>
|
2022-06-10 20:42:36 +00:00
|
|
|
public ValueTask<PublicVoidFile?> Get(Guid id)
|
2022-02-27 13:54:25 +00:00
|
|
|
{
|
2022-09-11 19:07:38 +00:00
|
|
|
return Get<PublicVoidFile, FileMeta>(id);
|
2022-06-10 20:42:36 +00:00
|
|
|
}
|
2022-02-27 13:54:25 +00:00
|
|
|
|
2022-09-06 21:32:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get all private metadata for a single file
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id"></param>
|
|
|
|
/// <returns></returns>
|
2022-06-10 20:42:36 +00:00
|
|
|
public ValueTask<PrivateVoidFile?> GetPrivate(Guid id)
|
|
|
|
{
|
2022-09-11 19:07:38 +00:00
|
|
|
return Get<PrivateVoidFile, SecretFileMeta>(id);
|
2022-02-27 13:54:25 +00:00
|
|
|
}
|
2022-03-04 20:05:01 +00:00
|
|
|
|
2022-09-06 21:32:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get all metadata for multiple files
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
/// <returns></returns>
|
2022-06-06 21:51:25 +00:00
|
|
|
public async ValueTask<IReadOnlyList<PublicVoidFile>> Get(Guid[] ids)
|
|
|
|
{
|
|
|
|
var ret = new List<PublicVoidFile>();
|
|
|
|
foreach (var id in ids)
|
|
|
|
{
|
|
|
|
var v = await Get(id);
|
|
|
|
if (v != default)
|
|
|
|
{
|
|
|
|
ret.Add(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 21:32:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Deletes all file metadata
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id"></param>
|
|
|
|
/// <returns></returns>
|
2022-03-04 20:05:01 +00:00
|
|
|
public async ValueTask Delete(Guid id)
|
|
|
|
{
|
|
|
|
await _metadataStore.Delete(id);
|
2022-09-07 14:52:40 +00:00
|
|
|
await _paymentStore.Delete(id);
|
2022-03-04 20:05:01 +00:00
|
|
|
await _statsReporter.Delete(id);
|
2022-03-07 13:38:28 +00:00
|
|
|
await _virusScanStore.Delete(id);
|
2022-03-04 20:05:01 +00:00
|
|
|
}
|
2022-06-10 20:42:36 +00:00
|
|
|
|
|
|
|
private async ValueTask<TFile?> Get<TFile, TMeta>(Guid id)
|
2022-09-11 19:07:38 +00:00
|
|
|
where TMeta : FileMeta where TFile : VoidFile<TMeta>, new()
|
2022-06-10 20:42:36 +00:00
|
|
|
{
|
|
|
|
var meta = _metadataStore.Get<TMeta>(id);
|
2022-09-07 14:52:40 +00:00
|
|
|
var payment = _paymentStore.Get(id);
|
2022-06-10 20:42:36 +00:00
|
|
|
var bandwidth = _statsReporter.GetBandwidth(id);
|
2022-06-13 10:29:16 +00:00
|
|
|
var virusScan = _virusScanStore.GetByFile(id);
|
2022-06-10 20:42:36 +00:00
|
|
|
var uploader = _userUploadsStore.Uploader(id);
|
2022-09-07 14:52:40 +00:00
|
|
|
await Task.WhenAll(meta.AsTask(), payment.AsTask(), bandwidth.AsTask(), virusScan.AsTask(), uploader.AsTask());
|
2022-06-10 20:42:36 +00:00
|
|
|
|
|
|
|
if (meta.Result == default) return default;
|
2022-09-08 09:41:31 +00:00
|
|
|
var user = uploader.Result.HasValue ? await _userStore.Get<PublicUser>(uploader.Result.Value) : null;
|
2022-06-10 20:42:36 +00:00
|
|
|
|
|
|
|
return new TFile()
|
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
Metadata = meta.Result,
|
2022-09-07 14:52:40 +00:00
|
|
|
Payment = payment.Result,
|
2022-06-10 20:42:36 +00:00
|
|
|
Bandwidth = bandwidth.Result,
|
2022-09-08 09:41:31 +00:00
|
|
|
Uploader = user?.Flags.HasFlag(UserFlags.PublicProfile) == true ? user : null,
|
2022-06-10 20:42:36 +00:00
|
|
|
VirusScan = virusScan.Result
|
|
|
|
};
|
|
|
|
}
|
2022-06-06 21:51:25 +00:00
|
|
|
}
|