Compare commits
1
Commits
next
...
c52c9be180
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c52c9be180 |
@@ -492,6 +492,20 @@ failure still exits non-zero.
|
|||||||
<name>.json collection metadata + file list
|
<name>.json collection metadata + file list
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A collection's directory and JSON are named after the collection, and a symlink
|
||||||
|
after the file's title, both with unsafe characters replaced. When two
|
||||||
|
collections would get the same name, or two files in one collection the same
|
||||||
|
title (ignoring case in both), each of them gets its ID added: two albums named
|
||||||
|
`Trip` become `Trip (10)/` and `Trip (11)/`, and two files titled `IMG_0001.JPG`
|
||||||
|
become `IMG_0001 (12345).JPG` and `IMG_0001 (12346).JPG`. IDs never change, so a
|
||||||
|
name stays the same from run to run until such a clash appears or goes away.
|
||||||
|
|
||||||
|
Each run removes the symlinks into `originals/` that no longer belong in their
|
||||||
|
collection's directory, and the directories (and JSON) of collections that were
|
||||||
|
deleted or renamed. Nothing else in `collections/` is touched: a file or a
|
||||||
|
symlink you put there stays, and a directory that still holds one after its
|
||||||
|
symlinks are removed stays too, with its JSON.
|
||||||
|
|
||||||
Each file is downloaded exactly once regardless of how many collections it
|
Each file is downloaded exactly once regardless of how many collections it
|
||||||
appears in. On subsequent runs, existing originals are skipped. If a download
|
appears in. On subsequent runs, existing originals are skipped. If a download
|
||||||
fails, the error is logged and the backup continues with the next file. The exit
|
fails, the error is logged and the backup continues with the next file. The exit
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ Tag v1.0.0.
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
|
- 2026-09-23: Fixed the backup's per-collection folders (issue 103). Two files
|
||||||
|
in one collection with the same title, and two collections with the same name,
|
||||||
|
each get their ID added to the name (`IMG_0001 (12345).JPG`, `Trip (10)/`), so
|
||||||
|
none replaces another's symlink or JSON. Each run removes symlinks into
|
||||||
|
`originals/` for files no longer in the collection, and the folders of deleted
|
||||||
|
or renamed collections, leaving anything else in `collections/` alone. The
|
||||||
|
README backup layout states the naming rule.
|
||||||
- 2026-09-23: Single-sourced the version string (issue 5). `package.json` is the
|
- 2026-09-23: Single-sourced the version string (issue 5). `package.json` is the
|
||||||
only place it is written: `src/index.ts` imports it for `VERSION` and
|
only place it is written: `src/index.ts` imports it for `VERSION` and
|
||||||
`bin/quak.ts` passes `VERSION` to commander. tsc copies `package.json` to
|
`bin/quak.ts` passes `VERSION` to commander. tsc copies `package.json` to
|
||||||
|
|||||||
+124
-11
@@ -17,7 +17,9 @@
|
|||||||
// temp-then-rename, so a file that exists is whole and is never re-fetched — an
|
// temp-then-rename, so a file that exists is whole and is never re-fetched — an
|
||||||
// interrupted run resumes by listing the directory. The derived views hold no
|
// interrupted run resumes by listing the directory. The derived views hold no
|
||||||
// unique state, so they are rebuilt every run; that repairs stale sidecars and
|
// unique state, so they are rebuilt every run; that repairs stale sidecars and
|
||||||
// missing or broken symlinks left by an earlier crash.
|
// missing or broken symlinks left by an earlier crash. A rebuild also removes
|
||||||
|
// the symlinks into originals/ that no longer belong to an album, and the
|
||||||
|
// directories of albums that no longer exist.
|
||||||
//
|
//
|
||||||
// Resilience (issue #8): no per-file condition aborts the run. A failed
|
// Resilience (issue #8): no per-file condition aborts the run. A failed
|
||||||
// download or a failed symlink is caught, recorded in `failures.json` with a
|
// download or a failed symlink is caught, recorded in `failures.json` with a
|
||||||
@@ -34,13 +36,14 @@ import {
|
|||||||
readdirSync,
|
readdirSync,
|
||||||
readFileSync,
|
readFileSync,
|
||||||
readlinkSync,
|
readlinkSync,
|
||||||
|
rmdirSync,
|
||||||
rmSync,
|
rmSync,
|
||||||
statSync,
|
statSync,
|
||||||
symlinkSync,
|
symlinkSync,
|
||||||
writeFileSync,
|
writeFileSync,
|
||||||
} from "node:fs";
|
} from "node:fs";
|
||||||
import { copyFile, rename, rm } from "node:fs/promises";
|
import { copyFile, rename, rm } from "node:fs/promises";
|
||||||
import { basename, dirname, join, relative } from "node:path";
|
import { basename, dirname, extname, join, relative } from "node:path";
|
||||||
|
|
||||||
import { fsyncPath } from "./download/index.js";
|
import { fsyncPath } from "./download/index.js";
|
||||||
import { safeExtension, sanitizeFileName } from "./filename.js";
|
import { safeExtension, sanitizeFileName } from "./filename.js";
|
||||||
@@ -227,6 +230,83 @@ const rebuildSymlink = (linkPath: string, target: string): void => {
|
|||||||
symlinkSync(target, linkPath);
|
symlinkSync(target, linkPath);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The on-disk names for the entries of one directory, keyed by ID. Each name
|
||||||
|
// is used as is unless another entry has the same name, ignoring case (two
|
||||||
|
// names that differ only in case are one entry on a case-insensitive file
|
||||||
|
// system); then every entry sharing it gets ` (<id>)`, before the extension
|
||||||
|
// when `beforeExtension` is set. IDs are stable, so the names are too.
|
||||||
|
const namesByID = (
|
||||||
|
entries: { id: number; name: string }[],
|
||||||
|
beforeExtension: boolean,
|
||||||
|
): Map<number, string> => {
|
||||||
|
const counts = new Map<string, number>();
|
||||||
|
for (const { name } of entries) {
|
||||||
|
const key = name.toLowerCase();
|
||||||
|
counts.set(key, (counts.get(key) ?? 0) + 1);
|
||||||
|
}
|
||||||
|
const names = new Map<number, string>();
|
||||||
|
for (const { id, name } of entries) {
|
||||||
|
if (counts.get(name.toLowerCase()) === 1) {
|
||||||
|
names.set(id, name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const ext = beforeExtension ? extname(name) : "";
|
||||||
|
const stem = name.slice(0, name.length - ext.length);
|
||||||
|
names.set(id, `${stem} (${id})${ext}`);
|
||||||
|
}
|
||||||
|
return names;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove the symlinks in the album directory `dir` that point into
|
||||||
|
// `originalsDir` and are not named in `keep`. Nothing else in the directory
|
||||||
|
// is touched: anything else there was put there by the user.
|
||||||
|
const removeStaleLinks = (
|
||||||
|
dir: string,
|
||||||
|
keep: Set<string>,
|
||||||
|
originalsDir: string,
|
||||||
|
): void => {
|
||||||
|
const target = relative(dir, originalsDir);
|
||||||
|
for (const name of readdirSync(dir)) {
|
||||||
|
if (keep.has(name)) continue;
|
||||||
|
const path = join(dir, name);
|
||||||
|
if (
|
||||||
|
lstatSync(path).isSymbolicLink() &&
|
||||||
|
dirname(readlinkSync(path)) === target
|
||||||
|
) {
|
||||||
|
rmSync(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove the directories under `collectionsDir` that an earlier run wrote for
|
||||||
|
// an album that is gone or renamed: a directory not named in `current` with a
|
||||||
|
// `<name>.json` beside it holding an album ID, which is what a run writes. Its
|
||||||
|
// symlinks into originals/ are removed; if that leaves it empty, it and its
|
||||||
|
// JSON are deleted, otherwise both stay for what the user put there.
|
||||||
|
const removeStaleAlbumDirs = (
|
||||||
|
collectionsDir: string,
|
||||||
|
current: Set<string>,
|
||||||
|
originalsDir: string,
|
||||||
|
): void => {
|
||||||
|
for (const entry of readdirSync(collectionsDir, { withFileTypes: true })) {
|
||||||
|
if (!entry.isDirectory() || current.has(entry.name)) continue;
|
||||||
|
const jsonPath = join(collectionsDir, `${entry.name}.json`);
|
||||||
|
try {
|
||||||
|
const album = JSON.parse(readFileSync(jsonPath, "utf-8")) as {
|
||||||
|
id?: unknown;
|
||||||
|
};
|
||||||
|
if (typeof album.id !== "number") continue;
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const dir = join(collectionsDir, entry.name);
|
||||||
|
removeStaleLinks(dir, new Set(), originalsDir);
|
||||||
|
if (readdirSync(dir).length > 0) continue;
|
||||||
|
rmdirSync(dir);
|
||||||
|
rmSync(jsonPath);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const loadLedger = (path: string): Map<number, FailureEntry> => {
|
const loadLedger = (path: string): Map<number, FailureEntry> => {
|
||||||
const ledger = new Map<number, FailureEntry>();
|
const ledger = new Map<number, FailureEntry>();
|
||||||
try {
|
try {
|
||||||
@@ -303,9 +383,10 @@ export const runBackup = async (
|
|||||||
|
|
||||||
// Collections in scope, and the distinct files across them (a file shared
|
// Collections in scope, and the distinct files across them (a file shared
|
||||||
// by two albums is one original).
|
// by two albums is one original).
|
||||||
const collections = lib
|
const allCollections = lib.listCollections();
|
||||||
.listCollections()
|
const collections = allCollections.filter((c) =>
|
||||||
.filter((c) => (only ? only.has(c.name) : true));
|
only ? only.has(c.name) : true,
|
||||||
|
);
|
||||||
const collectionName = new Map<number, string>();
|
const collectionName = new Map<number, string>();
|
||||||
for (const c of collections) collectionName.set(c.id, c.name);
|
for (const c of collections) collectionName.set(c.id, c.name);
|
||||||
|
|
||||||
@@ -406,23 +487,55 @@ export const runBackup = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then the per-collection symlink trees and JSON.
|
// Then the per-collection symlink trees and JSON. Directory names are
|
||||||
|
// chosen across every album, not just those in scope, so a scoped run
|
||||||
|
// names an album the same as a full one and never takes the directory of
|
||||||
|
// an album it skipped. Stale entries are removed before anything is
|
||||||
|
// rebuilt, so on a case-insensitive file system removing an old name can
|
||||||
|
// never remove the new one.
|
||||||
|
const albumDirNames = namesByID(
|
||||||
|
allCollections.map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
name: sanitizeFileName(c.name, `collection-${c.id}`),
|
||||||
|
})),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
removeStaleAlbumDirs(
|
||||||
|
collectionsDir,
|
||||||
|
new Set(albumDirNames.values()),
|
||||||
|
originalsDir,
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
log(`FAILED removing old album directories: ${errorMessage(err)}`);
|
||||||
|
}
|
||||||
|
|
||||||
for (const c of collections) {
|
for (const c of collections) {
|
||||||
const colDirName = sanitizeFileName(c.name, `collection-${c.id}`);
|
const colDirName = albumDirNames.get(c.id)!;
|
||||||
const colDir = join(collectionsDir, colDirName);
|
const colDir = join(collectionsDir, colDirName);
|
||||||
mkdirSync(colDir, { recursive: true });
|
mkdirSync(colDir, { recursive: true });
|
||||||
|
|
||||||
const files = filesByCollection.get(c.id) ?? [];
|
const files = filesByCollection.get(c.id) ?? [];
|
||||||
|
const linkNames = namesByID(
|
||||||
|
files.map((f) => ({
|
||||||
|
id: f.id,
|
||||||
|
name: sanitizeFileName(f.metadata.title, `file-${f.id}`),
|
||||||
|
})),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
removeStaleLinks(colDir, new Set(linkNames.values()), originalsDir);
|
||||||
|
} catch (err) {
|
||||||
|
log(`FAILED removing old links in ${c.name}: ${errorMessage(err)}`);
|
||||||
|
}
|
||||||
|
|
||||||
const metaFiles: { id: number; metadata: EnteFile["metadata"] }[] = [];
|
const metaFiles: { id: number; metadata: EnteFile["metadata"] }[] = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
metaFiles.push({ id: file.id, metadata: file.metadata });
|
metaFiles.push({ id: file.id, metadata: file.metadata });
|
||||||
if (!includeOriginals) continue;
|
if (!includeOriginals) continue;
|
||||||
const orig = join(originalsDir, originalName(file));
|
const orig = join(originalsDir, originalName(file));
|
||||||
if (!isPresent(orig)) continue;
|
if (!isPresent(orig)) continue;
|
||||||
const linkName = sanitizeFileName(
|
const linkName = linkNames.get(file.id)!;
|
||||||
file.metadata.title,
|
|
||||||
`file-${file.id}`,
|
|
||||||
);
|
|
||||||
const linkPath = join(colDir, linkName);
|
const linkPath = join(colDir, linkName);
|
||||||
try {
|
try {
|
||||||
rebuildSymlink(linkPath, relative(colDir, orig));
|
rebuildSymlink(linkPath, relative(colDir, orig));
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import {
|
|||||||
readFileSync,
|
readFileSync,
|
||||||
readlinkSync,
|
readlinkSync,
|
||||||
rmSync,
|
rmSync,
|
||||||
|
symlinkSync,
|
||||||
writeFileSync,
|
writeFileSync,
|
||||||
} from "node:fs";
|
} from "node:fs";
|
||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
@@ -47,6 +48,7 @@ import { join } from "node:path";
|
|||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||||||
|
|
||||||
|
import { runBackup, type BackupLibrary } from "../../src/backup.js";
|
||||||
import { Library } from "../../src/library/index.js";
|
import { Library } from "../../src/library/index.js";
|
||||||
import type { ContentSource } from "../../src/library/content.js";
|
import type { ContentSource } from "../../src/library/content.js";
|
||||||
import type { CollectionsPage, FilesPage } from "../../src/client.js";
|
import type { CollectionsPage, FilesPage } from "../../src/client.js";
|
||||||
@@ -622,3 +624,172 @@ describe("lib.backup", () => {
|
|||||||
lib.close();
|
lib.close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The album folders under collections/, driven through `runBackup` with a
|
||||||
|
// stand-in library whose albums a test changes between runs.
|
||||||
|
describe("backup album folders", () => {
|
||||||
|
interface Album {
|
||||||
|
collection: Collection;
|
||||||
|
files: EnteFile[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const libraryOf = (albums: Album[]): BackupLibrary => ({
|
||||||
|
refresh: async () => {},
|
||||||
|
listCollections: () => albums.map((a) => a.collection),
|
||||||
|
listFiles: (id) =>
|
||||||
|
albums.find((a) => a.collection.id === id)?.files ?? [],
|
||||||
|
original: async (fileID) => {
|
||||||
|
const path = join(root, `source-${fileID}`);
|
||||||
|
writeFileSync(path, `original ${fileID}`);
|
||||||
|
return { path };
|
||||||
|
},
|
||||||
|
thumbnail: async () => {
|
||||||
|
throw new Error("no thumbnails in this stand-in");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Every entry under collections/, one level of directories deep, with each
|
||||||
|
// symlink's target.
|
||||||
|
const tree = (outDir: string): string[] => {
|
||||||
|
const lines: string[] = [];
|
||||||
|
const list = (dir: string, prefix: string): void => {
|
||||||
|
for (const name of readdirSync(dir).sort()) {
|
||||||
|
const path = join(dir, name);
|
||||||
|
const st = lstatSync(path);
|
||||||
|
if (st.isSymbolicLink()) {
|
||||||
|
lines.push(`${prefix}${name} -> ${readlinkSync(path)}`);
|
||||||
|
} else if (st.isDirectory() && prefix === "") {
|
||||||
|
lines.push(`${name}/`);
|
||||||
|
list(path, `${name}/`);
|
||||||
|
} else {
|
||||||
|
lines.push(`${prefix}${name}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
list(join(outDir, "collections"), "");
|
||||||
|
return lines;
|
||||||
|
};
|
||||||
|
|
||||||
|
const albumID = (outDir: string, jsonName: string): number =>
|
||||||
|
JSON.parse(readFileSync(join(outDir, "collections", jsonName), "utf-8"))
|
||||||
|
.id;
|
||||||
|
|
||||||
|
it("gives every file and every album its own name when names repeat", async () => {
|
||||||
|
const outDir = join(root, "backup");
|
||||||
|
const lib = libraryOf([
|
||||||
|
{
|
||||||
|
collection: collection(10, "Trip"),
|
||||||
|
files: [
|
||||||
|
file(1, 10, "IMG_0001.JPG"),
|
||||||
|
file(2, 10, "IMG_0001.JPG"),
|
||||||
|
file(4, 10, "img_0001.jpg"),
|
||||||
|
file(3, 10, "other.jpg"),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: collection(11, "Trip"),
|
||||||
|
files: [file(3, 11, "other.jpg")],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const result = await runBackup(lib, { downloadDirectory: outDir });
|
||||||
|
|
||||||
|
expect(result.failed).toBe(0);
|
||||||
|
expect(tree(outDir)).toEqual([
|
||||||
|
"Trip (10)/",
|
||||||
|
"Trip (10)/IMG_0001 (1).JPG -> ../../originals/1.JPG",
|
||||||
|
"Trip (10)/IMG_0001 (2).JPG -> ../../originals/2.JPG",
|
||||||
|
"Trip (10)/img_0001 (4).jpg -> ../../originals/4.jpg",
|
||||||
|
"Trip (10)/other.jpg -> ../../originals/3.jpg",
|
||||||
|
"Trip (10).json",
|
||||||
|
"Trip (11)/",
|
||||||
|
"Trip (11)/other.jpg -> ../../originals/3.jpg",
|
||||||
|
"Trip (11).json",
|
||||||
|
]);
|
||||||
|
expect(albumID(outDir, "Trip (10).json")).toBe(10);
|
||||||
|
expect(albumID(outDir, "Trip (11).json")).toBe(11);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("changes nothing on a second run over an unchanged account", async () => {
|
||||||
|
const outDir = join(root, "backup");
|
||||||
|
const lib = libraryOf([
|
||||||
|
{
|
||||||
|
collection: collection(10, "Trip"),
|
||||||
|
files: [
|
||||||
|
file(1, 10, "IMG_0001.JPG"),
|
||||||
|
file(2, 10, "IMG_0001.JPG"),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: collection(11, "Trip"),
|
||||||
|
files: [file(3, 11, "other.jpg")],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
await runBackup(lib, { downloadDirectory: outDir });
|
||||||
|
const before = tree(outDir);
|
||||||
|
const second = await runBackup(lib, { downloadDirectory: outDir });
|
||||||
|
|
||||||
|
expect(second.downloaded).toBe(0);
|
||||||
|
expect(second.failed).toBe(0);
|
||||||
|
expect(tree(outDir)).toEqual(before);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes links and album folders that are gone, and nothing the user added", async () => {
|
||||||
|
const outDir = join(root, "backup");
|
||||||
|
const albums: Album[] = [
|
||||||
|
{
|
||||||
|
collection: collection(10, "Trip"),
|
||||||
|
files: [
|
||||||
|
file(1, 10, "IMG_0001.JPG"),
|
||||||
|
file(2, 10, "IMG_0001.JPG"),
|
||||||
|
file(3, 10, "other.jpg"),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: collection(12, "Work"),
|
||||||
|
files: [file(5, 12, "a.jpg")],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
collection: collection(13, "Old"),
|
||||||
|
files: [file(5, 13, "a.jpg")],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const lib = libraryOf(albums);
|
||||||
|
await runBackup(lib, { downloadDirectory: outDir });
|
||||||
|
|
||||||
|
// What the user put in the tree: a note and a symlink of their own in
|
||||||
|
// an album, a note in an album about to be renamed, and a folder quak
|
||||||
|
// did not create.
|
||||||
|
const collectionsDir = join(outDir, "collections");
|
||||||
|
writeFileSync(join(collectionsDir, "Trip", "notes.txt"), "mine");
|
||||||
|
symlinkSync("../elsewhere", join(collectionsDir, "Trip", "mine"));
|
||||||
|
writeFileSync(join(collectionsDir, "Work", "keep.txt"), "mine");
|
||||||
|
mkdirSync(join(collectionsDir, "Mine"));
|
||||||
|
writeFileSync(join(collectionsDir, "Mine", "keep.txt"), "mine");
|
||||||
|
|
||||||
|
// File 2 leaves Trip, Work is renamed Office, Old is deleted.
|
||||||
|
albums[0]!.files.splice(1, 1);
|
||||||
|
albums[1]!.collection = collection(12, "Office");
|
||||||
|
albums.splice(2, 1);
|
||||||
|
const result = await runBackup(lib, { downloadDirectory: outDir });
|
||||||
|
|
||||||
|
expect(result.failed).toBe(0);
|
||||||
|
expect(tree(outDir)).toEqual([
|
||||||
|
"Mine/",
|
||||||
|
"Mine/keep.txt",
|
||||||
|
"Office/",
|
||||||
|
"Office/a.jpg -> ../../originals/5.jpg",
|
||||||
|
"Office.json",
|
||||||
|
"Trip/",
|
||||||
|
"Trip/IMG_0001.JPG -> ../../originals/1.JPG",
|
||||||
|
"Trip/mine -> ../elsewhere",
|
||||||
|
"Trip/notes.txt",
|
||||||
|
"Trip/other.jpg -> ../../originals/3.jpg",
|
||||||
|
"Trip.json",
|
||||||
|
"Work/",
|
||||||
|
"Work/keep.txt",
|
||||||
|
"Work.json",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user