mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(cli): a cloud upload leaves out a file it cannot read instead of failing (#8575)
* fix(cli): a cloud upload leaves out a file it cannot read instead of failing * docs(cli): list the unreadable left-out reason and its cause
This commit is contained in:
+10
-1
@@ -2961,11 +2961,20 @@ same share as the project is copied like any other. To include such a
|
||||
file, move it into the project folder. Under `--json`, the result lists those
|
||||
as `left_out`, each with the `file` that points at it, the `reference` as
|
||||
written, its `path` from the project folder, the `reason` (`blocklisted`,
|
||||
`received`, `file_type`, `private`, `network_share` or `not_approved`) and `levels_up`, how many folders above the project
|
||||
`received`, `file_type`, `private`, `network_share`, `script`, `not_approved` or `unreadable`) and `levels_up`, how many folders above the project
|
||||
it is. A full run also prints each as a `{"event":"left_out",…}`
|
||||
line on stderr, so a wrapper can ask with `--dry-run --json` before uploading
|
||||
and still hear about each one during the run.
|
||||
|
||||
A file or folder in the project that cannot be read is left out the same way
|
||||
instead of stopping the upload, with a warning saying why. Its entry has the
|
||||
reason `unreadable` and a `cause`: `locked` (another app has it open), `denied`
|
||||
(the computer does not allow reading it), `missing` (it was deleted, or is a
|
||||
link to a file that is gone) or `drive` (its drive could not read it). When a
|
||||
page the video plays uses that file, a second entry names that page as `file`,
|
||||
so a wrapper can ask before uploading a video that would miss it. The page the
|
||||
video starts from must be readable: if it is not, the command stops and names it.
|
||||
|
||||
Which files came in with an imported project is kept in the project's
|
||||
`.hyperframes/received-files.json`. A project without that file is read as
|
||||
having received nothing, which suits projects you made yourself. An app that
|
||||
|
||||
@@ -14,6 +14,8 @@ export type { LeftOutReason };
|
||||
export const leftOutWarning = (file: string, reference: string, reason: LeftOutReason): string =>
|
||||
terminalText(leftOutText(file, reference, reason));
|
||||
|
||||
export type UnreadableCause = "locked" | "denied" | "missing" | "drive";
|
||||
|
||||
/** One file an upload left out. `path` is from the project folder; `levels_up` is null on another drive. */
|
||||
export interface LeftOutReference {
|
||||
file: string;
|
||||
@@ -21,6 +23,7 @@ export interface LeftOutReference {
|
||||
path: string;
|
||||
reason: LeftOutReason;
|
||||
levels_up: number | null;
|
||||
cause?: UnreadableCause;
|
||||
}
|
||||
|
||||
export type OnLeftOut = (warning: string, reference: LeftOutReference) => void;
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
existsSync,
|
||||
fstatSync,
|
||||
openSync,
|
||||
type Dirent,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
type Stats,
|
||||
@@ -40,6 +41,7 @@ import {
|
||||
type OnLeftOut,
|
||||
type OnOutsideFile,
|
||||
outsideFileReference,
|
||||
type UnreadableCause,
|
||||
} from "./leftOutReference.js";
|
||||
import { isCopiedOutsideFileType, isUploadedOutsideFileType } from "./outsideFileType.js";
|
||||
import {
|
||||
@@ -328,13 +330,57 @@ function shouldIgnoreSegment(segment: string): boolean {
|
||||
return IGNORED_DIRS.has(segment) || IGNORED_FILES.has(segment);
|
||||
}
|
||||
|
||||
const UNREADABLE_CODES = new Map<string, UnreadableCause>([
|
||||
["EBUSY", "locked"],
|
||||
["EPERM", "denied"],
|
||||
["EACCES", "denied"],
|
||||
["ENOENT", "missing"],
|
||||
["ELOOP", "missing"],
|
||||
["EIO", "drive"],
|
||||
["UNKNOWN", "drive"],
|
||||
]);
|
||||
|
||||
const UNREADABLE_WORDS: Record<UnreadableCause, string> = {
|
||||
locked: "another app has it open",
|
||||
denied: "this computer does not allow reading it",
|
||||
missing: "it was deleted or is a link to a file that is gone",
|
||||
drive: "its drive could not read it",
|
||||
};
|
||||
|
||||
/** A read the file system refused for one file, as its cause; any other error is thrown. */
|
||||
function readOrCause<T>(
|
||||
read: () => T,
|
||||
): { ok: true; value: T } | { ok: false; cause: UnreadableCause } {
|
||||
try {
|
||||
return { ok: true, value: read() };
|
||||
} catch (error) {
|
||||
const cause = UNREADABLE_CODES.get(String((error as NodeJS.ErrnoException)?.code));
|
||||
if (!cause) throw error;
|
||||
return { ok: false, cause };
|
||||
}
|
||||
}
|
||||
|
||||
/** The project's own folder must list; a folder inside it that will not is noted in `unreadable`. */
|
||||
function listFolder(
|
||||
rootDir: string,
|
||||
dir: string,
|
||||
unreadable: Map<string, UnreadableCause>,
|
||||
): Dirent[] {
|
||||
if (dir === rootDir) return readdirSync(dir, { withFileTypes: true });
|
||||
const listed = readOrCause(() => readdirSync(dir, { withFileTypes: true }));
|
||||
if (listed.ok) return listed.value;
|
||||
unreadable.set(archiveKey(relative(rootDir, dir).replaceAll("\\", "/")), listed.cause);
|
||||
return [];
|
||||
}
|
||||
|
||||
function collectProjectFiles(
|
||||
rootDir: string,
|
||||
currentDir: string,
|
||||
paths: string[],
|
||||
matcher: Ignore,
|
||||
unreadable: Map<string, UnreadableCause>,
|
||||
): void {
|
||||
for (const entry of readdirSync(currentDir, { withFileTypes: true })) {
|
||||
for (const entry of listFolder(rootDir, currentDir, unreadable)) {
|
||||
if (shouldIgnoreSegment(entry.name)) continue;
|
||||
const absolutePath = join(currentDir, entry.name);
|
||||
const relativePath = relative(rootDir, absolutePath).replaceAll("\\", "/");
|
||||
@@ -342,13 +388,46 @@ function collectProjectFiles(
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
if (matcher.ignores(`${relativePath}/`)) continue;
|
||||
collectProjectFiles(rootDir, absolutePath, paths, matcher);
|
||||
collectProjectFiles(rootDir, absolutePath, paths, matcher, unreadable);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!statSync(absolutePath).isFile()) continue;
|
||||
if (matcher.ignores(relativePath)) continue;
|
||||
paths.push(relativePath);
|
||||
const stat = readOrCause(() => statSync(absolutePath));
|
||||
if (!stat.ok) unreadable.set(archiveKey(relativePath), stat.cause);
|
||||
else if (stat.value.isFile()) paths.push(relativePath);
|
||||
}
|
||||
}
|
||||
|
||||
/** Why `rel`, or a folder it is in, could not be read; null when it could. */
|
||||
function unreadableAt(
|
||||
unreadable: ReadonlyMap<string, UnreadableCause>,
|
||||
rel: string,
|
||||
): UnreadableCause | null {
|
||||
for (let at = rel; at; at = at.includes("/") ? at.slice(0, at.lastIndexOf("/")) : "") {
|
||||
const cause = unreadable.get(at);
|
||||
if (cause) return cause;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The page the video starts from must be read; any other unreadable path is listed as left out. */
|
||||
function reportUnreadable(
|
||||
absProjectDir: string,
|
||||
unreadable: ReadonlyMap<string, UnreadableCause>,
|
||||
entry: string,
|
||||
onLeftOut: OnLeftOut | undefined,
|
||||
): void {
|
||||
for (const page of new Set([entry, "index.html"])) {
|
||||
const cause = unreadable.get(page);
|
||||
if (cause) throw new Error(`${page} could not be read because ${UNREADABLE_WORDS[cause]}.`);
|
||||
}
|
||||
for (const [path, cause] of unreadable) {
|
||||
const warning = `${path} could not be read because ${UNREADABLE_WORDS[cause]}; it was left out of the upload.`;
|
||||
onLeftOut?.(terminalText(warning), {
|
||||
...leftOutReference(absProjectDir, path, path, join(absProjectDir, path), "unreadable"),
|
||||
cause,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,6 +501,7 @@ interface ExternalAssetContext {
|
||||
onOutsideFile?: OnOutsideFile;
|
||||
received: ReceivedFiles;
|
||||
walkedLeftOut: ReadonlySet<string>;
|
||||
unreadable: Map<string, UnreadableCause>;
|
||||
filmPages: Set<string> | null;
|
||||
}
|
||||
|
||||
@@ -452,7 +532,12 @@ function addExternalAsset(
|
||||
archivePath = `${base}_${i}${ext}`;
|
||||
}
|
||||
|
||||
ctx.fileContents.set(archivePath, stageFile(absPath, ctx.onDiskMinBytes));
|
||||
const staged = readOrCause(() => stageFile(absPath, ctx.onDiskMinBytes));
|
||||
if (!staged.ok) {
|
||||
leaveUnreadable(ctx, entryPath, rawPath, absPath, staged.cause);
|
||||
return null;
|
||||
}
|
||||
ctx.fileContents.set(archivePath, staged.value);
|
||||
ctx.externalMap.set(absPath, archivePath);
|
||||
ctx.usedArchivePaths.add(archivePath);
|
||||
return archivePath;
|
||||
@@ -509,7 +594,7 @@ function includeReferencedProjectFile(
|
||||
const absPath = candidate.replace(/[?#].*$/, "");
|
||||
if (!isPathInside(absPath, ctx.absProjectDir)) continue;
|
||||
const rel = archiveKey(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
|
||||
if (staysOut(ctx, rel)) return;
|
||||
if (staysOutOrUnreadable(ctx, entryPath, rawPath, absPath, rel)) return;
|
||||
if (!existsSync(absPath)) {
|
||||
if (isPrivateCompositionFile(ctx.absProjectDir, absPath)) return;
|
||||
continue;
|
||||
@@ -517,8 +602,7 @@ function includeReferencedProjectFile(
|
||||
if (!statSync(absPath).isFile()) return;
|
||||
const reason = referencedFileLeftOut(ctx, entryPath, rel, absPath);
|
||||
if (reason) return leaveOut(ctx, entryPath, rawPath, absPath, reason);
|
||||
ctx.fileContents.set(rel, stageFile(absPath, ctx.onDiskMinBytes));
|
||||
return;
|
||||
return stageReferenced(ctx, entryPath, rawPath, absPath, rel);
|
||||
}
|
||||
if (!requireOnDisk) return;
|
||||
throw new Error(`The composition references "${rawPath}", which is not in the project folder.`);
|
||||
@@ -558,6 +642,50 @@ function leaveOut(
|
||||
);
|
||||
}
|
||||
|
||||
function staysOutOrUnreadable(
|
||||
ctx: ExternalAssetContext,
|
||||
entryPath: string,
|
||||
rawPath: string,
|
||||
absPath: string,
|
||||
rel: string,
|
||||
): boolean {
|
||||
const cause = unreadableAt(ctx.unreadable, rel);
|
||||
if (!cause) return staysOut(ctx, rel);
|
||||
leaveUnreadable(ctx, entryPath, rawPath, absPath, cause);
|
||||
return true;
|
||||
}
|
||||
|
||||
function stageReferenced(
|
||||
ctx: ExternalAssetContext,
|
||||
entryPath: string,
|
||||
rawPath: string,
|
||||
absPath: string,
|
||||
rel: string,
|
||||
): void {
|
||||
const staged = readOrCause(() => stageFile(absPath, ctx.onDiskMinBytes));
|
||||
if (staged.ok) return void ctx.fileContents.set(rel, staged.value);
|
||||
ctx.unreadable.set(rel, staged.cause);
|
||||
leaveUnreadable(ctx, entryPath, rawPath, absPath, staged.cause);
|
||||
}
|
||||
|
||||
/** A file the video's page uses that could not be read: listed with that page, so the person can see it is used. */
|
||||
function leaveUnreadable(
|
||||
ctx: ExternalAssetContext,
|
||||
entryPath: string,
|
||||
rawPath: string,
|
||||
absPath: string,
|
||||
cause: UnreadableCause,
|
||||
): void {
|
||||
if (!(ctx.filmPages?.has(entryPath) ?? true)) return;
|
||||
const warning = terminalText(
|
||||
`${entryPath} uses ${rawPath}, which could not be read because ${UNREADABLE_WORDS[cause]}; it was left out of the upload.`,
|
||||
);
|
||||
if (ctx.leftOut.has(warning)) return;
|
||||
ctx.leftOut.add(warning);
|
||||
const reference = leftOutReference(ctx.absProjectDir, entryPath, rawPath, absPath, "unreadable");
|
||||
ctx.onLeftOut?.(warning, { ...reference, cause });
|
||||
}
|
||||
|
||||
function leftOnAnotherNetworkRoot(
|
||||
ctx: ExternalAssetContext,
|
||||
entryPath: string,
|
||||
@@ -830,6 +958,7 @@ export function localizeExternalAssets(
|
||||
onOutsideFile?: OnOutsideFile,
|
||||
entry = "index.html",
|
||||
walkedLeftOut: ReadonlySet<string> = new Set(),
|
||||
unreadable = new Map<string, UnreadableCause>(),
|
||||
): number {
|
||||
const ctx: ExternalAssetContext = {
|
||||
absProjectDir,
|
||||
@@ -843,6 +972,7 @@ export function localizeExternalAssets(
|
||||
onOutsideFile,
|
||||
received: receivedFiles(absProjectDir),
|
||||
walkedLeftOut,
|
||||
unreadable,
|
||||
filmPages: filmPagesOf(absProjectDir, fileContents, entry),
|
||||
};
|
||||
|
||||
@@ -893,7 +1023,10 @@ function composeNamesInText(fileContents: Map<string, StagedFile>, filePaths: st
|
||||
}
|
||||
}
|
||||
|
||||
type WalkedLeftOutReason = Exclude<LeftOutReason, "file_type" | "network_share" | "script">;
|
||||
type WalkedLeftOutReason = Exclude<
|
||||
LeftOutReason,
|
||||
"file_type" | "network_share" | "script" | "unreadable"
|
||||
>;
|
||||
|
||||
const WALKED_LEFT_OUT: Record<WalkedLeftOutReason, string> = {
|
||||
private:
|
||||
@@ -928,8 +1061,15 @@ export function stagePublishFiles(
|
||||
): Map<string, StagedFile> {
|
||||
const absProjectDir = resolve(projectDir);
|
||||
const filePaths: string[] = [];
|
||||
collectProjectFiles(absProjectDir, absProjectDir, filePaths, projectIgnore(absProjectDir));
|
||||
if (!filePaths.includes("index.html")) {
|
||||
const unreadable = new Map<string, UnreadableCause>();
|
||||
collectProjectFiles(
|
||||
absProjectDir,
|
||||
absProjectDir,
|
||||
filePaths,
|
||||
projectIgnore(absProjectDir),
|
||||
unreadable,
|
||||
);
|
||||
if (!filePaths.includes("index.html") && !unreadable.has("index.html")) {
|
||||
throw new Error(
|
||||
"Project archive must include index.html at the root. Check that .hyperframesignore does not exclude it.",
|
||||
);
|
||||
@@ -966,8 +1106,11 @@ export function stagePublishFiles(
|
||||
walkedLeftOut.add(key);
|
||||
continue;
|
||||
}
|
||||
fileContents.set(key, stageFile(absPath, onDiskMinBytes));
|
||||
const staged = readOrCause(() => stageFile(absPath, onDiskMinBytes));
|
||||
if (staged.ok) fileContents.set(key, staged.value);
|
||||
else unreadable.set(key, staged.cause);
|
||||
}
|
||||
reportUnreadable(absProjectDir, unreadable, entry, onLeftOut);
|
||||
|
||||
composeNamesInText(fileContents, filePaths);
|
||||
localizeExternalAssets(
|
||||
@@ -978,6 +1121,7 @@ export function stagePublishFiles(
|
||||
onOutsideFile,
|
||||
entry,
|
||||
walkedLeftOut,
|
||||
unreadable,
|
||||
);
|
||||
return fileContents;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import type { LeftOutReference } from "./leftOutReference.js";
|
||||
import { stagePublishFiles } from "./publishProject.js";
|
||||
|
||||
let dir: string;
|
||||
const locked: string[] = [];
|
||||
|
||||
beforeEach(() => {
|
||||
dir = mkdtempSync(join(tmpdir(), "hf-publish-unreadable-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const path of locked.splice(0)) chmodSync(path, 0o755);
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function write(path: string, content = "x"): void {
|
||||
mkdirSync(dirname(join(dir, path)), { recursive: true });
|
||||
writeFileSync(join(dir, path), content, "utf-8");
|
||||
}
|
||||
|
||||
function lock(path: string): void {
|
||||
chmodSync(join(dir, path), 0o000);
|
||||
locked.push(join(dir, path));
|
||||
}
|
||||
|
||||
function stage(): { keys: string[]; leftOut: LeftOutReference[]; warnings: string[] } {
|
||||
const leftOut: LeftOutReference[] = [];
|
||||
const warnings: string[] = [];
|
||||
const staged = stagePublishFiles(dir, Infinity, (warning, reference) => {
|
||||
warnings.push(warning);
|
||||
leftOut.push(reference);
|
||||
});
|
||||
return { keys: [...staged.keys()].sort(), leftOut, warnings };
|
||||
}
|
||||
|
||||
const page = (body = "") =>
|
||||
`<!doctype html><html><body><div data-composition-id="main">${body}</div></body></html>`;
|
||||
|
||||
// chmod does not deny reads on Windows, and root reads through it.
|
||||
const canDenyReads = process.platform !== "win32" && process.getuid?.() !== 0;
|
||||
|
||||
describe("a project file the upload cannot read", () => {
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"is left out with its cause when it is a link to a file that is gone",
|
||||
() => {
|
||||
write("index.html", page());
|
||||
symlinkSync(join(dir, "nowhere.mp4"), join(dir, "gone.mp4"));
|
||||
|
||||
const { keys, leftOut, warnings } = stage();
|
||||
|
||||
expect(keys).toEqual(["index.html"]);
|
||||
expect(leftOut).toEqual([
|
||||
expect.objectContaining({
|
||||
file: "gone.mp4",
|
||||
reference: "gone.mp4",
|
||||
reason: "unreadable",
|
||||
cause: "missing",
|
||||
}),
|
||||
]);
|
||||
expect(warnings).toEqual([
|
||||
"gone.mp4 could not be read because it was deleted or is a link to a file that is gone; it was left out of the upload.",
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(!canDenyReads)(
|
||||
"leaves out a folder that cannot be listed, and uploads the rest",
|
||||
() => {
|
||||
write("index.html", page());
|
||||
write("logo.png");
|
||||
write("private/notes.txt");
|
||||
lock("private");
|
||||
|
||||
const { keys, leftOut } = stage();
|
||||
|
||||
expect(keys).toEqual(["index.html", "logo.png"]);
|
||||
expect(leftOut).toEqual([
|
||||
expect.objectContaining({ file: "private", reason: "unreadable", cause: "denied" }),
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(!canDenyReads)("says so quietly when the video does not use it", () => {
|
||||
write("index.html", page('<img src="logo.png">'));
|
||||
write("logo.png");
|
||||
write("draft.png");
|
||||
lock("draft.png");
|
||||
|
||||
const { keys, leftOut } = stage();
|
||||
|
||||
expect(keys).toEqual(["index.html", "logo.png"]);
|
||||
expect(leftOut.map(({ file, reference }) => [file, reference])).toEqual([
|
||||
["draft.png", "draft.png"],
|
||||
]);
|
||||
});
|
||||
|
||||
it.skipIf(!canDenyReads)("names the page that uses it when the video does", () => {
|
||||
write("index.html", page('<img src="assets/logo.png">'));
|
||||
write("assets/logo.png");
|
||||
lock("assets/logo.png");
|
||||
|
||||
const { keys, leftOut, warnings } = stage();
|
||||
|
||||
expect(keys).toEqual(["index.html"]);
|
||||
expect(leftOut).toEqual([
|
||||
expect.objectContaining({ file: "assets/logo.png", reference: "assets/logo.png" }),
|
||||
expect.objectContaining({
|
||||
file: "index.html",
|
||||
reference: "assets/logo.png",
|
||||
reason: "unreadable",
|
||||
cause: "denied",
|
||||
}),
|
||||
]);
|
||||
expect(warnings[1]).toBe(
|
||||
"index.html uses assets/logo.png, which could not be read because this computer does not allow reading it; it was left out of the upload.",
|
||||
);
|
||||
});
|
||||
|
||||
it.skipIf(!canDenyReads)("names the page that uses a file inside a folder it cannot list", () => {
|
||||
write("index.html", page('<video src="media/intro.mp4"></video>'));
|
||||
write("media/intro.mp4");
|
||||
lock("media");
|
||||
|
||||
const { leftOut } = stage();
|
||||
|
||||
expect(leftOut.map(({ file, reference, cause }) => [file, reference, cause])).toEqual([
|
||||
["media", "media", "denied"],
|
||||
["index.html", "media/intro.mp4", "denied"],
|
||||
]);
|
||||
});
|
||||
|
||||
it.skipIf(!canDenyReads)("refuses, naming it, when it is the page the video starts from", () => {
|
||||
write("index.html", page());
|
||||
lock("index.html");
|
||||
|
||||
expect(() => stage()).toThrow(
|
||||
"index.html could not be read because this computer does not allow reading it.",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -162,7 +162,8 @@ export type LeftOutReason =
|
||||
| "private"
|
||||
| "network_share"
|
||||
| "script"
|
||||
| "not_approved";
|
||||
| "not_approved"
|
||||
| "unreadable";
|
||||
|
||||
const WHY: Record<Exclude<LeftOutReason, "file_type">, string> = {
|
||||
private: "which holds secrets or tool data",
|
||||
@@ -171,6 +172,7 @@ const WHY: Record<Exclude<LeftOutReason, "file_type">, string> = {
|
||||
network_share: "which is on another computer's network share or is a Windows device path",
|
||||
script: "which is a script outside the project",
|
||||
not_approved: "which is outside the project and was not approved for upload",
|
||||
unreadable: "which could not be read",
|
||||
};
|
||||
|
||||
export const INCLUDE_OUTSIDE_FILES_HINT = "Pass --include-outside-files to upload it.";
|
||||
|
||||
Reference in New Issue
Block a user