jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

Issue uploading a file with non-English filename #222

Closed jchariot closed 4 years ago

jchariot commented 4 years ago
var my_graphQL_func = {
  type: responseType,
  args: {
    file_upload: { type : GraphQLUpload, required: true }
  },
  resolve: async (_, args) => {
      ...
    console.log( args.file_upload )
    ...
  }

I am using GraphQLUpload. Everything works ok, but whenever I upload a file that contains non-English characters in the file's name, the app seems to . For example, I got a file named "平仮名.pdf". When I uploaded it in, this is what I got from console.log above:

Promise {
  { filename: 's�\r.pdf',
    mimetype: 'application/pdf',
    encoding: '7bit',
    createReadStream: [Function: createReadStream] } }

So "平仮名.pdf" became "s�\r.pdf" . This occurs with other languages as well. How do I fix this issue?

jaydenseric commented 4 years ago

If you use the Chrome network inspector to look at the outgoing GraphQL request, in the request body in the multipart string do the file field names look correct?

jchariot commented 4 years ago

If you use the Chrome network inspector to look at the outgoing GraphQL request, in the request body in the multipart string do the file field names look correct?

My code is on node.js, and I am using Postman to do a post request to node.js. All of this is on localhost.

For additional information, GraphQL returned this 500 Internal Server Error and this:

{ "errors": [ { "message": "Cannot read property 'statuscode' of null" } ] }

statuscode is one of the things I requested as a returning value, but it seems GraphQL went haywire and couldn't return anything properly.

Chrome network inspector to look at the outgoing GraphQL request

Sorry, I am not sure how to do this. Would you mind give me some pointers?

jchariot commented 4 years ago

In node.js app, I logged out the request.body.variables.file1 out:

app.use(
  entry_url,
  graphqlUploadExpress({ maxFileSize: 4000000, maxFiles: 10 }),
  graphqlHTTP((request) => {
      console.log( request.body.variables.file1 )
    return {
      schema: inputSchema,
      context: { startTime: Date.now(), bodyQuery: request.body.query}, 
      originalContext: request },
      graphiql: true,
      extensions,
    };
  }),
);

and I got the following:

Upload {
  resolve: [Function],
  reject: [Function],
  promise:
   Promise {
     { filename: 's�\r.pdf',
       mimetype: 'application/pdf',
       encoding: '7bit',
       createReadStream: [Function: createReadStream] } },
  file:
   { filename: 's�\r.pdf',
     mimetype: 'application/pdf',
     encoding: '7bit',
     createReadStream: [Function: createReadStream] } }
jaydenseric commented 4 years ago

Personally I don't use Postman, but it seems you can easily open a network inspector this way:

Screen Shot 2020-10-19 at 9 19 02 pm

It would be good to figure out if the filenames are being sent correctly from the client. If they are not, then we know the problem is on the client.

jchariot commented 4 years ago

Personally I don't use Postman, but it seems you can easily open a network inspector this way:

Screen Shot 2020-10-19 at 9 19 02 pm

It would be good to figure out if the filenames are being sent correctly from the client. If they are not, then we know the problem is on the client.

Upon further inspection. We created another tiny app to test this, and the tiny app could send files with non-English name to the node.js app without any issue. Therefore, Postman (v7.34.0) is at fault here.