我使用 imagekit 和 AdminBro 为文件上传创建自定义提供程序。这里是:
const { BaseProvider } = require("@admin-bro/upload");
const ImageKit = require("imagekit");
const imagekit = new ImageKit({
publicKey: process.env.IMAGE_KIT_PUBLIC_KEY,
privateKey: process.env.IMAGE_KIT_PRIVATE_KEY,
urlEndpoint: process.env.IMAGE_KIT_URL,
});
class UploadProvider extends BaseProvider {
constructor() {
super(process.env.IMAGE_KIT_URL);
}
async upload(file, key) {
console.log(file);
console.log(key);
const imageBinary = Buffer.from(JSON.stringify(file));
imagekit.upload(
{
file: imageBinary,
fileName: "test.jpg",
},
function (error, result) {
if (error) console.log(error);
else console.log(result);
}
);
return true;
}
async delete(key) {
imagekit.deleteFile(key, function (error, result) {
if (error) console.log(error);
else {
console.log(result);
return true;
}
});
}
async path(key, bucket) {
return bucket + key;
}
}
module.exports = UploadProvider;
当我上传一个 img 文件时,我得到以下响应:
{
fileId: '6065faae524afb4c982ea36b',
name: 'test_lQ-cxEt5i0.jpg',
size: 991,
filePath: '/test_lQ-cxEt5i0.jpg',
url: 'https://ik.imagekit.io/my-site/test_lQ-cxEt5i0.jpg',
fileType: 'non-image'
}
和 Imagekit 仪表板: jpg 扩展名丢失
所以文件被发送到 imagekit 但它丢失了扩展名。
这也是console.log(文件)中的内容:
{
size: 45742,
path: 'C:\\Users\\adibu\\AppData\\Local\\Temp\\upload_7627ceeabb5e466ad9e4c780613105f7',
name: 'candle.jpg',
type: 'image/jpeg',
hash: null,
_events: {},
_eventsCount: 0,
_writeStream: {
fd: null,
pos: undefined,
path: 'C:\\Users\\adibu\\AppData\\Local\\Temp\\upload_7627ceeabb5e466ad9e4c780613105f7',
mode: 438,
flags: 'w',
start: undefined,
closed: true,
_events: {},
bytesWritten: 45742,
_eventsCount: 0,
_maxListeners: undefined,
_writableState: {
sync: false,
ended: true,
ending: true,
length: 0,
corked: 0,
closed: true,
writing: false,
onwrite: [Function: bound onwrite],
writecb: null,
allNoop: true,
errored: null,
finished: true,
writelen: 0,
buffered: [],
needDrain: true,
destroyed: true,
pendingcb: 0,
emitClose: true,
objectMode: false,
allBuffers: true,
finalCalled: false,
constructed: true,
prefinished: true,
autoDestroy: true,
errorEmitted: false,
closeEmitted: true,
highWaterMark: 16384,
decodeStrings: true,
bufferedIndex: 0,
defaultEncoding: 'utf8',
bufferProcessing: false,
afterWriteTickInfo: null
}
},
_maxListeners: undefined,
lastModifiedDate: 2021-04-01T16:54:05.017Z
}
我不知道为什么文件在上传时丢失了扩展名,有人知道解决方案吗?