Редактор: превью с поворотом, проекты, безопасное сохранение zip, dev-меню
RotatedImage: размер контейнера через clientWidth/Height (не getBoundingClientRect), чтобы cover при 90°/270° работал под zoom React Flow; убраны отладочные логи. Главное меню в dev: пункт «Вид» с DevTools (Ctrl+Shift+I без пустого application menu). Список проектов: project.list без лицензии; список подгружается при неактивной лицензии; ProjectPicker с подсказками; listProjects пропускает битые zip. Сохранение проектов: atomicReplace — замена zip без rm до commit; восстановление *.dnd.zip.tmp при старте; тесты. EditorApp: блокировка UI при открытых окнах презентации и пульта; стили оверлея. Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import crypto from 'node:crypto';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
import { readProjectJsonFromZip } from './yauzlProjectZip';
|
||||
|
||||
/**
|
||||
* Подменяет файл `finalPath` готовым `completedSrc` (обычно `*.dnd.zip.tmp`).
|
||||
* Нельзя сначала удалять `finalPath`: при сбое rename после rm проект теряется (Windows/антивирус).
|
||||
*/
|
||||
export async function replaceFileAtomic(completedSrc: string, finalPath: string): Promise<void> {
|
||||
if (completedSrc === finalPath) return;
|
||||
|
||||
const stSrc = await fs.stat(completedSrc).catch(() => null);
|
||||
if (!stSrc?.isFile() || stSrc.size === 0) {
|
||||
throw new Error(`replaceFileAtomic: нет или пустой источник: ${completedSrc}`);
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.rename(completedSrc, finalPath);
|
||||
return;
|
||||
} catch (first: unknown) {
|
||||
const c = (first as NodeJS.ErrnoException).code;
|
||||
if (c === 'EXDEV') {
|
||||
await fs.copyFile(completedSrc, finalPath);
|
||||
await fs.unlink(completedSrc).catch(() => undefined);
|
||||
return;
|
||||
}
|
||||
// Windows: чаще всего EEXIST — целевой файл есть, rename не перезаписывает; идём через backup ниже.
|
||||
}
|
||||
|
||||
const stFinal = await fs.stat(finalPath).catch(() => null);
|
||||
if (!stFinal?.isFile()) {
|
||||
try {
|
||||
await fs.rename(completedSrc, finalPath);
|
||||
return;
|
||||
} catch (again: unknown) {
|
||||
throw again instanceof Error ? again : new Error(String(again));
|
||||
}
|
||||
}
|
||||
|
||||
const backupPath = `${finalPath}.bak.${Date.now().toString(36)}_${crypto.randomBytes(4).toString('hex')}`;
|
||||
await fs.rename(finalPath, backupPath);
|
||||
try {
|
||||
await fs.rename(completedSrc, finalPath);
|
||||
} catch (place: unknown) {
|
||||
await fs.rename(backupPath, finalPath).catch(() => undefined);
|
||||
throw place instanceof Error ? place : new Error(String(place));
|
||||
}
|
||||
await fs.unlink(backupPath).catch(() => undefined);
|
||||
}
|
||||
|
||||
/** Если сохранение оборвалось, остаётся только `*.dnd.zip.tmp` — восстанавливаем в `*.dnd.zip`. */
|
||||
export async function recoverOrphanDndZipTmpInRoot(root: string): Promise<void> {
|
||||
let names: string[];
|
||||
try {
|
||||
names = await fs.readdir(root);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
for (const name of names) {
|
||||
if (!name.endsWith('.dnd.zip.tmp')) continue;
|
||||
const tmpPath = path.join(root, name);
|
||||
const finalName = name.slice(0, -'.tmp'.length);
|
||||
if (!finalName.endsWith('.dnd.zip')) continue;
|
||||
const finalPath = path.join(root, finalName);
|
||||
try {
|
||||
await fs.access(finalPath);
|
||||
continue;
|
||||
} catch {
|
||||
/* final отсутствует — пробуем поднять из tmp */
|
||||
}
|
||||
try {
|
||||
const st = await fs.stat(tmpPath);
|
||||
if (!st.isFile() || st.size < 22) continue;
|
||||
await readProjectJsonFromZip(tmpPath);
|
||||
await fs.rename(tmpPath, finalPath);
|
||||
} catch {
|
||||
/* битый tmp не трогаем */
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user