mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 22:49:26 +08:00
* 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
31 lines
935 B
TypeScript
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('/');
|
|
}
|