diff --git a/api/index.js b/api/index.js index 70d3a30..77e4739 100644 --- a/api/index.js +++ b/api/index.js @@ -20,7 +20,10 @@ app.post("/run", express.json(), async (req, res, next) => { try { let type; if (object.path) { - type = object.type ? object.type : await magick.getType(object.path); + type = object.type; + if (!object.type) { + type = await magick.getType(object.path); + } if (!type) { return res.sendStatus(400); } @@ -92,4 +95,4 @@ app.get("/image", (req, res) => { app.listen(port, () => { console.log(`Started image API on port ${port}.`); -}); \ No newline at end of file +}); diff --git a/utils/image.js b/utils/image.js index b03df0f..831b996 100644 --- a/utils/image.js +++ b/utils/image.js @@ -64,6 +64,13 @@ exports.check = (cmd) => { }; exports.getType = async (image) => { + if (!image.startsWith("http")) { + imageType = await fileType.fromFile(image) + if (imageType && formats.includes(imageType.mime)) { + return imageType.mime; + } + return undefined + } let type; const controller = new AbortController(); const timeout = setTimeout(() => { @@ -86,4 +93,4 @@ exports.getType = async (image) => { clearTimeout(timeout); } return type; -}; \ No newline at end of file +};