Skip to content

Commit

Permalink
fix: error handling for no json plus for when file name too long
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Williams <[email protected]>
  • Loading branch information
technovangelist committed Jan 29, 2024
1 parent 8015f3f commit e58993f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,27 @@ export async function getkeywords(image: string): Promise<string[]> {
body: JSON.stringify(body),
});

const json = await response.json();
const keywords = JSON.parse(json.response);
if (response.status !== 200) {
console.log(`Error: ${response.status}: ${response.statusText}`);
return [];
} else {
const json = await response.json();
const keywords = JSON.parse(json.response);
return keywords?.keywords || [];
}

return keywords?.keywords || [];
}

function createFileName(keywords: string[], fileext: string): string {
const fileparts = keywords.map(k => k.replace(/ /g, "_"));
const newfilename = fileparts.join("-") + "." + fileext;
let newfilename = "";
if (keywords.length > 0) {
const fileparts = keywords.map(k => k.replace(/ /g, "_"));
const filteredWords = fileparts.filter(w => {
const cl = newfilename.length + w.length;
return cl <= 230
})
newfilename = filteredWords.join("-") + "." + fileext;
}
return newfilename;
}

Expand Down

0 comments on commit e58993f

Please sign in to comment.