sdk-for-node
sdk-for-node copied to clipboard
🐛 Bug Report: Uploaded file has wrong type
👟 Reproduction steps
When I try to upload an image to storage using the Node client the uploaded file has the wrong type.
- I create an image using
node-canvasand save it a buffer:canvas.toBuffer() - I upload the image to Appwrite storage:
InputFile.fromBuffer(buffer, 'name.png') - It uploads as type
application/octet-streaminstead of PNG
The buffer should be valid as I am able to save and view the image locally using fs.writeFileSync("name.png", buffer);
👍 Expected behavior
I expect that it would store it as a png. From my understanding, it should be grabbing the type from the file name.
Here are some relevant links @stnguyen90 shared on Discord.
Node client: https://github.com/appwrite/sdk-for-node/blob/0b061a1102d95ec3468d39d3103a50dc03d35a0f/lib/services/storage.js#L416
Server: https://github.com/appwrite/appwrite/blob/0bf39bd152ad4a939711cd6bf0231c1baf4511f8/app/controllers/api/storage.php#L476
👎 Actual Behavior
It uploads as application/octet-stream. I've also tried not using the .png extension but that made no difference.
This is what it shows on the dashboard:

Here you can see the file names. I also attempted saving the base64 image which is the text file you see.

When opening the last file, with the .png it shows this. You can see where it says PNG at the top.

🎲 Appwrite version
Version 0.15.x
💻 Operating system
MacOS
🧱 Your Environment
I'm using "node-appwrite": "^7.0.2"
👀 Have you spent some time to check if this issue has been raised before?
- [X] I checked and didn't find similar issue
🏢 Have you read the Code of Conduct?
- [X] I have read the Code of Conduct
I tested this using the playground-for-python (swapping InputFile.from_path() with InputFile.from_bytes()) and it worked.
I switched over to the playground-for-node and did the same thing, but I got application/octet-stream even though $path here was "/storage/uploads/app-file/files/6/2/e/0/62e0479c125ab67067f9.jpg".
I tried to download and open the file uploaded by the node-appwrite SDK, but it failed to open.
I think buffer.toString() in the node-appwrite SDK might be causing a problem.
I tried removing the toString() and the upload successfully worked with the right content type.
@CEOSiyris, for now, instead of InputFile.fromBuffer(buffer, 'name.png') try this:
new InputFile(Readable.from(buffer), "name.png", Buffer.byteLength(buffer))
@CEOSiyris, for now, instead of
InputFile.fromBuffer(buffer, 'name.png')try this:new InputFile(Readable.from(buffer), "name.png", Buffer.byteLength(buffer))
This works great, thank you! Will do that for now.
I confirm I am also affected by this. However InputFile's constructor is not exposed in Typescript, thus for the workaround to work someone would need:
new (InputFile as any)(Readable.from(buffer), "name.png", Buffer.byteLength(buffer))
@CEOSiyris, for now, instead of
InputFile.fromBuffer(buffer, 'name.png')try this:new InputFile(Readable.from(buffer), "name.png", Buffer.byteLength(buffer))
I've tried this workaround and the issue is still occurring. This is with Appwrite v1 and node SDK version 8.1.0
@JacksonToomey, it might be best for you to share your code on our Discord.