tdoss: Improve error handling of downloadImage + add the tdoss template
All checks were successful
CI / CI (push) Successful in 1m22s

Also use tdoss directory in the data/ dir so I won't have a bunch of
random directories in the data dir.
This commit is contained in:
SileNce5k 2025-05-10 15:02:59 +02:00
parent 2f371d00ed
commit aa2ab653b9
Signed by: SileNce
GPG key ID: B0A142BB4291B204
2 changed files with 31 additions and 12 deletions

View file

@ -10,9 +10,11 @@ module.exports = {
description: 'Combine picture with tdoss album cover template', description: 'Combine picture with tdoss album cover template',
async execute({ message, args }) { async execute({ message, args }) {
let dataDir = path.resolve(__dirname, '..', '..', 'data'); let tdossDir = path.resolve(process.cwd(), 'data', 'tdoss');
const directory = path.resolve(dataDir, Math.floor(new Date).toString()) const tdossTemplate = path.resolve(process.cwd(), 'resources', 'tdoss_template.png');
fs.mkdirSync(directory)
const directory = path.resolve(tdossDir, Math.floor(new Date).toString())
fs.mkdirSync(directory, {recursive: true})
let url = ""; let url = "";
if(message.attachments.size > 0){ if(message.attachments.size > 0){
@ -44,14 +46,21 @@ module.exports = {
} }
// TODO: Download with correct extension. // TODO: Download with correct extension.
message.channel.sendTyping(); message.channel.sendTyping();
if(await this.downloadImage(url, path.resolve(directory, "input.png")) != 0){ let downloadResult = await this.downloadImage(url, path.resolve(directory, "input.png"));
message.channel.send("Something went wrong during the download.\nThe link might be unreachable for the bot or it's not an image.") if(downloadResult.value != this.ERROR_CODES.SUCCESS){
if(downloadResult.value === this.ERROR_CODES.FETCH_ERROR){
message.channel.send(`Failed to download the provided image, got error '${downloadResult.errorMessage}'`);
}else if (downloadResult.value === this.ERROR_CODES.HTTP_ERROR){
message.channel.send(`Failed to download the provided image, got response status '${downloadResult.errorMessage}'`);
}else if(downloadResult.value === this.ERROR_CODES.NOT_IMAGE){
message.channel.send(`The provided url was not an image.`)
}
fs.rmSync(`${directory}`, {recursive: true}) fs.rmSync(`${directory}`, {recursive: true})
return; return;
} }
const command = `magick ${dataDir}/tdoss_template.png \\( ${directory}/input.png -resize 800x800^ -gravity center -extent 1000x1000 \\) -compose dst-over -composite ${directory}/tdoss_result.png`; const command = `magick ${tdossTemplate} \\( ${directory}/input.png -resize 800x800^ -gravity center -extent 1000x1000 \\) -compose dst-over -composite ${directory}/tdoss_result.png`;
if (this.executeCommand(command).error === true) { if (this.executeCommand(command).error === true) {
message.channel.send("Something went wrong during image manipulation.\nTry again and if it keeps happening, contact the owner of the bot.") message.channel.send("Something went wrong during image manipulation.\nTry again and if it keeps happening, contact the owner of the bot.")
fs.rmSync(`${directory}`, {recursive: true}) fs.rmSync(`${directory}`, {recursive: true})
@ -82,14 +91,24 @@ module.exports = {
}, },
// https://stackoverflow.com/a/77210219 // https://stackoverflow.com/a/77210219
async downloadImage(url, path) { async downloadImage(url, path) {
const res = await fetch(url); let res;
if(!res.ok) return 1; try {
if(!res.headers.get('content-type').startsWith("image")) return 2; res = await fetch(url);
} catch (error) {
return {value: this.ERROR_CODES.FETCH_ERROR, errorMessage: error.cause?.message || error.message};
}
if(!res.ok) return {value: this.ERROR_CODES.HTTP_ERROR, errorMessage: res.status.toString()};
const contentType = res.headers.get('content-type');
if(!contentType || !contentType.startsWith("image")) return {value: this.ERROR_CODES.NOT_IMAGE, errorMessage: contentType || "No content-type header"};
const stream = Readable.fromWeb(res.body) const stream = Readable.fromWeb(res.body)
await writeFile(path, stream); await writeFile(path, stream);
return 0; return {value: this.ERROR_CODES.SUCCESS, errorMessage: ""};
},
ERROR_CODES: {
SUCCESS: 0,
HTTP_ERROR: 1,
NOT_IMAGE: 2,
FETCH_ERROR: 3
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 KiB