aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorPitu <[email protected]>2021-01-07 03:08:08 +0900
committerPitu <[email protected]>2021-01-07 03:08:08 +0900
commita0824b5a972ccff31e2105502a899db8f00bfce2 (patch)
tree0242209276be5fe4379503f1fd45db54ce59f7d4 /src/api
parentfix: early return if duplicated file (diff)
downloadhost.fuwn.me-a0824b5a972ccff31e2105502a899db8f00bfce2.tar.xz
host.fuwn.me-a0824b5a972ccff31e2105502a899db8f00bfce2.zip
feat: add rehashing script
Diffstat (limited to 'src/api')
-rw-r--r--src/api/utils/rehashDatabase.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/api/utils/rehashDatabase.js b/src/api/utils/rehashDatabase.js
new file mode 100644
index 0000000..4ccb301
--- /dev/null
+++ b/src/api/utils/rehashDatabase.js
@@ -0,0 +1,54 @@
+require('dotenv').config();
+const blake3 = require('blake3');
+const path = require('path');
+const fs = require('fs');
+const db = require('knex')({
+ client: 'sqlite3',
+ connection: {
+ filename: path.join(__dirname, '../../../database/', 'database.sqlite')
+ }
+});
+
+const start = async () => {
+ const hrstart = process.hrtime();
+ const uploads = await db.table('files')
+ .select('id', 'name', 'hash');
+ console.log(`Uploads : ${uploads.length}`);
+
+
+ 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) => {
+ fs.createReadStream(path.join(__dirname, '../../../uploads', upload.name))
+ .on('error', reject)
+ .pipe(blake3.createHash())
+ .on('error', reject)
+ .on('data', async source => {
+ const hash = source.toString('hex');
+ console.log(`${upload.name}: ${hash}`);
+ await db.table('files')
+ .update('hash', hash)
+ .where('id', upload.id);
+ done++;
+ resolve();
+ });
+ }).catch(error => {
+ console.log(`${upload.name}: ${error.toString()}`);
+ });
+ }
+
+ clearInterval(progressInterval);
+ printProgress();
+
+ const hrend = process.hrtime(hrstart);
+ console.log(`Done in : ${(hrend[0] + (hrend[1] / 1e9)).toFixed(4)}s`);
+};
+
+start();