[workers] Add the cast-albums worker
This commit is contained in:
parent
5092b74503
commit
7c383023e4
4 changed files with 68 additions and 0 deletions
9
infra/workers/cast-albums/package.json
Normal file
9
infra/workers/cast-albums/package.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "cast-albums",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20240314.0",
|
||||
"typescript": "^5",
|
||||
"wrangler": "^3"
|
||||
}
|
||||
}
|
50
infra/workers/cast-albums/src/index.ts
Normal file
50
infra/workers/cast-albums/src/index.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
/** Proxy file and thumbnail requests from the cast web app */
|
||||
|
||||
export default {
|
||||
async fetch(request: Request) {
|
||||
switch (request.method) {
|
||||
case "GET":
|
||||
return handleGET(request);
|
||||
case "OPTIONS":
|
||||
return handleOPTIONS(request);
|
||||
default:
|
||||
throw new Error(
|
||||
`HTTP 405 Method Not Allowed: ${request.method}`
|
||||
);
|
||||
}
|
||||
},
|
||||
} satisfies ExportedHandler;
|
||||
|
||||
const handleGET = async (request: Request) => {
|
||||
const url = new URL(request.url);
|
||||
const urlParams = new URLSearchParams(url.search);
|
||||
const token =
|
||||
request.headers.get("X-Cast-Access-Token") ??
|
||||
urlParams.get("castToken");
|
||||
|
||||
const fileID = urlParams.get("fileID");
|
||||
const pathname = url.pathname;
|
||||
|
||||
let response = await fetch(
|
||||
`https://api.ente.io/cast/files${pathname}${fileID}?castToken=${token}`
|
||||
);
|
||||
|
||||
response = new Response(response.body, response);
|
||||
response.headers.set("Access-Control-Allow-Origin", "*");
|
||||
return response;
|
||||
};
|
||||
|
||||
const handleOPTIONS = (request: Request) => {
|
||||
let corsHeaders: Record<string, string> = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET,OPTIONS",
|
||||
"Access-Control-Max-Age": "86400",
|
||||
};
|
||||
|
||||
const acrh = request.headers.get("Access-Control-Request-Headers");
|
||||
if (acrh) {
|
||||
corsHeaders["Access-Control-Allow-Headers"] = acrh;
|
||||
}
|
||||
|
||||
return new Response("", { headers: corsHeaders });
|
||||
};
|
1
infra/workers/cast-albums/tsconfig.json
Normal file
1
infra/workers/cast-albums/tsconfig.json
Normal file
|
@ -0,0 +1 @@
|
|||
{ "extends": "../tsconfig.base.json", "include": ["src/**/*.ts"] }
|
8
infra/workers/cast-albums/wrangler.toml
Normal file
8
infra/workers/cast-albums/wrangler.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
name = "cast-albums"
|
||||
main = "src/index.ts"
|
||||
compatibility_date = "2024-03-14"
|
||||
|
||||
[[routes]]
|
||||
pattern = "cast-albums.ente.io"
|
||||
zone_name = "ente.io"
|
||||
custom_domain = true
|
Loading…
Add table
Reference in a new issue