diff --git a/src/ctl/commands/import-dir.ts b/src/ctl/commands/import-dir.ts index bc38a95d..15ff7213 100755 --- a/src/ctl/commands/import-dir.ts +++ b/src/ctl/commands/import-dir.ts @@ -2,11 +2,18 @@ import { guess } from '@/lib/mimes'; import { statSync } from 'fs'; import { readFile, readdir } from 'fs/promises'; import { join, parse, resolve } from 'path'; +import { reloadSettings } from '@/lib/config'; +import { bytes } from '@/lib/bytes'; -export async function importDir(directory: string, { id, folder }: { id?: string; folder?: string }) { +export async function importDir( + directory: string, + { id, folder, skipDb }: { id?: string; folder?: string; skipDb?: boolean }, +) { const fullPath = resolve(directory); if (!statSync(fullPath).isDirectory()) return console.error('Not a directory:', directory); + await reloadSettings(); + const { prisma } = await import('@/lib/db/index.js'); let userId: string; @@ -62,18 +69,39 @@ export async function importDir(directory: string, { id, folder }: { id?: string }; } - const res = await prisma.file.createMany({ - data, - }); + if (!skipDb) { + const { count } = await prisma.file.createMany({ + data, + }); + console.log(`Inserted ${count} files into the database.`); + } - console.log('Imported', res.count, 'files'); + const totalSize = data.reduce((acc, file) => acc + file.size, 0); + let completed = 0; const { datasource } = await import('@/lib/datasource/index.js'); for (let i = 0; i !== files.length; ++i) { - const buff = await readFile(join(fullPath, files[i])); + console.log(`Uploading ${data[i].name} (${bytes(data[i].size)})...`); + const start = process.hrtime(); + + const buff = await readFile(join(fullPath, files[i])); await datasource.put(data[i].name, buff); - console.log('Uploaded', data[i].name); + + const diff = process.hrtime(start); + + const time = diff[0] * 1e9 + diff[1]; + const timeStr = time > 1e9 ? `${(time / 1e9).toFixed(2)}s` : `${(time / 1e6).toFixed(2)}ms`; + + const uploadSpeed = (data[i].size / time) * 1e9; + const uploadSpeedStr = + uploadSpeed > 1e9 ? `${(uploadSpeed / 1e9).toFixed(2)} GB/s` : `${(uploadSpeed / 1e6).toFixed(2)} MB/s`; + + completed += data[i].size; + + console.log( + `Uploaded ${data[i].name} in ${timeStr} (${bytes(data[i].size)}) ${i + 1}/${files.length} ${bytes(completed)}/${bytes(totalSize)} ${uploadSpeedStr}`, + ); } console.log('Done importing files.'); diff --git a/src/ctl/index.ts b/src/ctl/index.ts index 969bd9d7..47b29f0a 100755 --- a/src/ctl/index.ts +++ b/src/ctl/index.ts @@ -39,6 +39,8 @@ cli '-i, --id [user_id]', 'the id that imported files should belong to. if unspecificed the user with the "administrator" username as well as the "SUPERADMIN" role will be used', ) + .option('--skip-db', 'do not add the files to the database') + .option('--skip-ds', 'do not add the files to the datasource') .option('-f, --folder [folder_id]', 'an optional folder to add the files to') .argument('', 'the directory to import into Zipline') .action(importDir);