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

Add responseType: "stream" to the POST request of the EsrGan API #6

Open
CruxCoder7 opened this issue Oct 1, 2023 · 0 comments
Open

Comments

@CruxCoder7
Copy link

return axios({
  url: this.url,
  data: JSON.stringify(data),
  method: "post",
  responseType: "stream",
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "application/json",
    "x-api-key": `${this.apiKey}`,
   },
})

The data returned by the API is in a binary string format.

Adding responseType: "stream" makes the return type as an object. This helps in parsing the response and downloading the image returned by the API by calling the pipe().

export const EnhanceImage = async (image: string) => {
  esr.generate({
      image,
    })
    .then((response) => {
      if (response.status === 200) {
        const saveDirectory = "./enhanced_images"
        const fileName = 'downloaded_image.jpg'

        if (!fs.existsSync(saveDirectory)) {
          fs.mkdirSync(saveDirectory, { recursive: true })
        }

        const filePath = path.join(saveDirectory, fileName)

        const writer = fs.createWriteStream(filePath)
        response.data.pipe(writer)

        return new Promise((resolve, reject) => {
          writer.on("finish", resolve)
          writer.on("error", reject)
        })
      }
    })
    .catch((err) => console.log(err))
}
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

1 participant