From 574168ad92158ed2eadae4894f68d22777dfa70e Mon Sep 17 00:00:00 2001 From: nzambello Date: Mon, 25 Mar 2024 11:31:12 +0200 Subject: [PATCH] add preview with try now --- README.md | 2 +- index.ts | 91 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5b840bd..9ffc74a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Then call the API as, for example: /api/imgresize/200/200/https%3A%2F%2Fmemori.ai%2Flogo.png ``` -You can also specify a format using the querystring `?format=` and indicating one of the following: avif, gif, heif, jpeg, jpg, jp2, pdf, png, svg, tiff, webp. +You can also specify a format using the querystring `?format=` and indicating one of the following: `avif`, `gif`, `heif`, `jpeg`, `jpg`, `pdf`, `png`, `svg`, `tiff`, `webp`. (Note: Experimental!) ## Docker diff --git a/index.ts b/index.ts index 33840ab..c3c58dc 100644 --- a/index.ts +++ b/index.ts @@ -23,24 +23,72 @@ const homepage = html`

Resize images API

+ -
-

The structure of the API path is:

-

/api/imgresize/:width/:height/:url

-

Where width and height can be numbers in pixels or auto.

-

The URL should be URL encoded, which can be done in JS with encodeURIComponent. See MDN ref.

-

Example for https://memori.ai/logo.png:

-

https%3A%2F%2Fmemori.ai%2Flogo.png

-

Then call the API as, for example:

-

/api/imgresize/200/200/https%3A%2F%2Fmemori.ai%2Flogo.png

-

You can also specify a format using the querystring ?format= and indicating one of the following: avif, gif, heif, jpeg, jpg, jp2, pdf, png, svg, tiff, webp.

+
+

Try now

+
+
+ + + +
+ +
+
+
+

Docs

+

The structure of the API path is:

+

/api/imgresize/:width/:height/:url

+

Where width and height can be numbers in pixels or auto.

+

The URL should be URL encoded, which can be done in JS with encodeURIComponent. See MDN ref.

+

Example for https://memori.ai/logo.png:

+

https%3A%2F%2Fmemori.ai%2Flogo.png

+

Then call the API as, for example:

+

/api/imgresize/200/200/https%3A%2F%2Fmemori.ai%2Flogo.png

+

You can also specify a format using the querystring ?format= and indicating one of the following: avif, gif, heif, jpeg, jpg, jp2, pdf, png, svg, tiff, webp. Note: Experimental!

@@ -57,6 +105,27 @@ app.get("/", (c) => { return c.html(homepage); }); +app.post("/api/preview", async (c) => { + const body = await c.req.parseBody(); + + c.header("Cache-Control", "s-maxage=31536000, stale-while-revalidate"); + + const url = body["url"]; + + if (!url || typeof url !== "string") { + c.status(400); + return c.json({ error: "No URL provided" }); + } + + const width = body["width"]; + const height = body["height"]; + + const w = width && width !== "auto" ? Number(width) : 200; + const h = height && height !== "auto" ? Number(height) : 200; + + return c.redirect(`/api/imgresize/${w}/${h}/${encodeURIComponent(url)}`); +}); + app.get("/api/imgresize/:width/:height/:url", async (c) => { const { width, height, url } = c.req.param(); const format = c.req.query("format");