fix: incorrect logic when removing files
This commit is contained in:
parent
94bd14db52
commit
96b4cecaec
@ -19,8 +19,9 @@ export function checkFramesForEncoding() {
|
|||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
SELECT id, imgFilename, createdAt
|
SELECT id, imgFilename, createdAt
|
||||||
FROM frame
|
FROM frame
|
||||||
WHERE encodeStatus = 0 AND imgFilename IS NOT NULL
|
WHERE encodeStatus = 0
|
||||||
ORDER BY createdAt ASC;
|
AND imgFilename IS NOT NULL
|
||||||
|
ORDER BY createdAt;
|
||||||
`);
|
`);
|
||||||
const frames = stmt.all() as Frame[];
|
const frames = stmt.all() as Frame[];
|
||||||
|
|
||||||
@ -52,19 +53,23 @@ export function checkFramesForEncoding() {
|
|||||||
if (chunkConditionSatisfied) {
|
if (chunkConditionSatisfied) {
|
||||||
// Create new encoding task
|
// Create new encoding task
|
||||||
const taskStmt = db.prepare(`
|
const taskStmt = db.prepare(`
|
||||||
INSERT INTO encoding_task (status) VALUES (0);
|
INSERT INTO encoding_task (status)
|
||||||
|
VALUES (0);
|
||||||
`);
|
`);
|
||||||
const taskId = taskStmt.run().lastInsertRowid;
|
const taskId = taskStmt.run().lastInsertRowid;
|
||||||
|
|
||||||
// Insert frames into encoding_task_data
|
// Insert frames into encoding_task_data
|
||||||
const insertStmt = db.prepare(`
|
const insertStmt = db.prepare(`
|
||||||
INSERT INTO encoding_task_data (encodingTaskID, frame) VALUES (?, ?);
|
INSERT INTO encoding_task_data (encodingTaskID, frame)
|
||||||
|
VALUES (?, ?);
|
||||||
`);
|
`);
|
||||||
for (const frame of buffer) {
|
for (const frame of buffer) {
|
||||||
insertStmt.run(taskId, frame.id);
|
insertStmt.run(taskId, frame.id);
|
||||||
db.prepare(
|
db.prepare(
|
||||||
`
|
`
|
||||||
UPDATE frame SET encodeStatus = 1 WHERE id = ?;
|
UPDATE frame
|
||||||
|
SET encodeStatus = 1
|
||||||
|
WHERE id = ?;
|
||||||
`
|
`
|
||||||
).run(frame.id);
|
).run(frame.id);
|
||||||
}
|
}
|
||||||
@ -78,16 +83,21 @@ function deleteEncodedScreenshots() {
|
|||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
// TODO: double-check that the frame was really encoded into the video
|
// TODO: double-check that the frame was really encoded into the video
|
||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
SELECT * FROM frame WHERE encodeStatus = 2 AND imgFilename IS NOT NULL;
|
SELECT *
|
||||||
|
FROM frame
|
||||||
|
WHERE encodeStatus = 2
|
||||||
|
AND imgFilename IS NOT NULL;
|
||||||
`);
|
`);
|
||||||
const frames = stmt.all() as Frame[];
|
const frames = stmt.all() as Frame[];
|
||||||
for (const frame of frames) {
|
for (const frame of frames) {
|
||||||
if (!frame.imgFilename) continue;
|
const imgPath = path.join(getScreenshotsDir(), frame.imgFilename!);
|
||||||
const imgPath = path.join(getScreenshotsDir(), frame.imgFilename);
|
if (fs.existsSync(imgPath)) {
|
||||||
if (!fs.existsSync(imgPath)) return;
|
|
||||||
fs.unlinkSync(imgPath);
|
fs.unlinkSync(imgPath);
|
||||||
|
}
|
||||||
const updateStmt = db.prepare(`
|
const updateStmt = db.prepare(`
|
||||||
UPDATE frame SET imgFilename = NULL WHERE id = ?;
|
UPDATE frame
|
||||||
|
SET imgFilename = NULL
|
||||||
|
WHERE id = ?;
|
||||||
`);
|
`);
|
||||||
updateStmt.run(frame.id);
|
updateStmt.run(frame.id);
|
||||||
}
|
}
|
||||||
@ -99,7 +109,9 @@ function _deleteNonExistentScreenshots() {
|
|||||||
const filesInDir = new Set(fs.readdirSync(screenshotDir));
|
const filesInDir = new Set(fs.readdirSync(screenshotDir));
|
||||||
|
|
||||||
const dbStmt = db.prepare(`
|
const dbStmt = db.prepare(`
|
||||||
SELECT imgFilename FROM frame WHERE imgFilename IS NOT NULL;
|
SELECT imgFilename
|
||||||
|
FROM frame
|
||||||
|
WHERE imgFilename IS NOT NULL;
|
||||||
`);
|
`);
|
||||||
const dbFiles = dbStmt.all() as { imgFilename: string }[];
|
const dbFiles = dbStmt.all() as { imgFilename: string }[];
|
||||||
const dbFileSet = new Set(dbFiles.map((f) => f.imgFilename));
|
const dbFileSet = new Set(dbFiles.map((f) => f.imgFilename));
|
||||||
@ -120,7 +132,9 @@ export async function deleteUnnecessaryScreenshots() {
|
|||||||
export function deleteFrameFromDB(id: number) {
|
export function deleteFrameFromDB(id: number) {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
const deleteStmt = db.prepare(`
|
const deleteStmt = db.prepare(`
|
||||||
DELETE FROM frame WHERE id = ?;
|
DELETE
|
||||||
|
FROM frame
|
||||||
|
WHERE id = ?;
|
||||||
`);
|
`);
|
||||||
deleteStmt.run(id);
|
deleteStmt.run(id);
|
||||||
console.log(`Deleted frame ${id} from database`);
|
console.log(`Deleted frame ${id} from database`);
|
||||||
@ -150,8 +164,7 @@ export function processEncodingTasks() {
|
|||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
SELECT id, status
|
SELECT id, status
|
||||||
FROM encoding_task
|
FROM encoding_task
|
||||||
WHERE status = 0
|
WHERE status = 0 LIMIT ?
|
||||||
LIMIT ?
|
|
||||||
`);
|
`);
|
||||||
|
|
||||||
const tasks = stmt.all(CONCURRENCY - tasksPerforming.length) as EncodingTask[];
|
const tasks = stmt.all(CONCURRENCY - tasksPerforming.length) as EncodingTask[];
|
||||||
@ -163,7 +176,9 @@ export function processEncodingTasks() {
|
|||||||
|
|
||||||
// Update task status as processing (1)
|
// Update task status as processing (1)
|
||||||
const updateStmt = db.prepare(`
|
const updateStmt = db.prepare(`
|
||||||
UPDATE encoding_task SET status = 1 WHERE id = ?
|
UPDATE encoding_task
|
||||||
|
SET status = 1
|
||||||
|
WHERE id = ?
|
||||||
`);
|
`);
|
||||||
updateStmt.run(taskId);
|
updateStmt.run(taskId);
|
||||||
|
|
||||||
@ -172,7 +187,7 @@ export function processEncodingTasks() {
|
|||||||
FROM encoding_task_data
|
FROM encoding_task_data
|
||||||
JOIN frame ON encoding_task_data.frame = frame.id
|
JOIN frame ON encoding_task_data.frame = frame.id
|
||||||
WHERE encoding_task_data.encodingTaskID = ?
|
WHERE encoding_task_data.encodingTaskID = ?
|
||||||
ORDER BY frame.createdAt ASC
|
ORDER BY frame.createdAt
|
||||||
`);
|
`);
|
||||||
const frames = framesStmt.all(taskId) as Frame[];
|
const frames = framesStmt.all(taskId) as Frame[];
|
||||||
|
|
||||||
@ -193,13 +208,19 @@ export function processEncodingTasks() {
|
|||||||
console.log(`Video ${videoPath} created successfully`);
|
console.log(`Video ${videoPath} created successfully`);
|
||||||
// Update task status to complete (2)
|
// Update task status to complete (2)
|
||||||
const completeStmt = db.prepare(`
|
const completeStmt = db.prepare(`
|
||||||
UPDATE encoding_task SET status = 2 WHERE id = ?
|
UPDATE encoding_task
|
||||||
|
SET status = 2
|
||||||
|
WHERE id = ?
|
||||||
`);
|
`);
|
||||||
completeStmt.run(taskId);
|
completeStmt.run(taskId);
|
||||||
for (let frameIndex = 0; frameIndex < frames.length; frameIndex++) {
|
for (let frameIndex = 0; frameIndex < frames.length; frameIndex++) {
|
||||||
const frame = frames[frameIndex];
|
const frame = frames[frameIndex];
|
||||||
const updateFrameStmt = db.prepare(`
|
const updateFrameStmt = db.prepare(`
|
||||||
UPDATE frame SET videoPath = ?, videoFrameIndex = ?, encodeStatus = 2 WHERE id = ?
|
UPDATE frame
|
||||||
|
SET videoPath = ?,
|
||||||
|
videoFrameIndex = ?,
|
||||||
|
encodeStatus = 2
|
||||||
|
WHERE id = ?
|
||||||
`);
|
`);
|
||||||
updateFrameStmt.run(`${taskId}.mp4`, frameIndex, frame.id);
|
updateFrameStmt.run(`${taskId}.mp4`, frameIndex, frame.id);
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ function init(db: Database) {
|
|||||||
|
|
||||||
export async function initDatabase() {
|
export async function initDatabase() {
|
||||||
const dbPath = getDatabaseDir();
|
const dbPath = getDatabaseDir();
|
||||||
const db = new DB(dbPath, { verbose: console.log });
|
const db = new DB(dbPath);
|
||||||
const libSimpleExtensionPath = getLibSimpleExtensionPath();
|
const libSimpleExtensionPath = getLibSimpleExtensionPath();
|
||||||
|
|
||||||
db.loadExtension(libSimpleExtensionPath);
|
db.loadExtension(libSimpleExtensionPath);
|
||||||
|
Loading…
Reference in New Issue
Block a user