feat: add rehashing script

This commit is contained in:
Pitu 2021-01-07 03:08:08 +09:00
parent df4e4272f4
commit a0824b5a97
2 changed files with 57 additions and 0 deletions

View File

@ -10,6 +10,9 @@
### Attention
If you are upgrading from v3 to v4 (current release) and you want to keep your files and relations please read the [migration guide](docs/migrating.md).
### Attention (2)
v4.0.1 changed the hashing algorithm for a better, faster one. So if you are currently running v4.0.0 and decide to update to v4.0.1+ it's in your best interest to rehash all the files your instance is serving. To do this go to the chibisafe root folder and run `node src/api/utils/rehashDatabase.js`. Depending on how many files you have it can take a few minutes or hours, there's a progress bar that will give you an idea.
### What is this?
Chibisafe is a file uploader service written in node that aims to to be easy to use and easy to set up. It's mainly intended for images and videos, but it accepts anything you throw at it.
- You can run it in public or private mode, making it so only people with user accounts can upload files as well as controlling if user signup is enabled or not.

View File

@ -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();