2020-12-27 11:16:01 +00:00
|
|
|
const blake3 = require('blake3')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const paths = require('../controllers/pathsController')
|
|
|
|
const utils = require('../controllers/utilsController')
|
|
|
|
const config = require('./../config')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
|
|
|
|
;(async () => {
|
|
|
|
const location = process.argv[1].replace(process.cwd() + '/', '')
|
|
|
|
const args = process.argv.slice(2)
|
|
|
|
|
2020-12-27 11:54:05 +00:00
|
|
|
const mode = parseInt(args[0])
|
2020-12-27 11:16:01 +00:00
|
|
|
|
2020-12-27 11:54:05 +00:00
|
|
|
if (![0, 1, 2].includes(mode) ||
|
2020-12-27 11:16:01 +00:00
|
|
|
args.includes('--help') ||
|
|
|
|
args.includes('-h')) {
|
|
|
|
return console.log(utils.stripIndents(`
|
|
|
|
Rebuild file hashes.
|
|
|
|
|
2020-12-27 11:54:05 +00:00
|
|
|
Usage:
|
2020-12-27 12:44:10 +00:00
|
|
|
node ${location} <mode=0|1|2>
|
2020-12-27 11:16:01 +00:00
|
|
|
|
|
|
|
mode:
|
|
|
|
0 = Dry run (recalculate hashes, print them, but do NOT store to DB).
|
2020-12-27 11:54:05 +00:00
|
|
|
1 = Recalculate hashes and store to DB.
|
|
|
|
2 = Verbose (recalculate hashes, print them, and store to DB).
|
2020-12-27 11:16:01 +00:00
|
|
|
`).trim())
|
|
|
|
}
|
|
|
|
|
2020-12-27 11:54:05 +00:00
|
|
|
const dryrun = mode === 0
|
|
|
|
const verbose = [0, 2].includes(mode)
|
2020-12-27 11:16:01 +00:00
|
|
|
|
2020-12-27 11:54:05 +00:00
|
|
|
console.log('Querying uploads\u2026')
|
2020-12-27 11:16:01 +00:00
|
|
|
const hrstart = process.hrtime()
|
|
|
|
const uploads = await db.table('files')
|
|
|
|
.select('id', 'name', 'hash')
|
|
|
|
console.log(`Uploads : ${uploads.length}`)
|
|
|
|
|
2020-12-27 12:44:10 +00:00
|
|
|
let done = 0
|
|
|
|
|
|
|
|
const printProgress = () => {
|
|
|
|
console.log(`PROGRESS: ${done}/${uploads.length}`)
|
|
|
|
if (done >= uploads.length) clearInterval(progressInterval)
|
|
|
|
}
|
|
|
|
const progressInterval = setInterval(printProgress, 1000)
|
|
|
|
printProgress()
|
|
|
|
|
|
|
|
for (const upload of uploads) {
|
|
|
|
await new Promise((resolve, reject) => {
|
2020-12-27 11:16:01 +00:00
|
|
|
fs.createReadStream(path.join(paths.uploads, upload.name))
|
2020-12-27 12:44:10 +00:00
|
|
|
.on('error', reject)
|
2020-12-27 11:16:01 +00:00
|
|
|
.pipe(blake3.createHash())
|
2020-12-27 12:44:10 +00:00
|
|
|
.on('error', reject)
|
|
|
|
.on('data', async source => {
|
|
|
|
const hash = source.toString('hex')
|
2020-12-27 11:54:05 +00:00
|
|
|
if (verbose) console.log(`${upload.name}: ${hash}`)
|
2020-12-27 11:16:01 +00:00
|
|
|
if (!dryrun && upload.hash !== hash) {
|
|
|
|
await db.table('files')
|
|
|
|
.update('hash', hash)
|
|
|
|
.where('id', upload.id)
|
|
|
|
}
|
2020-12-27 12:44:10 +00:00
|
|
|
done++
|
2020-12-27 11:16:01 +00:00
|
|
|
resolve()
|
|
|
|
})
|
2020-12-27 12:44:10 +00:00
|
|
|
}).catch(error => {
|
|
|
|
console.log(`${upload.name}: ${error.toString()}`)
|
2020-12-27 11:16:01 +00:00
|
|
|
})
|
2020-12-27 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clearInterval(progressInterval)
|
|
|
|
printProgress()
|
2020-12-27 11:16:01 +00:00
|
|
|
|
|
|
|
const hrend = process.hrtime(hrstart)
|
|
|
|
console.log(`Done in : ${(hrend[0] + (hrend[1] / 1e9)).toFixed(4)}s`)
|
|
|
|
if (dryrun) {
|
|
|
|
console.log('INFO: This was a dry run. DB had not been modified.')
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
})
|