Triment / blog

0 stars 0 forks source link

graphql-yoga文件上传的问题 #54

Open Triment opened 1 year ago

Triment commented 1 year ago

stackoverflow

另一个关于此的讨论

后者给出了yoga的另一种文件上传的写法


profileImageUpload: async (_, { file }: { file: File }) => {
      // get readableStream from blob
      const readableStream = file.stream()
      const stream = readableStream.getReader()
      console.log('file', file.type)
      let _file: Buffer | undefined
      while (true) {
        // for each iteration: value is the next blob fragment
        const { done, value } = await stream.read()
        if (done) {
          // no more data in the stream
          console.log('all blob processed.')
          break
        }

        if (value)
          _file = Buffer.concat([_file || Buffer.alloc(0), Buffer.from(value)])
      }

      if (_file) {
        const image = sharp(_file)
        const metadata = await image.metadata()
        console.log(metadata, 'metadata')
        try {
          const image = await sharp(_file).resize(600, 600).webp().toBuffer()
          fs.writeFileSync('test.webp', image)
          console.log(image, 'image')
        }
        catch (error) {
          console.error(error)
        }
      }

      return 'a'
    },
Triment commented 1 year ago

https://stackoverflow.com/questions/49053193/nodejs-request-how-to-send-multipart-form-data-post-request

Yoga 是通过解析request对象实现上传的,这是关于发送数据的讨论

Triment commented 1 year ago

关于 File 标准api在浏览器中的数据控制和文件规则用法https://web.dev/read-files/#read-metadata

Triment commented 1 year ago

express关于blob的pr

https://github.com/expressjs/express/issues/4807