-
Notifications
You must be signed in to change notification settings - Fork 2
/
importer.js
36 lines (26 loc) · 1010 Bytes
/
importer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
// stolen logic from addfrog.js, will mass import blindly any files in _temp
// hash + filename validation
const reg = new RegExp('^[0-9a-f]{40}\\.(png|jpg|gif)$');
const frogs = fs.readdirSync('./frogs/_temp');
console.log(frogs.length);
for (const tempPath of frogs) {
// pull the temp file from fs
const tempFileBuffer = fs.readFileSync(path.resolve(__dirname, 'frogs/_temp', tempPath));
// get sha1 hash
const hashSum = crypto.createHash('sha1');
hashSum.update(tempFileBuffer);
const frogHash = hashSum.digest('hex');
const frogExtension = path.extname(tempPath);
const frogFileName = `${frogHash}${frogExtension}`;
// validate the name of new frog
if (!reg.test(frogFileName)) {
console.log(`${frogFileName} failed validation`);
}
else {
const frogAcceptedPath = path.resolve(__dirname, 'frogs', frogFileName);
fs.writeFileSync(frogAcceptedPath, tempFileBuffer);
}
}