pomdtr-ejm.web.val.run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { encodeBase64 } from "jsr:@std/encoding@0.224.3/base64";
import { contentType } from "jsr:@std/media-types@0.224.1";
import { extname } from "jsr:@std/path@0.225.2";
export default async function(req: Request) {
const url = new URL(req.url);
if (url.pathname == "/") {
return new Response("Invalid URL", { status: 400 });
}
const target = "https://" + url.pathname
const srcURL = new URL(target);
const resp = await fetch(srcURL);
if (!resp.ok) {
return new Response("not found", { status: 404 });
}
return Response.json({
b64: encodeBase64(await resp.arrayBuffer()),
contentType: contentType(extname(srcURL.pathname)) || "text/plain",
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 23, 2024