This commit is contained in:
parent
6a0350f13c
commit
787bfb43d4
1 changed files with 11 additions and 5 deletions
|
@ -46,12 +46,12 @@ module.exports = {
|
||||||
// TODO: Download with correct extension.
|
// TODO: Download with correct extension.
|
||||||
message.channel.sendTyping();
|
message.channel.sendTyping();
|
||||||
let downloadResult = await this.downloadImage(url, path.resolve(directory, "input.png"));
|
let downloadResult = await this.downloadImage(url, path.resolve(directory, "input.png"));
|
||||||
if(downloadResult.value != 0){
|
if(downloadResult.value != this.ERROR_CODES.SUCCESS){
|
||||||
if(downloadResult.value === 3){
|
if(downloadResult.value === this.ERROR_CODES.FETCH_ERROR){
|
||||||
message.channel.send(`Failed to download the provided image, got error '${downloadResult.errorMessage}'`);
|
message.channel.send(`Failed to download the provided image, got error '${downloadResult.errorMessage}'`);
|
||||||
}else if (downloadResult.value === 1){
|
}else if (downloadResult.value === this.ERROR_CODES.HTTP_ERROR){
|
||||||
message.channel.send(`Failed to download the provided image, got response status '${downloadResult.errorMessage}'`);
|
message.channel.send(`Failed to download the provided image, got response status '${downloadResult.errorMessage}'`);
|
||||||
}else if(downloadResult.value === 2){
|
}else if(downloadResult.value === this.ERROR_CODES.NOT_IMAGE){
|
||||||
message.channel.send(`The provided url was not an image.`)
|
message.channel.send(`The provided url was not an image.`)
|
||||||
}
|
}
|
||||||
fs.rmSync(`${directory}`, {recursive: true})
|
fs.rmSync(`${directory}`, {recursive: true})
|
||||||
|
@ -90,7 +90,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
// https://stackoverflow.com/a/77210219
|
// https://stackoverflow.com/a/77210219
|
||||||
async downloadImage(url, path) {
|
async downloadImage(url, path) {
|
||||||
let res = new Response()
|
let res;
|
||||||
try {
|
try {
|
||||||
res = await fetch(url);
|
res = await fetch(url);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -102,6 +102,12 @@ module.exports = {
|
||||||
const stream = Readable.fromWeb(res.body)
|
const stream = Readable.fromWeb(res.body)
|
||||||
await writeFile(path, stream);
|
await writeFile(path, stream);
|
||||||
return {value: 0, errorMessage: ""};
|
return {value: 0, errorMessage: ""};
|
||||||
|
},
|
||||||
|
ERROR_CODES: {
|
||||||
|
SUCCESS: 0,
|
||||||
|
HTTP_ERROR: 1,
|
||||||
|
NOT_IMAGE: 2,
|
||||||
|
FETCH_ERROR: 3
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue