Skip to content

Commit

Permalink
feat(image-request.ts): incorporate logic getting image from either s…
Browse files Browse the repository at this point in the history
…3 or external url
  • Loading branch information
wjjmjh committed May 24, 2024
1 parent 0a1702e commit f3cf3a9
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/controllers/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,17 @@ interface HandlerResponse {
export const handle = async (
request: CloudFrontRequest,
): Promise<HandlerResponse> => {
const uri = request.uri;

if (uri === undefined) {
return {
statusCode: '400',
body: 'Invalid Request',
headers: {
'Content-Type': 'text/plain',
},
};
}

logger.info({
message: 'valid uri',
uri,
});

const queryString = new URLSearchParams(request.querystring);

logger.info({
queryString,
});

const getFromExternal = queryString.get('getFromExternal') === 'true';
const externalUrl = queryString.get('externalUrl');
const width = parseInt(queryString.get('width') ?? '0');
logger.info({
width,
});

const quality = parseInt(queryString.get('quality') ?? '75');

logger.info({
getFromExternal,
externalUrl,
width,
quality,
});

Expand All @@ -111,13 +91,33 @@ export const handle = async (
};
}

let imagePath = request.uri;
if (getFromExternal && externalUrl) {
imagePath = externalUrl;
}

if (!imagePath) {
return {
statusCode: '400',
body: 'Invalid Request',
headers: {
'Content-Type': 'text/plain',
},
};
}

logger.info({
message: 'valid uri or externalUrl',
imagePath,
});

const format = bestAcceptedFormat(request.headers['accept']);
logger.info({
message: `accepted format: ${format}`,
});

const result = await imageService.getOptimisedImage(
uri,
imagePath,
width,
Math.min(quality, 75),
format,
Expand All @@ -134,7 +134,7 @@ export const handle = async (
}

logger.info({
message: 'succesfully generated optimised image',
message: 'successfully generated optimised image',
});

return {
Expand Down

0 comments on commit f3cf3a9

Please sign in to comment.