Video clip tool (v1.8.3-video-mgmt) #13
1 changed files with 45 additions and 15 deletions
|
|
@ -481,25 +481,55 @@ export async function saveToLibrary(db, job, userId) {
|
|||
const audio = job.file.endsWith(".mp3");
|
||||
const ext = audio ? ".mp3" : ".mp4";
|
||||
const filename = `${randomBytes(12).toString("hex")}${ext}`;
|
||||
await mkdir(UPLOADS_DIR, { recursive: true });
|
||||
await copyFile(from, path.join(UPLOADS_DIR, filename));
|
||||
const to = path.join(UPLOADS_DIR, filename);
|
||||
|
||||
const info = audio ? { width: null, height: null } : await probe(from);
|
||||
const { size } = await stat(from);
|
||||
const params = JSON.parse(job.params ?? "{}");
|
||||
const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`;
|
||||
const label = `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`;
|
||||
// Each step says what it was doing if it fails, so the page can
|
||||
// tell an admin (this tool is admin-only) what to fix: usually the
|
||||
// uploads folder's permissions, or UPLOADS_DIR not set.
|
||||
const step = async (what, fn) => {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
const why =
|
||||
err.code === "EACCES" || err.code === "EPERM"
|
||||
? "the service's user isn't allowed to write there"
|
||||
: err.message;
|
||||
throw new ClipError(500, `Couldn't save it to the library: ${what} (${why}).`);
|
||||
}
|
||||
};
|
||||
|
||||
const { lastInsertRowid } = db
|
||||
.prepare(
|
||||
`INSERT INTO media (filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, 0, ?)`,
|
||||
)
|
||||
.run(filename, label.slice(0, 200), audio ? "audio/mpeg" : "video/mp4", size, info.width, info.height, userId);
|
||||
await step(`making the uploads folder ${UPLOADS_DIR}`, () => mkdir(UPLOADS_DIR, { recursive: true }));
|
||||
await step(`copying the clip into ${UPLOADS_DIR}`, () => copyFile(from, to));
|
||||
|
||||
let id;
|
||||
try {
|
||||
const info = audio
|
||||
? { width: null, height: null }
|
||||
: await step("reading the clip's size", () => probe(from));
|
||||
const { size } = await stat(from);
|
||||
const params = JSON.parse(job.params ?? "{}");
|
||||
const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`;
|
||||
const label = `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`;
|
||||
|
||||
id = await step("adding it to the media table", () =>
|
||||
Number(
|
||||
db
|
||||
.prepare(
|
||||
`INSERT INTO media (filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, 0, ?)`,
|
||||
)
|
||||
.run(filename, label.slice(0, 200), audio ? "audio/mpeg" : "video/mp4", size, info.width, info.height, userId)
|
||||
.lastInsertRowid,
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
await rm(to, { force: true }); // no file in uploads without its row
|
||||
throw err;
|
||||
}
|
||||
|
||||
await rm(from, { force: true });
|
||||
update(db, job.id, { saved_media_id: Number(lastInsertRowid), file: null });
|
||||
return { id: Number(lastInsertRowid), url: `${PUBLIC_PREFIX}${filename}` };
|
||||
update(db, job.id, { saved_media_id: id, file: null });
|
||||
return { id, url: `${PUBLIC_PREFIX}${filename}` };
|
||||
}
|
||||
|
||||
/* After a clip has gone out in full: its file isn't needed. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue