Skip to content

Commit

Permalink
UPLOAD_FROM_SOURCE setting
Browse files Browse the repository at this point in the history
  • Loading branch information
yo-han committed Sep 17, 2024
1 parent 3dfba06 commit de59ad4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
7 changes: 5 additions & 2 deletions init-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const envVariables = [
{ name: 'LIVE_SOURCE_URL', description: 'The live source URL (e.g., https://cdn.example.com/path/)' },
{ name: 'LIVE_PUBLIC_DOMAIN', description: 'The live public domain (e.g., https://cf.example.com)' },
{ name: 'KV_NAMESPACE_ID', description: 'Your KV namespace ID' },
{ name: 'CACHE_KEY_PREFIX', description: 'Cache key prefix' }
{ name: 'CACHE_KEY_PREFIX', description: 'Cache key prefix' },
{ name: 'UPLOAD_FROM_SOURCE', description: 'Upload from source (true/false)' },
{ name: 'RATELIMIT_ENABLED', description: 'Rate limiting enabled (true/false)' }
];

const envValues = {};
Expand Down Expand Up @@ -69,7 +71,8 @@ function updateWranglerToml() {
'<cloudflare-api-token>': envValues.API_TOKEN,
'<account_id>': envValues.ACCOUNT_ID,
'<account_hash>': envValues.ACCOUNT_HASH,
'<rate_limit>': process.env.RATELIMIT_ENABLED === 'true' ? 'true' : 'false',
'<rate_limit>': envValues.RATELIMIT_ENABLED === 'true' ? 'true' : 'false',
'<upload_from_source>': envValues.UPLOAD_FROM_SOURCE === 'true' ? 'true' : 'false',
'https://cdn.example.com/path/': envValues.LIVE_SOURCE_URL,
'https://cf.example.com': envValues.LIVE_PUBLIC_DOMAIN
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"init": "node init-config.js",
"update-config": "node update-config.js",
"build": "npm run update-config && wrangler build",
"build": "npm run update-config && wrangler deploy --dry-run --outdir=dist",
"deploy": "npm run build && wrangler deploy",
"test": "jest",
"test:watch": "jest --watch",
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ async function fetchOriginalCloudflareImage(env: Env, id: string): Promise<Respo
headers: { 'Authorization': `Bearer ${env.API_TOKEN}` },
});

// TODO: Handle 404s when original source url is not active
// if (!response.ok) {
// throw new CloudflareApiError('Failed to fetch original image', response.status, await response.text());
// }
if (!response.ok && env.UPLOAD_FROM_SOURCE === false) {
throw new CloudflareApiError('Failed to fetch original image', response.status, await response.text());
}

return response;
}
Expand Down Expand Up @@ -137,7 +136,7 @@ export async function handleImageRequest(request: Request, env: Env): Promise<Re
imageResponse = await fetchOriginalCloudflareImage(env, id);
}

if (!imageResponse || !imageResponse.ok) {
if (env.UPLOAD_FROM_SOURCE && (!imageResponse || !imageResponse.ok)) {
const sourceUrl = `${env.LIVE_SOURCE_URL}/${image_path}.${extension}`;
const uploadResponse = await uploadToCloudflareImages(env, sourceUrl, id);

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export interface Env {
LIVE_SOURCE_URL: string;
LIVE_PUBLIC_DOMAIN: string;
KV_STORE: KVNamespace;
RATELIMIT_ENABLED: string;
RATELIMIT_ENABLED: boolean;
CACHE_KEY_PREFIX: string;
UPLOAD_FROM_SOURCE: boolean;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion update-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const replacements = {
'<bucket_name>': process.env.BUCKET,
'<cachekey_prefix>': process.env.CACHE_KEY_PREFIX,
'<kv_namespace_id>': process.env.KV_NAMESPACE_ID,
'<rate_limit>': process.env.RATELIMIT_ENABLED === 'true' ? 'true' : 'false'
'<rate_limit>': process.env.RATELIMIT_ENABLED === 'true' ? 'true' : 'false',
'<upload_from_source>': process.env.UPLOAD_FROM_SOURCE === 'true' ? 'true' : 'false'
};

for (const [placeholder, envValue] of Object.entries(replacements)) {
Expand Down
3 changes: 2 additions & 1 deletion wrangler.toml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ ACCOUNT_HASH = "<account_hash>"
LIVE_SOURCE_URL = "https://cdn.example.com/path/"
LIVE_PUBLIC_DOMAIN = "https://cf.example.com"
CACHE_KEY_PREFIX = "<cachekey_prefix>"
RATELIMIT_ENABLED = "<ratelimit>"
RATELIMIT_ENABLED = <rate_limit>
UPLOAD_FROM_SOURCE = <upload_from_source>

[[ r2_buckets ]]
binding = "R2_IMAGES_BUCKET"
Expand Down

0 comments on commit de59ad4

Please sign in to comment.