Skip to content

Streamed file size is 5-6 times bigger than the original size #82

@raxosiris

Description

@raxosiris

I am using apollo-upload-server with graphql-nodejs boilerplate template, and apollo-upload-client implementation with react native (expo).

While uploading the files from filesystem, the filesize on the server is at least 5-6 times the size in the local system. I do not know what is the exact cause of the issue. Here are the steps to reproduce:

In Expo, I am returning the local uri from ImagePicker and creating a file to upload:
const picture = new ReactNativeFile({ uri: returnedURI, type: 'image/jpg', name: 'somename.jpg', });

and then running a mutation as follows:

<Mutation mutation={uploadFileMutation}>
                {uploadFile => (
                  <Button
                    styleName="clear"
                    style={{ flex: 1 }}
                    onPress={() => {
                      uploadFile({
                        variables: {
                          picture,
                        },
                      });
                    }}
                  >
                  </Button>
                )}
              </Mutation>

On the server-side, upload is processed as follows:

const processUpload = async upload => {
  const { stream, filename } = await upload
  const { id, path } = await storeUpload({ stream, filename })
  return path
}

const storeUpload = async ({ stream, filename }) => {
  const id = nanoid()
  const path = somepath

  return new Promise((resolve, reject) =>
    stream
      .pipe(createWriteStream(path))
      .on('finish', () => resolve({ id, path }))
      .on('error', reject),
  )
}

const file = {
    async uploadFile(parent, { picture }, ctx, info) {
      const resolvedURL = await processUpload(picture)
      const url = `http://localhost:4000/${resolvedURL}`
      return ctx.db.mutation.createFile(
        {
          data: {
            url,
          },
        },
        info
      )
    },
}

Here are two images for comparison - one from the filesystem, and second uploaded one

From filesystem- Size: 45kb
81fceb8e-2352-40cd-a10a-c77084fd8a08
Uploaded copy - Size: 257kb
iyvmzwjig0coq4bnunbqh

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions