Skip to content

Commit

Permalink
update project to sync CF
Browse files Browse the repository at this point in the history
  • Loading branch information
enapupe committed Jun 3, 2024
1 parent b1c40cd commit 3d8a2f6
Showing 1 changed file with 61 additions and 5 deletions.
66 changes: 61 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
# media-server

This project is a basic express server image resizer "proxy" coupled with Google Storage API.
The main goal of this project is to reduce the payload served to users. We also make use of heavy caching on Cloud Flare to be sure images aren't processed more than once.
This project has been replaced by the following CloudFlare worker:

## development
```
the main development branch is `develop`, whenever it is merged an automatic service deployment to staging happens. similarly when a `release`is performed, the main production service is deployed.
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
the code should be formatted with standard prettier config, even though there are no active linters configured currently.
const getBucket = (hostname) => {
if (hostname === "stg-media.openbeta.io") {
return "https://storage.googleapis.com/openbeta-staging";
}
return "https://storage.googleapis.com/openbeta-prod";
};
/**
* Fetch and log a request
* @param {Request} request
*/
async function handleRequest(request) {
// Parse request URL to get access to query string
const url = new URL(request.url);
if (url.pathname.length < 2)
return new Response("Missing image", { status: 400 });
if (!/\.(jpe?g|png|gif|webp)$/i.test(url.pathname)) {
return new Response("Disallowed file extension", { status: 400 });
}
// Cloudflare-specific options are in the cf object.
let options = { cf: { image: {} } };
// Copy parameters from query string to request options.
// You can implement various different parameters here.
if (url.searchParams.has("w"))
options.cf.image.width = url.searchParams.get("w");
if (url.searchParams.has("q"))
options.cf.image.quality = url.searchParams.get("q");
if (url.searchParams.has("h"))
options.cf.image.height = url.searchParams.get("h");
// Your Worker is responsible for automatic format negotiation. Check the Accept header.
const accept = request.headers.get("Accept");
if (/image\/avif/.test(accept)) {
options.cf.image.format = "avif";
} else if (/image\/webp/.test(accept)) {
options.cf.image.format = "webp";
}
const bucket = getBucket(url.hostname);
const imageURL = `${bucket}${url.pathname}`;
// Build a request that passes through request headers
const imageRequest = new Request(imageURL, {
headers: request.headers,
});
let response = await fetch(imageRequest, options);
response = new Response(response.body, response);
response.headers.set("Cache-Control", "public, max-age=15552000");
return response;
}
```

0 comments on commit 3d8a2f6

Please sign in to comment.