mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-30 17:33:09 +02:00
Fix: download file (#3138)
* Fix: url download * not explicitly close on end * resolve on stream finish * remove unnecessary code * reject on file error
This commit is contained in:
parent
2a5c19cd01
commit
5f59b980a7
1 changed files with 26 additions and 8 deletions
|
@ -35,21 +35,39 @@ export default async (url: string, user: IUser, folderId: mongodb.ObjectID = nul
|
||||||
// write content at URL to temp file
|
// write content at URL to temp file
|
||||||
await new Promise((res, rej) => {
|
await new Promise((res, rej) => {
|
||||||
const writable = fs.createWriteStream(path);
|
const writable = fs.createWriteStream(path);
|
||||||
|
|
||||||
|
writable.on('finish', () => {
|
||||||
|
res();
|
||||||
|
});
|
||||||
|
|
||||||
|
writable.on('error', error => {
|
||||||
|
rej(error);
|
||||||
|
});
|
||||||
|
|
||||||
const requestUrl = URL.parse(url).pathname.match(/[^\u0021-\u00ff]/) ? encodeURI(url) : url;
|
const requestUrl = URL.parse(url).pathname.match(/[^\u0021-\u00ff]/) ? encodeURI(url) : url;
|
||||||
request({
|
|
||||||
|
const req = request({
|
||||||
url: requestUrl,
|
url: requestUrl,
|
||||||
proxy: config.proxy,
|
proxy: config.proxy,
|
||||||
|
timeout: 10 * 1000,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': config.user_agent
|
'User-Agent': config.user_agent
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
.on('error', rej)
|
|
||||||
.on('end', () => {
|
req.pipe(writable);
|
||||||
|
|
||||||
|
req.on('response', response => {
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
writable.close();
|
writable.close();
|
||||||
res();
|
rej(response.statusCode);
|
||||||
})
|
}
|
||||||
.pipe(writable)
|
});
|
||||||
.on('error', rej);
|
|
||||||
|
req.on('error', error => {
|
||||||
|
writable.close();
|
||||||
|
rej(error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const instance = await fetchMeta();
|
const instance = await fetchMeta();
|
||||||
|
|
Loading…
Reference in a new issue