2020-12-31 12:11:38 +08:00
|
|
|
import crypto from 'crypto';
|
|
|
|
import path from 'path';
|
|
|
|
|
2021-03-28 13:34:51 +08:00
|
|
|
export function getFilename(req, file, cb) {
|
2020-12-31 12:11:38 +08:00
|
|
|
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-04 07:58:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export const cloudFilenameGetter = (storage) => (req, file, cb) => {
|
2021-12-04 07:58:31 +08:00
|
|
|
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
|
|
|
};
|