appwrite / sdk-for-node

[READ-ONLY] Official Appwrite Node.js SDK 🟒

Home Page:https://appwrite.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› Bug Report: Uploaded file has wrong type

SiyrisSoul opened this issue Β· comments

πŸ‘Ÿ 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:

payload['file'] = { type: 'file', file: stream, filename: file.filename };

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?

  • I checked and didn't find similar issue

🏒 Have you 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.