using System.Globalization;
using System.Text;
using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.IO;
using Chronos.Core.Model;
using Chronos.Core.Project;
namespace Chronos.Data.Projects;
/// The newest version of a project as its record names it: id and number.
public sealed record ProjectHead(Guid Id, int Number);
/// A version's file would be over , so it could never be read back: nothing was written.
public sealed class ProjectVersionTooLargeException(string message) : IOException(message);
///
/// The record of a project in its folder (.chronos/projects/<id>/project.json): what the index row is rebuilt
/// from. is set when the project was deleted, once its folder is in the projects' trash.
///
/// Main only: the version whose settings a mirror last wrote into the sequence's project.json
/// (any index of the archive). An index that meets Main for the first time compares project.json with it, never
/// with a copy of the settings it may have read long ago (). Older records have none.
public sealed record ProjectRecord(Guid Id, Guid SequenceId, string Name, bool IsMain, ProjectHead? Head, DateTimeOffset Created, string? CreatedBy,
DateTimeOffset Updated, DateTimeOffset? Deleted = null, ProjectHead? Mirrored = null)
{
/// The record of an index row, naming as the version mirrored last (Main).
public static ProjectRecord Of(SequenceProject project, ProjectHead? mirrored = null)
{
ArgumentNullException.ThrowIfNull(project);
return new ProjectRecord(project.Id, project.SequenceId, project.Name, project.IsMain,
project.HeadVersionId is { } head ? new ProjectHead(head, project.HeadNumber) : null, project.CreatedAt, project.CreatedBy, project.UpdatedAt,
project.DeletedAt, project.IsMain ? mirrored : null);
}
/// The index row of this record (nothing mirrored known yet).
public SequenceProject ToProject() => new()
{
Id = Id,
SequenceId = SequenceId,
Name = Name,
IsMain = IsMain,
HeadVersionId = Head?.Id,
HeadNumber = Head?.Number ?? 0,
CreatedBy = CreatedBy,
CreatedAt = Created,
UpdatedAt = Updated,
DeletedAt = Deleted,
};
}
///
/// The files of projects (owner review 2, R8), the same in an archive and in a workspace: the record of a project
/// () and one file per version, immutable once written:
/// { "format": "chronos-project-version", "formatVersion": 1, "id", "projectId", "parentId", "number", "kind",
/// "author", "clientName", "note", "settingsFormat", "framesVersion", "contentHash", "restoredFrom", "created",
/// "stored", "settings": "<the portable settings as text>" }
/// The settings are a JSON string, so the bytes the content hash covers come back exactly; a version whose text does not
/// match its hash is refused as damaged. Writes are atomic (temp, flush, rename).
///
public static class ProjectSidecars
{
public const string RecordFormat = "chronos-project-record";
public const string VersionFormat = "chronos-project-version";
public const int FormatVersion = 1;
///
/// The one size limit of a version file, for writing and reading alike: a version that could not be read back is never
/// written (). A version is a few kilobytes; each photo it parks (an exclusion, the hero, a
/// keyframe waiting for its photo) takes about 340 bytes of the file, so this holds the positions of about 190,000
/// photos, well past the largest sequences.
///
public const int MaxVersionBytes = 64 << 20;
private static readonly JsonWriterOptions Indented = new() { Indented = true };
/// Whether can be stored: its file within .
public static bool Fits(ProjectVersion version) => Encoding.UTF8.GetByteCount(VersionJson(version)) <= MaxVersionBytes;
///
/// Writes a version file. A version is immutable: an existing file is left when it holds the same version (same id
/// and content hash) and refused otherwise, unless (a workspace's own copy of a version,
/// stored at its home since).
///
/// Another version is at that path.
/// The file would be over : nothing was written.
public static void WriteVersion(string path, ProjectVersion version, bool replace = false)
{
ArgumentNullException.ThrowIfNull(version);
var json = VersionJson(version);
var bytes = Encoding.UTF8.GetByteCount(json);
if (bytes > MaxVersionBytes)
throw new ProjectVersionTooLargeException($"Version {version.Number} of project {version.ProjectId} takes {bytes} bytes, over the {MaxVersionBytes} a version may.");
if (!replace && File.Exists(path))
{
ProjectVersion existing;
try
{
existing = ReadVersion(path);
}
catch (InvalidDataException)
{
// A damaged file is never overwritten: it is moved aside, kept.
File.Move(path, path + ".damaged-" + DateTime.UtcNow.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture));
existing = null!;
}
if (existing is not null)
{
if (existing.Id == version.Id && string.Equals(existing.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase)) return;
throw new IOException($"{path} holds another version ({existing.Id}).");
}
}
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
AtomicFile.WriteAllText(path, json);
}
/// Reads a version file and checks its content hash.
/// Not a version file, damaged, newer, over , or its settings do not match their hash.
/// The file cannot be read.
public static ProjectVersion ReadVersion(string path)
{
if (new FileInfo(path).Length > MaxVersionBytes) throw new InvalidDataException($"{path} is too large to be a project version.");
return ParseVersion(AtomicFile.ReadAllText(path), path);
}
/// The text of a version file.
public static string VersionJson(ProjectVersion version)
{
ArgumentNullException.ThrowIfNull(version);
using var buffer = new MemoryStream();
using (var w = new Utf8JsonWriter(buffer, Indented))
{
w.WriteStartObject();
w.WriteString("format", VersionFormat);
w.WriteNumber("formatVersion", FormatVersion);
w.WriteString("id", version.Id);
w.WriteString("projectId", version.ProjectId);
if (version.ParentId is { } parent) w.WriteString("parentId", parent);
w.WriteNumber("number", version.Number);
w.WriteString("kind", ProjectVersionKinds.Name(version.Kind));
if (version.Author is { } author) w.WriteString("author", author);
if (version.ClientName is { } client) w.WriteString("clientName", client);
if (version.Note is { } note) w.WriteString("note", note);
w.WriteNumber("settingsFormat", version.SettingsFormat);
w.WriteNumber("framesVersion", version.FramesVersion);
w.WriteString("contentHash", version.ContentHash.ToLowerInvariant());
if (version.RestoredFrom is { } restored) w.WriteString("restoredFrom", restored);
w.WriteString("created", version.CreatedAt);
if (version.StoredAt is { } stored) w.WriteString("stored", stored);
w.WriteString("settings", version.SettingsJson);
w.WriteEndObject();
}
return Encoding.UTF8.GetString(buffer.GetBuffer(), 0, (int)buffer.Length);
}
/// Reads a version file's text ( names it in errors) and checks its content hash.
/// Not a version file, damaged, newer, or its settings do not match their hash.
public static ProjectVersion ParseVersion(string json, string source)
{
using var document = Parse(json, source);
var root = document.RootElement;
Check(root, VersionFormat, source);
var settings = Text(root, "settings") ?? throw new InvalidDataException($"{source} holds no settings.");
var hash = Text(root, "contentHash") ?? throw new InvalidDataException($"{source} has no content hash.");
if (!ProjectContent.Verifies(settings, hash)) throw new InvalidDataException($"The settings in {source} do not match their content hash.");
return new ProjectVersion
{
Id = Id(root, "id") ?? throw new InvalidDataException($"{source} has no version id."),
ProjectId = Id(root, "projectId") ?? throw new InvalidDataException($"{source} names no project."),
ParentId = Id(root, "parentId"),
Number = Int(root, "number") is { } number and > 0 ? number : throw new InvalidDataException($"{source} has no version number."),
Kind = ProjectVersionKinds.Parse(Text(root, "kind")),
Author = Text(root, "author"),
ClientName = Text(root, "clientName"),
Note = Text(root, "note"),
SettingsJson = settings,
ContentHash = hash.ToLowerInvariant(),
SettingsFormat = Int(root, "settingsFormat") ?? ProjectContent.FormatOf(settings),
FramesVersion = Int(root, "framesVersion") ?? 0,
RestoredFrom = Id(root, "restoredFrom"),
CreatedAt = Time(root, "created") ?? default,
StoredAt = Time(root, "stored"),
};
}
/// Writes a project's record atomically.
public static void WriteRecord(string path, ProjectRecord record)
{
ArgumentNullException.ThrowIfNull(record);
using var buffer = new MemoryStream();
using (var w = new Utf8JsonWriter(buffer, Indented))
{
w.WriteStartObject();
w.WriteString("format", RecordFormat);
w.WriteNumber("formatVersion", FormatVersion);
w.WriteString("id", record.Id);
w.WriteString("sequenceId", record.SequenceId);
w.WriteString("name", record.Name);
w.WriteBoolean("isMain", record.IsMain);
if (record.Head is { } head)
{
w.WriteStartObject("head");
w.WriteString("id", head.Id);
w.WriteNumber("number", head.Number);
w.WriteEndObject();
}
w.WriteString("created", record.Created);
if (record.CreatedBy is { } by) w.WriteString("createdBy", by);
w.WriteString("updated", record.Updated);
if (record.Deleted is { } deleted) w.WriteString("deleted", deleted);
if (record.Mirrored is { } mirrored)
{
w.WriteStartObject("mirrored");
w.WriteString("id", mirrored.Id);
w.WriteNumber("number", mirrored.Number);
w.WriteEndObject();
}
w.WriteEndObject();
}
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
AtomicFile.WriteAllBytes(path, buffer.GetBuffer().AsSpan(0, (int)buffer.Length));
}
/// Reads a project's record.
/// Not a project record, damaged or newer.
/// The file cannot be read.
public static ProjectRecord ReadRecord(string path)
{
using var document = Parse(AtomicFile.ReadAllText(path), path);
var root = document.RootElement;
Check(root, RecordFormat, path);
var name = Text(root, "name");
return new ProjectRecord(
Id(root, "id") ?? throw new InvalidDataException($"{path} has no project id."),
Id(root, "sequenceId") ?? throw new InvalidDataException($"{path} names no sequence."),
string.IsNullOrWhiteSpace(name) ? throw new InvalidDataException($"{path} has no name.") : name,
root.TryGetProperty("isMain", out var main) && main.ValueKind == JsonValueKind.True,
Head(root, "head"),
Time(root, "created") ?? default,
Text(root, "createdBy"),
Time(root, "updated") ?? Time(root, "created") ?? default,
Time(root, "deleted"),
Head(root, "mirrored"));
}
/// A version a record names (its head, the version mirrored): { "id", "number" }; null when it names none.
private static ProjectHead? Head(JsonElement root, string name) =>
root.TryGetProperty(name, out var h) && h.ValueKind == JsonValueKind.Object && Id(h, "id") is { } id && Int(h, "number") is { } number and > 0
? new ProjectHead(id, number)
: null;
/// The version files of a folder by number and id (000001-<id>.json), in number order; other files are not listed.
public static IReadOnlyList<(int Number, Guid Id, string Path)> ListVersions(string directory)
{
if (!Directory.Exists(directory)) return [];
var found = new List<(int, Guid, string)>();
foreach (var path in Directory.EnumerateFiles(directory, "*.json"))
{
if (TryParseName(Path.GetFileName(path), out var number, out var id)) found.Add((number, id, path));
}
return [.. found.OrderBy(f => f.Item1).ThenBy(f => f.Item2)];
}
/// The number and id a version file name carries ().
public static bool TryParseName(string fileName, out int number, out Guid id)
{
number = 0;
id = Guid.Empty;
var name = Path.GetFileNameWithoutExtension(fileName);
var dash = name.IndexOf('-', StringComparison.Ordinal);
return dash > 0 && Path.GetExtension(fileName).Equals(".json", StringComparison.OrdinalIgnoreCase)
&& int.TryParse(name.AsSpan(0, dash), NumberStyles.None, CultureInfo.InvariantCulture, out number) && number > 0
&& Guid.TryParseExact(name.AsSpan(dash + 1), "N", out id);
}
private static JsonDocument Parse(string json, string source)
{
try
{
return JsonDocument.Parse(json, new JsonDocumentOptions { MaxDepth = 64 });
}
catch (JsonException ex)
{
throw new InvalidDataException($"{source} is not valid JSON.", ex);
}
}
private static void Check(JsonElement root, string format, string source)
{
if (root.ValueKind != JsonValueKind.Object || Text(root, "format") != format) throw new InvalidDataException($"{source} is not a {format} file.");
if (Int(root, "formatVersion") is not { } version || version < 1) throw new InvalidDataException($"{source} has no valid format version.");
if (version > FormatVersion) throw new InvalidDataException($"{source} uses format {version}, newer than this Chronos ({FormatVersion}).");
}
private static string? Text(JsonElement element, string name) =>
element.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String ? value.GetString() : null;
private static int? Int(JsonElement element, string name) =>
element.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.Number && value.TryGetInt32(out var number) ? number : null;
private static Guid? Id(JsonElement element, string name) =>
element.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String && value.TryGetGuid(out var id) && id != Guid.Empty ? id : null;
private static DateTimeOffset? Time(JsonElement element, string name) =>
element.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String && value.TryGetDateTimeOffset(out var time) ? time : null;
}