tachybase_todo/packages/plugin-file-manager/src/utils.ts

18 lines
486 B
TypeScript
Raw Normal View History

import crypto from 'crypto';
import path from 'path';
2021-03-28 13:34:51 +08:00
export function getFilename(req, file, cb) {
crypto.pseudoRandomBytes(16, function (err, raw) {
cb(err, err ? undefined : `${raw.toString('hex')}${path.extname(file.originalname)}`)
});
}
export const cloudFilenameGetter = storage => (req, file, cb) => {
getFilename(req, file, (err, filename) => {
if (err) {
return cb(err);
}
cb(null, `${storage.path ? `${storage.path}/` : ''}${filename}`);
});
}