Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How proxing the upload files to request.js to another server without saving locally #172

Closed
anarkiVan opened this issue Nov 27, 2019 · 2 comments

Comments

@anarkiVan
Copy link

Hello! I have the problem need help plz.
I have apollo-server-express 2.9 and the client send file there. Me need proxing this request to the other server.
BUT! i cann't do this and i have error GraphQLError: Error: socket hang up:(
EXAMPLE:

async mutationResolver( _, { contacts : { user_scans, ...otherData } }, ctx ) {
  const getFileData = async ( filePromise ) => {
    const { filename, mimetype, createReadStream } = await filePromise;
    const stream = createReadStream();
    await new Promise( ( res, rej ) => {
      stream
      .on( 'error', error => {
        console.error('There was an error.');
        reject( error );
      } )
      .on( 'end', () => {
        res();
      } )
      .resume();
    } );

    return {
      value   : stream,
        options : {
          filename,
          contentType : mimetype,
        },
     };
  };
  // user_scans are promise files array from client
  const fileDataPromises = user_scans.map( getFileData );
  const files = await Promise.all( filedDataPromises );
  // swagger 2.0 does not understand file array and this hack need
  const attachFiles = files.reduce( ( acc, file, index ) => {
    return {
      ...acc,
      [`user_scan_${index + 1}`] : file,
    };
  }, {} );

  const formData = {
    ...otherData,
    ...attachFiles,
  };
  // customRp is request-promise with deafult headers
  const response = await customRp( {
    uri    : myOtherServerURL,
    method : 'POST',
    formData,
  } );
}

And i have error GraphQLError: Error: socket hang up. But if i send files with fs.createReadStream('./file1.jpg') it WORKS!(

Sorry if that not for you issue, but i can not find solve. but probarly you will give hint to me/
ps. sorry for my bad english

@jaydenseric
Copy link
Owner

I'm sorry, but this looks like a usage question that might be better suited for Stack Overflow or another discussion forum.

without saving locally

As a tip, you will not be able to avoid the temp file being created via fs-capacitor, you can read about the architecture here.

Your code is a little confusing to me, so I won't comment on your situation exactly but you should be able to figure out how to stream files out to another server; that is how most of us are processing file uploads. I stream my files up to DigitalOcean Spaces.

Of course, if you find a specific bug with minimal reproduction steps we can reopen.

@anarkiVan
Copy link
Author

thank you! i found explain in this issue form-data/form-data#394
trouble was be with compatibility between stream from fs-capacitor and form-data which used to request.js underhood as I understand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants