sdk-for-node icon indicating copy to clipboard operation
sdk-for-node copied to clipboard

🐛 Bug Report: Uploaded file has wrong type

Open SiyrisSoul opened this issue 3 years ago • 4 comments
trafficstars

👟 Reproduction steps

When I try to upload an image to storage using the Node client the uploaded file has the wrong type.

  1. I create an image using node-canvas and save it a buffer: canvas.toBuffer()
  2. I upload the image to Appwrite storage: InputFile.fromBuffer(buffer, 'name.png')
  3. It uploads as type application/octet-stream instead 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: Screenshot 2022-07-26 at 9 49 34 AM

Here you can see the file names. I also attempted saving the base64 image which is the text file you see. Screenshot 2022-07-26 at 9 51 05 AM

When opening the last file, with the .png it shows this. You can see where it says PNG at the top. Screenshot 2022-07-26 at 9 52 14 AM

🎲 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?

SiyrisSoul avatar Jul 26 '22 14:07 SiyrisSoul

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.

stnguyen90 avatar Jul 26 '22 20:07 stnguyen90

@CEOSiyris, for now, instead of InputFile.fromBuffer(buffer, 'name.png') try this:

new InputFile(Readable.from(buffer), "name.png", Buffer.byteLength(buffer))

stnguyen90 avatar Jul 26 '22 20:07 stnguyen90

@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.

SiyrisSoul avatar Jul 26 '22 22:07 SiyrisSoul

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))

maikeriva avatar Sep 02 '22 15:09 maikeriva

@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 avatar Oct 31 '22 00:10 JacksonToomey

@JacksonToomey, it might be best for you to share your code on our Discord.

stnguyen90 avatar Nov 17 '22 20:11 stnguyen90