void.cat/VoidCat/Services/Files/LocalDiskFileStorage.cs

70 lines
2.1 KiB
C#
Raw Normal View History

2022-01-25 23:39:51 +00:00
using VoidCat.Model;
using VoidCat.Model.Exceptions;
2022-02-16 16:33:00 +00:00
using VoidCat.Services.Abstractions;
2022-01-25 23:39:51 +00:00
2022-02-22 14:20:31 +00:00
namespace VoidCat.Services.Files;
2022-01-25 23:39:51 +00:00
2022-06-08 16:17:53 +00:00
/// <inheritdoc cref="IFileStore"/>
2022-03-01 11:32:41 +00:00
public class LocalDiskFileStore : StreamFileStore, IFileStore
2022-01-25 23:39:51 +00:00
{
2022-03-01 16:48:42 +00:00
private const string FilesDir = "files-v1";
2022-02-22 14:20:31 +00:00
private readonly ILogger<LocalDiskFileStore> _logger;
2022-01-25 23:39:51 +00:00
private readonly VoidSettings _settings;
2022-02-08 23:52:01 +00:00
2022-06-08 16:17:53 +00:00
public LocalDiskFileStore(ILogger<LocalDiskFileStore> logger, VoidSettings settings, IAggregateStatsCollector stats)
: base(stats)
2022-01-25 23:39:51 +00:00
{
_settings = settings;
2022-02-22 14:20:31 +00:00
_logger = logger;
2022-01-25 23:39:51 +00:00
2022-03-01 16:48:42 +00:00
var dir = Path.Combine(_settings.DataDirectory, FilesDir);
if (!Directory.Exists(dir))
2022-01-25 23:39:51 +00:00
{
2022-03-01 16:48:42 +00:00
Directory.CreateDirectory(dir);
2022-01-25 23:39:51 +00:00
}
}
2022-06-08 16:17:53 +00:00
/// <inheritdoc />
2022-02-16 16:33:00 +00:00
public async ValueTask Egress(EgressRequest request, Stream outStream, CancellationToken cts)
2022-01-25 23:39:51 +00:00
{
2022-03-07 13:38:53 +00:00
await using var fs = await Open(request, cts);
2022-03-01 11:32:41 +00:00
await EgressFromStream(fs, request, outStream, cts);
2022-01-25 23:39:51 +00:00
}
/// <inheritdoc />
public string Key => "local-disk";
2022-06-08 16:17:53 +00:00
/// <inheritdoc />
2022-02-17 15:52:49 +00:00
public async ValueTask<PrivateVoidFile> Ingress(IngressPayload payload, CancellationToken cts)
2022-01-25 23:39:51 +00:00
{
2022-03-01 11:32:41 +00:00
var fPath = MapPath(payload.Id);
2022-02-16 16:33:00 +00:00
await using var fsTemp = new FileStream(fPath,
2022-02-10 22:22:34 +00:00
payload.IsAppend ? FileMode.Append : FileMode.Create, FileAccess.Write);
2022-03-01 11:32:41 +00:00
return await IngressToStream(fsTemp, payload, cts);
2022-02-08 18:20:59 +00:00
}
2022-02-08 23:52:01 +00:00
2022-06-08 16:17:53 +00:00
/// <inheritdoc />
2022-06-06 21:51:25 +00:00
public ValueTask DeleteFile(Guid id)
2022-02-22 14:20:31 +00:00
{
var fp = MapPath(id);
if (File.Exists(fp))
{
_logger.LogInformation("Deleting file: {Path}", fp);
File.Delete(fp);
}
2022-06-08 16:17:53 +00:00
2022-06-06 21:51:25 +00:00
return ValueTask.CompletedTask;
2022-02-22 14:20:31 +00:00
}
2022-06-08 16:17:53 +00:00
/// <inheritdoc />
2022-03-07 13:38:53 +00:00
public ValueTask<Stream> Open(EgressRequest request, CancellationToken cts)
{
var path = MapPath(request.Id);
if (!File.Exists(path)) throw new VoidFileNotFoundException(request.Id);
return ValueTask.FromResult<Stream>(new FileStream(path, FileMode.Open, FileAccess.Read));
}
2022-01-25 23:39:51 +00:00
private string MapPath(Guid id) =>
2022-03-01 16:48:42 +00:00
Path.Join(_settings.DataDirectory, FilesDir, id.ToString());
2022-03-01 11:32:41 +00:00
}