Skip to content

Commit 35daaf4

Browse files
committed
get media filename from request
1 parent 225b0fd commit 35daaf4

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

src/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { resolveConfig } from './utils/config.js';
1515
import handlers from './routes/index.js';
1616

1717
/**
18-
* @param {Request} req
18+
* @param {import("@cloudflare/workers-types/experimental").Request} req
1919
*/
2020
async function parseData(req) {
2121
if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
@@ -41,7 +41,7 @@ async function parseData(req) {
4141

4242
/**
4343
* @param {import("@cloudflare/workers-types/experimental").ExecutionContext} pctx
44-
* @param {Request} req
44+
* @param {import("@cloudflare/workers-types/experimental").Request} req
4545
* @param {Env} env
4646
* @returns {Promise<Context>}
4747
*/
@@ -51,6 +51,8 @@ export async function makeContext(pctx, req, env) {
5151
const ctx = pctx;
5252
// @ts-ignore
5353
ctx.attributes = {};
54+
// @ts-ignore
55+
ctx.request = req;
5456
ctx.env = env;
5557
ctx.url = new URL(req.url);
5658
ctx.log = console;
@@ -69,7 +71,7 @@ export async function makeContext(pctx, req, env) {
6971

7072
export default {
7173
/**
72-
* @param {Request} request
74+
* @param {import("@cloudflare/workers-types/experimental").Request} request
7375
* @param {Env} env
7476
* @param {import("@cloudflare/workers-types/experimental").ExecutionContext} pctx
7577
* @returns {Promise<Response>}

src/routes/content/media.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export default async function handler(ctx) {
2222
log,
2323
config: { org, site },
2424
} = ctx;
25-
log.debug('handling media: ', url.pathname);
25+
log.debug('handling media: ', url.pathname, ctx.request.url);
2626

27-
const filename = url.pathname.split('/').pop();
27+
const filename = ctx.request.url.split('/').pop();
2828
const key = `${org}/${site}/media/${filename}`;
2929
log.debug('fetching media: ', key, filename);
3030
const resp = await env.CATALOG_BUCKET.get(key);

src/routes/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import config from './config/handler.js';
1616
import auth from './auth/handler.js';
1717

1818
/**
19-
* @type {Record<string, (ctx: Context, request: Request) => Promise<Response>>}
19+
* @type {Record<
20+
* string,
21+
* (
22+
* ctx: Context,
23+
* request: import("@cloudflare/workers-types/experimental").Request
24+
* ) => Promise<Response>
25+
* >}
2026
*/
2127
export default {
2228
content,

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ declare global {
247247
env: Env;
248248
log: Console;
249249
config: Config;
250+
request: Request;
250251
/** parsed from body or query params */
251252
data: any;
252253
info: {

0 commit comments

Comments
 (0)