uploading buffer to did space creates empty file
hello!
i am successfully able to write JSON files to my did space but when I try to use a Buffer, it upload a 0-byte empty file.
i logged the file details which show this:
(2024-10-16 18:38:41): File Details: {
(2024-10-16 18:38:41): originalname: 'cafe-theme.mp3',
(2024-10-16 18:38:41): mimetype: 'audio/mpeg',
(2024-10-16 18:38:41): size: 3319557,
(2024-10-16 18:38:41): bufferSize: 3319557
(2024-10-16 18:38:41): }can you help me figure out how to fix this? z1k7Ks2F6eWNeRWZxwnByDasrHCyLaQQfBG z1QsuPsVgzPk2Nij87QFjyVh4v7Hvz5fsZj
here's the api endpoint code:
import { PutObjectCommand, SpaceClient } from '@did-space/client';
import type { Request, Response } from 'express';
import { authService, wallet } from '../../libs/auth';
import multer from 'multer';
const upload = multer({
storage: multer.memoryStorage(), // Store file in memory temporarily
});
export default async function $post(req: Request, res: Response) {
upload.single('file')(req, res, async (err: any) => {
if (err) {
return res.status(500).json({ message: 'File upload error', error: err });
}
// Extract the file from the request
const file = req.file;
if (!file) {
return res.status(400).json({ message: 'No file uploaded' });
}
// Log file details for debugging
console.log('File Details:', {
originalname: file.originalname,
mimetype: file.mimetype,
size: file.size,
bufferSize: file.buffer ? file.buffer.length : 'No buffer',
});
// Check if buffer exists and has content
if (!file.buffer || file.buffer.length === 0) {
return res.status(400).json({ message: 'File buffer is empty' });
}
// Authenticate the user
const { user } = await authService.getUser(req.user?.did as string);
if (!req.user) {
return res.status(401).send('Unauthorized');
}
if (!user?.didSpace?.endpoint) {
console.log(`DID Spaces endpoint does not exist. Log in again to complete the authorization`);
return res.status(404).send('DID Spaces endpoint does not exist. Log in again to complete the authorization');
}
// Initialize SpaceClient
const spaceClient = new SpaceClient({
wallet,
endpoint: user.didSpace.endpoint,
});
try {
// Upload the file to DID Space
const output = await spaceClient.send(
new PutObjectCommand({
key: file.originalname || 'test.mp3',
data: file.buffer,
})
);
if (output.statusCode !== 200) {
console.log(`Error!`, output);
return res.status(output.statusCode).send(output.statusMessage);
}
return res.status(200).json({ message: 'File uploaded successfully' });
} catch (uploadError) {
console.error('Upload error:', uploadError);
return res.status(500).json({ message: 'Error uploading file', error: uploadError.message });
}
});
}
6 条回复
thank you for your feedback, we have released a new version, please try again, it should work properly now.
thank you ser! works great now!!! really appreciate your help.
Thank you for helping my friend Void. Would his find make him eligible for a bug bounty?
Yes, of course, this is a valuable bug report, just marked as qualified.
Thank you! Awesome and a high value one! Good find z1e4XATJSo1Y1jom17Uhj1SpKZ4LFmYSaHY now get back to werk👷🏿👷🏿👷🏿👷🏿👷🏿🤣🤣🤣
need harold pain emoji and/or kek emoji