I want to use nodejs to call WeChat Mini Programs"s API "get temporary material" to download the material locally. So far, I have written a logic that can be downloaded locally, but the file format can only be defined as" jpeg", "assuming that the material type is unknown, how to download? Does anyone know how to handle it? Is it convenient to paste the code?
paste my code
* downloadPic(url = "", path = "") {
// url:
// path:
// TODO jpg
url = {url: url}
const fs = require("fs")
const request = require("request")
//
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
path = path + "/" + this.getRandom(20) + ".jpeg"
yield new Promise((resolve, reject) => {
request
.get(url)
.on("response", (response) => {
console.log("img type:", response.headers["content-type"])
})
.pipe(fs.createWriteStream(path))
.on("error", (e) => {
console.log("pipe error", e)
resolve("");
})
.on("finish", () => {
console.log("finish");
resolve("ok");
})
.on("close", () => {
console.log("close");
})
})
//
return path.replace("app/", "")
}