tachybase_todo/packages/plugins/file-manager/src/server/utils.ts
Junyi 08711e6c8e refactor(plugin-file-manager): move client code into plugin folder and enable path config (#913)
# Conflicts:
#	packages/app/client/src/pages/index.tsx
#	packages/core/client/src/application/Application.tsx
#	packages/core/client/src/file-manager/index.ts
#	packages/plugins/file-manager/package.json
#	packages/plugins/file-manager/src/client/FileStorage.tsx
#	packages/plugins/file-manager/src/client/FileStorageShortcut.tsx
2022-10-27 13:49:06 +08:00

18 lines
490 B
TypeScript

import crypto from 'crypto';
import path from 'path';
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}`);
});
};