Junyi 40999ed22e
fix(plugin-fm): fix delete file error of cos (#4529)
* fix(plugin-fm): fix cos delete file record

* fix(plugin-fm): fix file cannot be deleted in cos

* fix(plugin-fm): fix path join logic

* fix(plugin-fm): fix test cases
2024-05-30 19:22:38 +08:00

31 lines
935 B
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
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.replace(/\/+$/, '')}/` : ''}${filename}`);
});
};
export function getFileKey(record) {
return [record.path.replace(/^\/|\/$/g, ''), record.filename].filter(Boolean).join('/');
}