mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 19:14:16 +00:00
added download-file
util
This commit is contained in:
parent
fd6c7016cc
commit
889f069012
33
packages/server/src/utils/download-file/index.js
Normal file
33
packages/server/src/utils/download-file/index.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import fs from "fs"
|
||||||
|
import path from "path"
|
||||||
|
import axios from "axios"
|
||||||
|
|
||||||
|
export default async (payload) => {
|
||||||
|
let { url, destination } = payload
|
||||||
|
|
||||||
|
// if destination path is not provided, use cache folder
|
||||||
|
if (!destination) {
|
||||||
|
destination = path.resolve(global.uploadCachePath, path.basename(url))
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(destination)
|
||||||
|
|
||||||
|
const writer = fs.createWriteStream(destination)
|
||||||
|
|
||||||
|
const response = await axios({
|
||||||
|
url,
|
||||||
|
method: "GET",
|
||||||
|
responseType: "stream"
|
||||||
|
})
|
||||||
|
|
||||||
|
response.data.pipe(writer)
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
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