feat: owner review 2 phase 1: :latest, Unraid array space, Settings with About, whole-folder import (#9)
CI / Build and test (push) Successful in 5m11s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 8m57s
CI / PostgreSQL 16 integration (push) Successful in 3m10s
CI / PostgreSQL 17 integration (push) Successful in 3m30s
CI / Docker image (push) Successful in 49s
Release / Server image (push) Successful in 1m4s
CI / Build and test (push) Successful in 5m11s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 8m57s
CI / PostgreSQL 16 integration (push) Successful in 3m10s
CI / PostgreSQL 17 integration (push) Successful in 3m30s
CI / Docker image (push) Successful in 49s
Release / Server image (push) Successful in 1m4s
- :latest follows the highest stable release, or the highest pre-release while none is stable (Chronos.Tools release-channels); bad tags are refused before anything is built. - Unraid: CHRONOS_SPACE_PATH and the template's Array view show the array's free space; the write volume (cache) is shown apart; completions check the archive volume (507). - Settings is a gear, docked at the foot of the admin sidebar; About is the last part of Settings on the desktop and the panel. - Import > Folder > Whole folder: one sequence per format (desktop only). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #9.
This commit is contained in:
1 parent
5acdf146c7
commit
1f089888e8
320 files changed
+3030
-927
No files matched your search
@@ -23,6 +23,7 @@ public sealed class RenderUploads(
|
||||
UploadStore store,
|
||||
RenderStore renders,
|
||||
EventHub events,
|
||||
VolumeSpace volumes,
|
||||
TimeProvider time)
|
||||
{
|
||||
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
|
||||
@@ -46,7 +47,8 @@ public sealed class RenderUploads(
|
||||
if (await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken) is { } open && open.SequenceId == sequenceId && open.Size == request.Size
|
||||
&& Read(open)?.RenderId == request.RenderId && store.Exists(open.Id))
|
||||
return Started(open);
|
||||
if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size) return Outcome<StartUploadResponse>.Fail(507, "insufficient_storage");
|
||||
if (volumes.Measure(options.UploadsPath) is { } uploadVolume && uploadVolume.FreeBytes < request.Size)
|
||||
return Outcome<StartUploadResponse>.Fail(507, "insufficient_storage");
|
||||
|
||||
var now = time.GetUtcNow();
|
||||
var upload = await db.Uploads.CreateAsync(new Upload
|
||||
@@ -77,6 +79,7 @@ public sealed class RenderUploads(
|
||||
if (upload.State != UploadState.Receiving) return Outcome<RenderDto>.Fail(409, "upload_state");
|
||||
if (!upload.IsComplete)
|
||||
return Outcome<RenderDto>.Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
|
||||
if (!uploads.ArchiveHasRoom(upload)) return Outcome<RenderDto>.Fail(507, "insufficient_storage");
|
||||
if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Committing, cancellationToken: cancellationToken))
|
||||
return Outcome<RenderDto>.Fail(409, "upload_state");
|
||||
|
||||
|
||||
@@ -66,17 +66,4 @@ public static class UploadRules
|
||||
|
||||
/// <summary>The session key an upload without a sequence is grouped by: the client's, or one per user.</summary>
|
||||
public static string SessionKeyOf(string? sessionKey, Guid userId) => sessionKey ?? "user-" + userId.ToString("N");
|
||||
|
||||
/// <summary>Free bytes on the volume holding <paramref name="path"/>, or null when the platform cannot tell.</summary>
|
||||
public static long? FreeBytes(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new DriveInfo(Path.GetFullPath(path)).AvailableFreeSpace;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,10 @@ namespace Chronos.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Chunked, resumable, hash-verified frame uploads (BRIEF §14.4): start (short-circuit on a known SHA-256 held by an
|
||||
/// intact frame, resume an unfinished upload, quota and disk checks), idempotent chunks, and completion that stages the
|
||||
/// file on the archive volume, verifies its hash and hands it to <see cref="IArchiveIngest"/>, which repairs a missing or
|
||||
/// damaged frame of that content (no quota is charged for a repair: the archive already counts those bytes).
|
||||
/// intact frame, resume an unfinished upload, quota and disk checks), idempotent chunks, and completion that checks the
|
||||
/// archive volume's free space, stages the file there, verifies its hash and hands it to <see cref="IArchiveIngest"/>,
|
||||
/// which repairs a missing or damaged frame of that content (no quota is charged for a repair: the archive already
|
||||
/// counts those bytes).
|
||||
/// </summary>
|
||||
public sealed class UploadService(
|
||||
ChronosDatabase db,
|
||||
@@ -29,11 +30,12 @@ public sealed class UploadService(
|
||||
IArchiveIngest ingest,
|
||||
SequenceJobs sequenceJobs,
|
||||
EventHub events,
|
||||
VolumeSpace volumes,
|
||||
IHostApplicationLifetime lifetime,
|
||||
TimeProvider time,
|
||||
ILogger<UploadService> logger)
|
||||
{
|
||||
/// <summary>Space kept free on the upload volume beyond the announced size.</summary>
|
||||
/// <summary>Space kept free on the upload and archive volumes beyond an upload's size.</summary>
|
||||
private const long DiskReserve = 64L * 1024 * 1024;
|
||||
|
||||
/// <summary>Card events one account may publish per minute: progress once a second leaves room for every start and end.</summary>
|
||||
@@ -74,7 +76,7 @@ public sealed class UploadService(
|
||||
if (caller.User.UsedBytes + pending + request.Size > caller.User.QuotaBytes)
|
||||
return Fail<StartUploadResponse>(403, "quota_exceeded", caller.User.QuotaBytes.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size + DiskReserve)
|
||||
if (volumes.Measure(options.UploadsPath) is { } uploadVolume && uploadVolume.FreeBytes < request.Size + DiskReserve)
|
||||
return Fail<StartUploadResponse>(507, "insufficient_storage");
|
||||
|
||||
var now = time.GetUtcNow();
|
||||
@@ -165,6 +167,7 @@ public sealed class UploadService(
|
||||
if (upload.State != UploadState.Receiving) return Fail<CompleteUploadResponse>(409, "upload_state");
|
||||
if (!upload.IsComplete)
|
||||
return Fail<CompleteUploadResponse>(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
|
||||
if (!ArchiveHasRoom(upload)) return Fail<CompleteUploadResponse>(507, "insufficient_storage");
|
||||
if (!await db.Uploads.TryTransitionAsync(id, UploadState.Receiving, UploadState.Committing, cancellationToken: stopping))
|
||||
return Fail<CompleteUploadResponse>(409, "upload_state");
|
||||
|
||||
@@ -240,9 +243,24 @@ public sealed class UploadService(
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The archive volume has room for <paramref name="upload"/> and the reserve: completing it copies the file into the
|
||||
/// staging folder there (<see cref="StageVerifiedAsync"/>). Checked before the upload leaves the receiving state, so a
|
||||
/// refused completion (507) can be repeated once there is room. A volume that cannot be measured refuses nothing.
|
||||
/// </summary>
|
||||
public bool ArchiveHasRoom(Upload upload)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(upload);
|
||||
if (volumes.Measure(archive.Root) is not { } volume || volume.FreeBytes >= upload.Size + DiskReserve) return true;
|
||||
logger.LogWarning("The archive volume {Archive} has {Free} bytes free: upload {Upload} ({File}, {Size} bytes) waits for room",
|
||||
archive.Root, volume.FreeBytes, upload.Id, upload.FileName, upload.Size);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a fully received upload to the archive staging folder and checks its SHA-256. On a mismatch the upload
|
||||
/// fails, its files are deleted and null is returned; the client starts over.
|
||||
/// fails, its files are deleted and null is returned; the client starts over. Callers check
|
||||
/// <see cref="ArchiveHasRoom"/> first.
|
||||
/// </summary>
|
||||
public async Task<string?> StageVerifiedAsync(Upload upload, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
using System.Text;
|
||||
using Chronos.Server.Configuration;
|
||||
|
||||
namespace Chronos.Server.Storage;
|
||||
|
||||
/// <summary>Free and total bytes of a volume.</summary>
|
||||
public sealed record VolumeFigures(long FreeBytes, long TotalBytes);
|
||||
|
||||
/// <summary>One mount of <c>/proc/self/mountinfo</c>: its mount point, filesystem type (<c>fuse.shfs</c>) and source.</summary>
|
||||
public sealed record MountEntry(string MountPoint, string FileSystem, string Source);
|
||||
|
||||
/// <summary>
|
||||
/// The archive's storage as the admin panel shows it: the space volume's figures (<see cref="ServerOptions.SpacePath"/>
|
||||
/// when set and measurable, else the archive root) and filesystem, plus the archive root's own figures (the write
|
||||
/// volume) when a space path is in use and they differ.
|
||||
/// </summary>
|
||||
public sealed record StorageReport(VolumeFigures? Space, string? FileSystem, bool FromSpacePath, VolumeFigures? Write);
|
||||
|
||||
/// <summary>
|
||||
/// Free space of the server's folders (owner review 2, R2). The archive root is where uploads are staged and filed; on
|
||||
/// Unraid it is a user share (FUSE <c>shfs</c>) that reports the share's primary storage, often the cache, while the
|
||||
/// mover sends the photos to the array. <see cref="ServerOptions.SpacePath"/> names a folder whose volume the admin panel
|
||||
/// shows instead (the share seen on the array only). Tests replace <see cref="Measure"/> to simulate a full volume.
|
||||
/// </summary>
|
||||
public class VolumeSpace(ServerOptions options, ILogger<VolumeSpace> logger)
|
||||
{
|
||||
/// <summary>Two reads of one volume a moment apart differ by what was written in between.</summary>
|
||||
private const long SameVolumeSlack = 64L * 1024 * 1024;
|
||||
|
||||
private const string MountInfo = "/proc/self/mountinfo";
|
||||
|
||||
/// <summary>1 while the space path cannot be measured: it is warned about once, not at every request.</summary>
|
||||
private int _spaceUnavailable;
|
||||
|
||||
/// <summary>Free and total bytes of the volume holding <paramref name="path"/>; null when the folder is missing or the platform cannot tell.</summary>
|
||||
public virtual VolumeFigures? Measure(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(path)) return null;
|
||||
var drive = Drive(path);
|
||||
return new VolumeFigures(drive.AvailableFreeSpace, drive.TotalSize);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The storage figures of the archive at <paramref name="archiveRoot"/>: of the space path when one is set and can be
|
||||
/// measured, else of the archive root (a space path that cannot be measured is logged once, until it can again).
|
||||
/// </summary>
|
||||
public StorageReport Report(string archiveRoot)
|
||||
{
|
||||
var write = Measure(archiveRoot);
|
||||
var spacePath = options.SpacePath;
|
||||
if (string.IsNullOrWhiteSpace(spacePath)) return new StorageReport(write, FileSystemOf(archiveRoot), FromSpacePath: false, Write: null);
|
||||
if (Measure(spacePath) is not { } space)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _spaceUnavailable, 1) == 0)
|
||||
{
|
||||
logger.LogWarning("The space path {SpacePath} (CHRONOS_SPACE_PATH) is missing or cannot be measured: the admin panel shows the free space "
|
||||
+ "of the archive {Archive} instead. Map the folder into the container, or clear the setting.", spacePath, archiveRoot);
|
||||
}
|
||||
return new StorageReport(write, FileSystemOf(archiveRoot), FromSpacePath: false, Write: null);
|
||||
}
|
||||
if (Interlocked.Exchange(ref _spaceUnavailable, 0) == 1) logger.LogInformation("The space path {SpacePath} can be measured again", spacePath);
|
||||
var differs = write is not null && (write.TotalBytes != space.TotalBytes || Math.Abs(write.FreeBytes - space.FreeBytes) > SameVolumeSlack);
|
||||
return new StorageReport(space, FileSystemOf(spacePath), FromSpacePath: true, differs ? write : null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The type of the filesystem holding the folder <paramref name="path"/>: on Linux the mount's type (an Unraid user
|
||||
/// share is <c>fuse.shfs</c>, which the kernel's own figures call only FUSE), elsewhere what the system reports
|
||||
/// (NTFS). Null when the folder is missing or the type unknown.
|
||||
/// </summary>
|
||||
public static string? FileSystemOf(string path)
|
||||
{
|
||||
if (!Directory.Exists(path)) return null;
|
||||
if (MountOf(path) is { } mount) return mount.FileSystem;
|
||||
try
|
||||
{
|
||||
return Drive(path).DriveFormat;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mount holding the folder <paramref name="path"/>, read from <c>/proc/self/mountinfo</c>; null off Linux, when
|
||||
/// the folder is missing or the file cannot be read.
|
||||
/// </summary>
|
||||
public static MountEntry? MountOf(string path)
|
||||
{
|
||||
if (!OperatingSystem.IsLinux() || !Directory.Exists(path)) return null;
|
||||
try
|
||||
{
|
||||
return FindMount(File.ReadAllText(MountInfo), Path.GetFullPath(path));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mount of <paramref name="mountInfo"/> (the text of <c>/proc/self/mountinfo</c>) that holds the absolute
|
||||
/// <paramref name="path"/>: the longest mount point that is the path or one of its parent folders and, of mounts
|
||||
/// stacked on that point, the last listed (the one on top). Null when no line covers it.
|
||||
/// </summary>
|
||||
public static MountEntry? FindMount(string mountInfo, string path)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(mountInfo);
|
||||
ArgumentNullException.ThrowIfNull(path);
|
||||
var target = path.TrimEnd('/');
|
||||
if (target.Length == 0) target = "/";
|
||||
MountEntry? best = null;
|
||||
foreach (var line in mountInfo.Split('\n'))
|
||||
{
|
||||
if (ParseMountLine(line) is not { } entry || !Covers(entry.MountPoint, target)) continue;
|
||||
if (best is null || entry.MountPoint.Length >= best.MountPoint.Length) best = entry;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One mountinfo line (proc(5)): <c>id parent major:minor root mount-point options [optional fields…] - type source
|
||||
/// super-options</c>, where fields write a space, tab, newline or backslash as an octal escape (<c>\040</c>). Null when
|
||||
/// the line has another shape.
|
||||
/// </summary>
|
||||
public static MountEntry? ParseMountLine(string line)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(line);
|
||||
var fields = line.TrimEnd('\r').Split(' ');
|
||||
if (fields.Length < 9) return null;
|
||||
// The optional fields (none or more, "shared:5 master:1") end at a lone "-".
|
||||
var separator = Array.IndexOf(fields, "-", 6);
|
||||
if (separator < 0 || separator + 2 >= fields.Length) return null;
|
||||
var mountPoint = Unescape(fields[4]);
|
||||
if (!mountPoint.StartsWith('/')) return null;
|
||||
return new MountEntry(mountPoint, Unescape(fields[separator + 1]), Unescape(fields[separator + 2]));
|
||||
}
|
||||
|
||||
/// <summary>Windows wants a drive root; Unix accepts any folder and reports the filesystem it is on.</summary>
|
||||
private static DriveInfo Drive(string path)
|
||||
{
|
||||
var full = Path.GetFullPath(path);
|
||||
return new DriveInfo(OperatingSystem.IsWindows() ? Path.GetPathRoot(full)! : full);
|
||||
}
|
||||
|
||||
/// <summary><paramref name="mountPoint"/> is <paramref name="path"/> or one of its parent folders.</summary>
|
||||
private static bool Covers(string mountPoint, string path) =>
|
||||
mountPoint == "/"
|
||||
? path.StartsWith('/')
|
||||
: path.StartsWith(mountPoint, StringComparison.Ordinal) && (path.Length == mountPoint.Length || path[mountPoint.Length] == '/');
|
||||
|
||||
/// <summary>Decodes the kernel's <c>\ooo</c> escapes (it escapes only ASCII characters).</summary>
|
||||
private static string Unescape(string field)
|
||||
{
|
||||
if (!field.Contains('\\', StringComparison.Ordinal)) return field;
|
||||
var sb = new StringBuilder(field.Length);
|
||||
for (var i = 0; i < field.Length; i++)
|
||||
{
|
||||
if (field[i] == '\\' && i + 3 < field.Length && IsOctal(field[i + 1]) && IsOctal(field[i + 2]) && IsOctal(field[i + 3]))
|
||||
{
|
||||
sb.Append((char)(((field[i + 1] - '0') * 64) + ((field[i + 2] - '0') * 8) + (field[i + 3] - '0')));
|
||||
i += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(field[i]);
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static bool IsOctal(char c) => c is >= '0' and <= '7';
|
||||
}
|
||||
Reference in new issue
Block a user