79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
const fs = require('fs');
|
|
|
|
// Patch 1: scanner
|
|
let scan = fs.readFileSync('src/lib/scanner/index.ts', 'utf8');
|
|
const oldLoop = scan.substring(scan.indexOf('// 3. Find new and changed files'), scan.indexOf('// 4. Find removed files'));
|
|
const newLoop = ` // 3. Find new and changed files
|
|
const newFiles = [];
|
|
for (const [relPath, file] of scannedFiles) {
|
|
const existing = dbPaths.get(relPath);
|
|
if (!existing) {
|
|
newFiles.push(file);
|
|
} else {
|
|
unchanged++;
|
|
dbPaths.delete(relPath);
|
|
}
|
|
}
|
|
|
|
const stableResults = await Promise.all(
|
|
newFiles.map(async (file) => {
|
|
const isStable = await checkFileStability(file.absolutePath);
|
|
return { file, isStable };
|
|
})
|
|
);
|
|
|
|
for (const { file, isStable } of stableResults) {
|
|
const relPath = file.relativePath;
|
|
if (!isStable) {
|
|
console.log('Skipping unstable file:', relPath);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
const mediaType = getMediaType(file.ext);
|
|
const mimeType = getMimeType(file.ext);
|
|
|
|
await prisma.mediaItem.create({
|
|
data: {
|
|
relativePath: relPath,
|
|
filename: file.filename,
|
|
directory: file.directory,
|
|
mediaType,
|
|
mimeType,
|
|
fileSize: file.size,
|
|
lastModified: file.mtime,
|
|
lastScanned: new Date(),
|
|
published: false,
|
|
},
|
|
});
|
|
|
|
await enqueueProcessing(relPath, mediaType);
|
|
added++;
|
|
console.log('Added:', relPath);
|
|
} catch (error) {
|
|
const msg = 'Failed to add ' + relPath + ': ' + (error instanceof Error ? error.message : String(error));
|
|
errors.push(msg);
|
|
console.error(msg);
|
|
}
|
|
}
|
|
|
|
`;
|
|
scan = scan.replace(oldLoop, newLoop);
|
|
fs.writeFileSync('src/lib/scanner/index.ts', scan, 'utf8');
|
|
|
|
// Patch 2: media API
|
|
let mediaApi = fs.readFileSync('src/app/api/media/route.ts', 'utf8');
|
|
mediaApi = mediaApi.replace(
|
|
'return NextResponse.json({\n items,\n nextCursor,\n votingOpen: settings?.votingOpen ?? false,\n });',
|
|
'return NextResponse.json({ items: JSON.parse(JSON.stringify(items, (k, v) => typeof v === \'bigint\' ? Number(v) : v)), nextCursor, votingOpen: settings?.votingOpen ?? false });'
|
|
);
|
|
fs.writeFileSync('src/app/api/media/route.ts', mediaApi, 'utf8');
|
|
|
|
// Patch 3: media ID update API
|
|
let mediaIdApi = fs.readFileSync('src/app/api/admin/media/[id]/route.ts', 'utf8');
|
|
mediaIdApi = mediaIdApi.replace(
|
|
'return NextResponse.json(updated);',
|
|
'return NextResponse.json(JSON.parse(JSON.stringify(updated, (k, v) => typeof v === \'bigint\' ? Number(v) : v)));'
|
|
);
|
|
fs.writeFileSync('src/app/api/admin/media/[id]/route.ts', mediaIdApi, 'utf8');
|