Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[workers] Add the cast-albums worker

Manav Rathi7c383023e426
diff --git a/infra/workers/cast-albums/package.json b/infra/workers/cast-albums/package.json
new file mode 100644
index 0000000000..7d87162d29
--- /dev/null
+++ b/infra/workers/cast-albums/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "cast-albums",
+ "private": true,
+ "devDependencies": {
+ "@cloudflare/workers-types": "^4.20240314.0",
+ "typescript": "^5",
+ "wrangler": "^3"
+ }
+}
diff --git a/infra/workers/cast-albums/src/index.ts b/infra/workers/cast-albums/src/index.ts
new file mode 100644
index 0000000000..d76c333755
--- /dev/null
+++ b/infra/workers/cast-albums/src/index.ts
@@ -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 });
+};
diff --git a/infra/workers/cast-albums/tsconfig.json b/infra/workers/cast-albums/tsconfig.json
new file mode 100644
index 0000000000..70c70a35da
--- /dev/null
+++ b/infra/workers/cast-albums/tsconfig.json
@@ -0,0 +1 @@
+{ "extends": "../tsconfig.base.json", "include": ["src/**/*.ts"] }
diff --git a/infra/workers/cast-albums/wrangler.toml b/infra/workers/cast-albums/wrangler.toml
new file mode 100644
index 0000000000..c070a5c246
--- /dev/null
+++ b/infra/workers/cast-albums/wrangler.toml
@@ -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