mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
use try&catch
This commit is contained in:
parent
5646510a22
commit
8cf613fbe6
@ -4,35 +4,36 @@ import axios from "axios"
|
|||||||
|
|
||||||
export default async (payload) => {
|
export default async (payload) => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let { url, destination } = payload
|
try {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
const writer = fs.createWriteStream(destination)
|
||||||
|
|
||||||
|
if (!writer) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios({
|
||||||
|
url,
|
||||||
|
method: "GET",
|
||||||
|
responseType: "stream"
|
||||||
|
})
|
||||||
|
|
||||||
|
response.data.pipe(writer)
|
||||||
|
|
||||||
|
writer.on("finish", () => resolve({
|
||||||
|
destination,
|
||||||
|
delete: () => fs.unlinkSync(destination),
|
||||||
|
read: () => fs.readFileSync(destination),
|
||||||
|
}))
|
||||||
|
writer.on("error", reject)
|
||||||
|
} catch (error) {
|
||||||
|
reject(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const writer = fs.createWriteStream(destination).catch((err) => {
|
|
||||||
reject(err)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!writer) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await axios({
|
|
||||||
url,
|
|
||||||
method: "GET",
|
|
||||||
responseType: "stream"
|
|
||||||
})
|
|
||||||
|
|
||||||
response.data.pipe(writer)
|
|
||||||
|
|
||||||
writer.on("finish", () => resolve({
|
|
||||||
destination,
|
|
||||||
delete: () => fs.unlinkSync(destination),
|
|
||||||
read: () => fs.readFileSync(destination),
|
|
||||||
}))
|
|
||||||
writer.on("error", reject)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user