improve error handling

This commit is contained in:
SrGooglo 2023-07-20 13:34:39 +00:00
parent 7343f5df83
commit 3b6e2fa9e2

View File

@ -3,26 +3,31 @@ import path from "path"
import axios from "axios" import axios from "axios"
export default async (payload) => { export default async (payload) => {
let { url, destination } = payload return new Promise(async (resolve, reject) => {
let { url, destination } = payload
// if destination path is not provided, use cache folder // if destination path is not provided, use cache folder
if (!destination) { if (!destination) {
destination = path.resolve(global.uploadCachePath, path.basename(url)) destination = path.resolve(global.uploadCachePath, path.basename(url))
} }
console.log(destination) const writer = fs.createWriteStream(destination).catch((err) => {
reject(err)
return false
})
const writer = fs.createWriteStream(destination) if (!writer) {
return false
}
const response = await axios({ const response = await axios({
url, url,
method: "GET", method: "GET",
responseType: "stream" responseType: "stream"
}) })
response.data.pipe(writer) response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on("finish", () => resolve({ writer.on("finish", () => resolve({
destination, destination,
delete: () => fs.unlinkSync(destination), delete: () => fs.unlinkSync(destination),