added random, original options for resource ID type

pull/12/head
tycrek 4 years ago
parent 45fdf744e8
commit 4d699ca015
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -7,7 +7,7 @@ try {
} }
// Load the config // Load the config
const { host, port, domain, useSsl, resourceIdSize } = require('./config.json'); const { host, port, domain, useSsl, resourceIdSize, resourceIdType } = require('./config.json');
//#region Imports //#region Imports
const fs = require('fs-extra'); const fs = require('fs-extra');
@ -16,7 +16,7 @@ const express = require('express');
const useragent = require('express-useragent'); const useragent = require('express-useragent');
const multer = require('multer'); const multer = require('multer');
const zws = require('./idgen/zws'); const zws = require('./idgen/zws');
const { path, saveData, log, verify } = require('./utils'); const { path, saveData, log, verify, generateId } = require('./utils');
//#endregion //#endregion
//#region Variables, module setup //#region Variables, module setup
@ -59,7 +59,7 @@ function startup() {
log(`Uploaded: ${req.file.originalname} (${req.file.mimetype})`); log(`Uploaded: ${req.file.originalname} (${req.file.mimetype})`);
// Save the file information // Save the file information
let resourceId = zws(resourceIdSize); //todo: use crypto-random-string for alternative resourceId let resourceId = generateId(resourceIdType, resourceIdSize, req.file.originalname);
data[resourceId] = req.file; data[resourceId] = req.file;
saveData(data); saveData(data);

@ -0,0 +1,2 @@
//const { cryptoRandomString } = require('crypto-random-string');
//module.exports = (length) => cryptoRandomString({ length, type: 'alphanumeric' });

39
package-lock.json generated

@ -8,6 +8,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"crypto-random-string": "^4.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-useragent": "^1.0.15", "express-useragent": "^1.0.15",
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",
@ -201,6 +202,20 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
}, },
"node_modules/crypto-random-string": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-4.0.0.tgz",
"integrity": "sha512-x8dy3RnvYdlUcPOjkEHqozhiwzKNSq7GcPuXFbnyMOCHxX8V3OgIg/pYuabl2sbUPfIJaeAQB7PMOK8DFIdoRA==",
"dependencies": {
"type-fest": "^1.0.1"
},
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/cycle": { "node_modules/cycle": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz", "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz",
@ -847,6 +862,17 @@
"node": ">=0.6" "node": ">=0.6"
} }
}, },
"node_modules/type-fest": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-1.0.1.tgz",
"integrity": "sha512-+UTPE7JT3O+sUpRroRgQAbbSfIRBwOHh+o/oruB1JJE6g6uBm3Y0D82fO3xu8VHfxJLQjeRp0PEY6mRmh/lElA==",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/type-is": { "node_modules/type-is": {
"version": "1.6.18", "version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
@ -1126,6 +1152,14 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
}, },
"crypto-random-string": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-4.0.0.tgz",
"integrity": "sha512-x8dy3RnvYdlUcPOjkEHqozhiwzKNSq7GcPuXFbnyMOCHxX8V3OgIg/pYuabl2sbUPfIJaeAQB7PMOK8DFIdoRA==",
"requires": {
"type-fest": "^1.0.1"
}
},
"cycle": { "cycle": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz", "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz",
@ -1633,6 +1667,11 @@
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
"integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw=="
}, },
"type-fest": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-1.0.1.tgz",
"integrity": "sha512-+UTPE7JT3O+sUpRroRgQAbbSfIRBwOHh+o/oruB1JJE6g6uBm3Y0D82fO3xu8VHfxJLQjeRp0PEY6mRmh/lElA=="
},
"type-is": { "type-is": {
"version": "1.6.18", "version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",

@ -23,6 +23,7 @@
}, },
"homepage": "https://github.com/tycrek/anssxustawai#readme", "homepage": "https://github.com/tycrek/anssxustawai#readme",
"dependencies": { "dependencies": {
"crypto-random-string": "^4.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-useragent": "^1.0.15", "express-useragent": "^1.0.15",
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",

@ -1,9 +1,21 @@
const fs = require('fs-extra'); const fs = require('fs-extra');
const Path = require('path'); const Path = require('path');
const zwsGen = require('./idgen/zws');
//const randomGen = require('./idgen/random');
const idModes = {
zws: 'zws', // Zero-width spaces (see: https://zws.im/)
og: 'original', // Use original uploaded filename
r: 'random' // Use a randomly generated ID with a mixed-case alphanumeric character set
};
module.exports = { module.exports = {
log: console.log, log: console.log,
path: (...paths) => Path.join(__dirname, ...paths), path: (...paths) => Path.join(__dirname, ...paths),
saveData: (data) => fs.writeJsonSync(Path.join(__dirname, 'data.json'), data, { spaces: 4 }), saveData: (data) => fs.writeJsonSync(Path.join(__dirname, 'data.json'), data, { spaces: 4 }),
verify: (req, tokens) => req.headers.authorization && tokens.includes(req.headers.authorization) verify: (req, tokens) => req.headers.authorization && tokens.includes(req.headers.authorization),
generateId: (mode, lenth, originalName) =>
(mode == idModes.zws) ? zwsGen(lenth)
: (mode == idModes.r) ? zwsGen(lenth)//randomGen(lenth)
: originalName
} }
Loading…
Cancel
Save