using Chronos.Core.Model; using Chronos.Data; namespace Chronos.Server.Storage; /// Validation shared by frame and render uploads. public static class UploadRules { /// A plain file name (no folders, no characters Windows or Linux refuse), or null. public static string? SafeFileName(string? name) { if (string.IsNullOrWhiteSpace(name)) return null; var trimmed = name.Trim(); if (trimmed.Length > 255 || trimmed is "." or ".." || trimmed.EndsWith('.')) return null; foreach (var c in trimmed) { if (char.IsControl(c) || c is '/' or '\\' or '<' or '>' or ':' or '"' or '|' or '?' or '*') return null; } return trimmed; } /// 64 hexadecimal characters. public static bool IsSha256(string? value) => value is { Length: 64 } && value.All(char.IsAsciiHexDigit); /// Indices of the chunks set in a received-chunk bitmap. public static List ReceivedChunks(Chronos.Data.Repositories.Upload upload) { var received = new List(upload.ReceivedCount); for (var i = 0; i < upload.ChunkCount; i++) { if (upload.HasChunk(i)) received.Add(i); } return received; } /// The frame's original is in the archive with its recorded size. public static bool IsOnDisk(string archiveRoot, Frame frame) => ArchivePathGuard.ExistingFile(archiveRoot, frame.RelativePath) is { } file && file.Length == frame.Size; /// /// Not flagged missing or corrupt, and : the copy holds its content, so it may stand for an upload /// of the same content; a damaged one is repaired by uploading the content again. /// public static bool IsIntact(string archiveRoot, Frame frame) => (frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) == 0 && IsOnDisk(archiveRoot, frame); /// /// The frame that stands for an upload of content the archive already has (): an intact copy, /// the one in the upload's sequence first. Null when there is none, or when the target holds /// the content only in damaged copies: the upload's bytes repair them, whatever other sequences hold. /// public static Frame? StandIn(string archiveRoot, IReadOnlyList copies, Guid? target) { var intact = copies.Where(f => IsIntact(archiveRoot, f)).ToList(); if (copies.Any(f => f.SequenceId == target) && !intact.Any(f => f.SequenceId == target)) return null; return intact.FirstOrDefault(f => f.SequenceId == target) ?? intact.FirstOrDefault(); } /// /// The sequence an upload files into: its own, or the one its session key names (the user's own key without one); /// null while that sequence does not exist yet. /// public static async Task TargetAsync(ChronosDatabase db, Guid archiveId, Guid? sequenceId, string? sessionKey, Guid userId, CancellationToken cancellationToken) => sequenceId ?? await db.Sequences.FindBySessionKeyAsync(archiveId, SessionKeyOf(sessionKey, userId), cancellationToken).ConfigureAwait(false); /// The session key an upload without a sequence is grouped by: the client's, or one per user. public static string SessionKeyOf(string? sessionKey, Guid userId) => sessionKey ?? "user-" + userId.ToString("N"); /// Free bytes on the volume holding , or null when the platform cannot tell. 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; } } }