fix: regex and id extraction for giphy media urls (#22276)

* fix regex and id extraction for giphy media urls

* Update get.ts

* Update get.ts

* tweak

---------

Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
This commit is contained in:
abhix4
2025-09-01 18:28:00 +00:00
committed by GitHub
co-authored by Anik Dhabal Babu
parent 17ef0dc12b
commit 40d70cd1d4
+5 -3
View File
@@ -3,7 +3,9 @@ import { z, ZodError } from "zod";
import { GiphyManager } from "../lib";
const giphyUrlRegexp = new RegExp("^https://(.*).giphy.com/media/(.*)/giphy.gif(.*)");
const giphyUrlRegexp = new RegExp(
"^https?:\\/\\/(?:[\\w.-]+\\.)?giphy\\.com\\/media\\/(?:[^/]+\\/)*([a-zA-Z0-9_-]+)\\/giphy\\.gif(?:\\?.*)?$"
);
const getSchema = z.object({
url: z.string().regex(giphyUrlRegexp, "Giphy URL is invalid"),
@@ -26,10 +28,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const sanitisedUrl = parsedUrl.origin + parsedUrl.pathname;
// Extract Giphy ID from embed url
const matches = giphyUrlRegexp.exec(sanitisedUrl);
if (!matches || matches.length < 3) {
if (!matches || matches.length < 2) {
return res.status(400).json({ message: "Giphy URL is invalid" });
}
const giphyId = matches[2];
const giphyId = matches[1];
const gifImageUrl = await GiphyManager.getGiphyById(giphyId);
return res.status(200).json({ image: gifImageUrl });
} catch (error: unknown) {