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

18 lines
490 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) {
2021-12-06 21:23:34 +08:00
cb(err, err ? undefined : `${raw.toString('hex')}${path.extname(file.originalname)}`);
});
}
2021-12-06 21:23:34 +08:00
export const cloudFilenameGetter = (storage) => (req, file, cb) => {
getFilename(req, file, (err, filename) => {
if (err) {
return cb(err);
}
cb(null, `${storage.path ? `${storage.path}/` : ''}${filename}`);
});
2021-12-06 21:23:34 +08:00
};