release: v0.16.0
See CHANGELOG.md for the full v0.16.0 entry. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: ralksta <ralksta@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Ralf <rohlenbo79@gmail.com> Co-authored-by: Paul Thies <110185070+ImScheinox@users.noreply.github.com> Co-authored-by: lancetm714 <108419425+lancetm714@users.noreply.github.com>
This commit is contained in:
@@ -6,14 +6,12 @@
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import yaml from 'js-yaml';
|
||||
import { atomicWrite } from '../atomicWrite';
|
||||
import type { GalleryYaml, SettingsYaml } from '../config/schema';
|
||||
|
||||
const CONTENT_DIR = path.join(process.cwd(), 'content');
|
||||
const MAX_BACKUPS = 10; // Keep last 10 backups per file
|
||||
|
||||
/** Disambiguates temp files within one process; the pid covers across them. */
|
||||
let tmpCounter = 0;
|
||||
|
||||
/** Read gallery.yaml and return parsed content. */
|
||||
export async function readGalleryYaml(): Promise<GalleryYaml | null> {
|
||||
try {
|
||||
@@ -43,9 +41,21 @@ async function writeYamlFile(filename: string, data: unknown): Promise<void> {
|
||||
// Ensure content directory exists
|
||||
await fs.mkdir(CONTENT_DIR, { recursive: true });
|
||||
|
||||
// Create backup if file exists
|
||||
// "No file yet" and "the backup could not be written" used to share one
|
||||
// catch, so a `.backups/` a save couldn't write to (owned by root after a
|
||||
// first start as root, say) looked exactly like a brand-new file: the save
|
||||
// went ahead with no snapshot taken (#630). Only ENOENT means there is
|
||||
// nothing to back up; anything else aborts the save before it overwrites
|
||||
// the live file.
|
||||
let fileExists = true;
|
||||
try {
|
||||
await fs.access(filePath);
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err;
|
||||
fileExists = false;
|
||||
}
|
||||
|
||||
if (fileExists) {
|
||||
const backupDir = path.join(CONTENT_DIR, '.backups');
|
||||
await fs.mkdir(backupDir, { recursive: true });
|
||||
|
||||
@@ -55,8 +65,6 @@ async function writeYamlFile(filename: string, data: unknown): Promise<void> {
|
||||
|
||||
// Prune old backups
|
||||
await pruneBackups(backupDir, filename);
|
||||
} catch {
|
||||
// File doesn't exist yet, no backup needed
|
||||
}
|
||||
|
||||
// Generate YAML content with header comment
|
||||
@@ -74,23 +82,7 @@ async function writeYamlFile(filename: string, data: unknown): Promise<void> {
|
||||
sortKeys: false,
|
||||
});
|
||||
|
||||
// Atomic write: write to a *unique* temp file, then rename.
|
||||
//
|
||||
// The rename is what makes this atomic; the temp filename is what did not.
|
||||
// With a constant `${filePath}.tmp`, two saves of the same file in flight at
|
||||
// once shared it — the second writeFile could interleave with the first's
|
||||
// rename, leaving one save's bytes published under the other's, or a
|
||||
// truncated mix. A double-clicked Save in the page builder is enough.
|
||||
const tmpPath = `${filePath}.${process.pid}.${++tmpCounter}.tmp`;
|
||||
try {
|
||||
await fs.writeFile(tmpPath, content, 'utf8');
|
||||
await fs.rename(tmpPath, filePath);
|
||||
} catch (err) {
|
||||
// Do not leave litter in content/ behind a failed save. Cleanup failure is
|
||||
// not worth masking the real error with.
|
||||
await fs.unlink(tmpPath).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
await atomicWrite(filePath, content);
|
||||
|
||||
console.log(`[Admin] ✅ Saved ${filename}`);
|
||||
}
|
||||
@@ -148,31 +140,63 @@ export async function restoreBackup(backupFilename: string): Promise<void> {
|
||||
const originalFilename = match[1];
|
||||
const targetPath = path.join(CONTENT_DIR, originalFilename);
|
||||
|
||||
// Create a backup of current state first
|
||||
// Read before touching anything, so a missing or unreadable backup changes
|
||||
// nothing on disk.
|
||||
const content = await fs.readFile(backupPath);
|
||||
|
||||
// Create a backup of current state first. A failed safety copy aborts the
|
||||
// restore rather than being swallowed: `fs.copyFile` below would otherwise
|
||||
// truncate `targetPath` before writing it, so on a full disk or a container
|
||||
// killed mid-copy the live file could be lost with no snapshot to fall back
|
||||
// on (#630). Only ENOENT — there is no current file to snapshot, which is
|
||||
// the normal case for a deleted entry — is not an abort.
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const preRestoreBackup = `${originalFilename}.${timestamp}.pre-restore.bak`;
|
||||
try {
|
||||
await fs.copyFile(targetPath, path.join(backupDir, preRestoreBackup));
|
||||
} catch {
|
||||
// Original might not exist
|
||||
// Bound them here rather than waiting for the next save: restoring twice in
|
||||
// a row is exactly when these pile up, and a save may be a long way off.
|
||||
await pruneBackups(backupDir, originalFilename);
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err;
|
||||
}
|
||||
|
||||
await fs.copyFile(backupPath, targetPath);
|
||||
// atomicWrite rather than fs.copyFile: a copy truncates the destination
|
||||
// before writing, so a failure partway through (full disk, killed
|
||||
// container) leaves targetPath empty. Temp-file-then-rename means the old
|
||||
// content stays in place until the new content is fully on disk.
|
||||
await atomicWrite(targetPath, content);
|
||||
console.log(`[Admin] 🔄 Restored ${originalFilename} from ${backupFilename}`);
|
||||
}
|
||||
|
||||
/** Remove old backups, keeping only MAX_BACKUPS most recent. */
|
||||
/**
|
||||
* Remove old backups, keeping the newest MAX_BACKUPS of each kind.
|
||||
*
|
||||
* The two kinds are counted separately on purpose: ten ordinary saves must not
|
||||
* push out the snapshot taken just before a restore, which is the only way back
|
||||
* from one. That is why pre-restore files were exempt from pruning.
|
||||
*
|
||||
* Exempting them entirely was the bug, though. listBackups() returns them and
|
||||
* the modal lists them, so every restore added a row that was never removed
|
||||
* until the useful backups were off the end of the list. A cap of their own
|
||||
* keeps the undo without letting it crowd out everything else.
|
||||
*/
|
||||
async function pruneBackups(backupDir: string, filename: string): Promise<void> {
|
||||
try {
|
||||
const files = await fs.readdir(backupDir);
|
||||
const relevant = files
|
||||
.filter((f) => f.startsWith(filename) && !f.includes('pre-restore'))
|
||||
.sort();
|
||||
|
||||
if (relevant.length > MAX_BACKUPS) {
|
||||
const toDelete = relevant.slice(0, relevant.length - MAX_BACKUPS);
|
||||
for (const f of toDelete) {
|
||||
await fs.unlink(path.join(backupDir, f));
|
||||
const saves: string[] = [];
|
||||
const preRestores: string[] = [];
|
||||
for (const f of files.filter((f) => f.startsWith(filename))) {
|
||||
(f.includes('pre-restore') ? preRestores : saves).push(f);
|
||||
}
|
||||
|
||||
// The timestamp is fixed-width and lexically ordered, so sorting by name
|
||||
// sorts by age.
|
||||
for (const group of [saves, preRestores]) {
|
||||
group.sort();
|
||||
for (const old of group.slice(0, Math.max(0, group.length - MAX_BACKUPS))) {
|
||||
await fs.unlink(path.join(backupDir, old));
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user