CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
123 lines
6.6 KiB
C#
123 lines
6.6 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Project;
|
|
using Chronos.Data.Workspaces;
|
|
|
|
namespace Chronos.Data.Tests.Homes;
|
|
|
|
/// <summary>Workspaces and caches laid out as 1.0 left them, for the sorting into homes (owner review 5, #66).</summary>
|
|
internal static class HomeFixtures
|
|
{
|
|
public const string Nas = "https://nas.local:8443";
|
|
public const string NasByIp = "https://192.168.1.5:8443";
|
|
public const string Other = "https://other.example:8443";
|
|
|
|
/// <summary>A working state of <paramref name="home"/> written into <c><root>/<id></c> with some media, versions and a render.</summary>
|
|
public static Guid Project(string root, BundleHome home, string name = "Main", DateTimeOffset? saved = null, Guid? id = null, int proxies = 3)
|
|
{
|
|
var projectId = id ?? Guid.CreateVersion7();
|
|
var folder = Path.Combine(root, projectId.ToString("N"));
|
|
Directory.CreateDirectory(folder);
|
|
var archive = home.IsArchive
|
|
? new BundleArchive(home.ArchiveId ?? Guid.CreateVersion7(), "This PC", @"D:\ChronosArchive")
|
|
: new BundleArchive(Data.Projects.ServerProjectHome.ArchiveIdOf(home.Server!), "Studio NAS", null);
|
|
var bundle = new ProjectBundle(archive, new BundleSequence(home.SequenceId, "2026/2026-11-15 Harbour", "Harbour", proxies), new ProjectSettings(),
|
|
saved ?? DateTimeOffset.UtcNow)
|
|
{
|
|
Workspace = new BundleWorkspace(projectId, name, true, home, null, new BundleMedia(BundleMedia.Proxies, 1)),
|
|
};
|
|
bundle.Save(Path.Combine(folder, WorkspaceProject.BundleFile));
|
|
var media = Path.Combine(folder, WorkspaceProject.MediaFolder, WorkspaceProject.ProxiesFolder);
|
|
Directory.CreateDirectory(media);
|
|
for (var i = 0; i < proxies; i++) File.WriteAllBytes(Path.Combine(media, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(projectId, i, 500 + i));
|
|
Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.VersionsFolder));
|
|
File.WriteAllText(Path.Combine(folder, WorkspaceProject.VersionsFolder, "1-" + Guid.CreateVersion7().ToString("N") + ".json"), "{}");
|
|
Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.ExportsFolder));
|
|
File.WriteAllBytes(Path.Combine(folder, WorkspaceProject.ExportsFolder, "render.mp4"), Bytes(projectId, 99, 2048));
|
|
return projectId;
|
|
}
|
|
|
|
public static BundleHome Local() => new(BundleHome.ArchiveKind, Guid.CreateVersion7(), Guid.CreateVersion7());
|
|
|
|
public static BundleHome Server(string key) => new(BundleHome.ServerKind, Guid.CreateVersion7(), null, key);
|
|
|
|
/// <summary>A 1.0 workspace: its manifest (format 1) and the folder.</summary>
|
|
public static string LegacyWorkspace(string parent)
|
|
{
|
|
var root = Path.Combine(parent, "workspace");
|
|
_ = new WorkspaceStore(root);
|
|
return root;
|
|
}
|
|
|
|
/// <summary>A trash entry of <paramref name="projectId"/> holding a working state, as a removal leaves it.</summary>
|
|
public static string TrashEntry(string root, Guid projectId, BundleHome home)
|
|
{
|
|
var trash = Path.Combine(root, WorkspaceTrash.FolderName);
|
|
var staging = Path.Combine(trash, "staging");
|
|
Directory.CreateDirectory(staging);
|
|
Project(staging, home, id: projectId, proxies: 1);
|
|
var entry = Path.Combine(trash, projectId.ToString("N") + "-638000000000000000");
|
|
Directory.Move(Path.Combine(staging, projectId.ToString("N")), entry);
|
|
Directory.Delete(staging);
|
|
return entry;
|
|
}
|
|
|
|
/// <summary>The 1.0 proxy cache of <paramref name="key"/>'s sequence <paramref name="remoteId"/>.</summary>
|
|
public static string LegacyProxies(string cache, string key, Guid remoteId, int frames = 2)
|
|
{
|
|
var folder = Data.Sync.ProxyCache.LegacyFolder(Path.Combine(cache, "proxies"), key, remoteId);
|
|
Directory.CreateDirectory(folder);
|
|
File.WriteAllText(Path.Combine(folder, "remote.json"), "{}");
|
|
for (var i = 0; i < frames; i++) File.WriteAllBytes(Path.Combine(folder, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(remoteId, i, 300));
|
|
return folder;
|
|
}
|
|
|
|
/// <summary>A live lease on a project folder, as another Chronos working on the same workspace holds it.</summary>
|
|
public static LeaseFile Hold(string projectFolder) =>
|
|
LeaseFile.TryAcquire(projectFolder, ".lock", "workspace@other", TimeSpan.FromMinutes(5)) ?? throw new InvalidOperationException("The lease is held.");
|
|
|
|
/// <summary>
|
|
/// The SHA-256 of every file under <paramref name="root"/> by its name relative to the project or entry it belongs to,
|
|
/// whatever home folder holds it: the same before and after a sorting that only renames (the test-only proof).
|
|
/// </summary>
|
|
public static SortedDictionary<string, string> Manifest(string root)
|
|
{
|
|
var manifest = new SortedDictionary<string, string>(StringComparer.Ordinal);
|
|
if (!Directory.Exists(root)) return manifest;
|
|
foreach (var file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories))
|
|
{
|
|
var relative = Path.GetRelativePath(root, file).Replace('\\', '/');
|
|
var name = Path.GetFileName(file);
|
|
if (name is WorkspaceStore.ManifestFile or "server.json" && !relative.Contains('/', StringComparison.Ordinal)) continue;
|
|
if (name is WorkspaceStore.ManifestFile or "server.json" && IsHomeLevel(relative)) continue;
|
|
manifest[Strip(relative)] = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(file))) + "|" + new FileInfo(file).Length;
|
|
}
|
|
return manifest;
|
|
}
|
|
|
|
/// <summary>A path without the home folders it lies in (<c>local/</c>, <c>servers/<folder>/</c>).</summary>
|
|
private static string Strip(string relative)
|
|
{
|
|
if (relative.StartsWith("local/", StringComparison.Ordinal)) return relative["local/".Length..];
|
|
if (relative.StartsWith("servers/", StringComparison.Ordinal))
|
|
{
|
|
var rest = relative["servers/".Length..];
|
|
var slash = rest.IndexOf('/', StringComparison.Ordinal);
|
|
return slash < 0 ? rest : rest[(slash + 1)..];
|
|
}
|
|
return relative;
|
|
}
|
|
|
|
private static bool IsHomeLevel(string relative) =>
|
|
relative.Split('/') is { Length: 2 } parts && parts[0] == "local" || relative.Split('/') is { Length: 3 } more && more[0] == "servers";
|
|
|
|
private static byte[] Bytes(Guid seed, int index, int length)
|
|
{
|
|
var bytes = new byte[length];
|
|
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(seed.ToString("N") + index));
|
|
for (var i = 0; i < length; i++) bytes[i] = hash[i % hash.Length];
|
|
return bytes;
|
|
}
|
|
}
|