Skip to content

Commit

Permalink
wip(files-component): refactor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Oct 9, 2019
1 parent fbc8355 commit 3f93a76
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 49 deletions.
31 changes: 14 additions & 17 deletions components/serverless-files/functions/images/imageLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,44 @@ module.exports = {
return SUPPORTED_IMAGES.includes(file.extension);
},
async process({ s3, file, options }) {
let params, key;
let objectParams;

const sanitizedTransformations = sanitizeImageTransformations(options);
const transformations = sanitizeImageTransformations(options);

if (
!sanitizedTransformations.empty &&
SUPPORTED_TRANSFORMABLE_IMAGES.includes(file.extension)
) {
key = getImageKey(file.name, sanitizedTransformations.transformations);
params = getObjectParams(key);
if (transformations && SUPPORTED_TRANSFORMABLE_IMAGES.includes(file.extension)) {

objectParams = getObjectParams(getImageKey(file.name, transformations));
console.log('transformationsszzzz', transformations, objectParams)
try {
return await s3.getObject(params).promise();
return await s3.getObject(objectParams).promise();
} catch (e) {
let imageTransformerLambdaResponse = await callImageTransformerLambda({
key: file.name,
transformations: sanitizedTransformations
transformations
});

if (imageTransformerLambdaResponse.error) {
throw Error(imageTransformerLambdaResponse.message);
}

return await s3.getObject(params).promise();
return await s3.getObject(objectParams).promise();
}
}

key = getImageKey(file.name);
params = getObjectParams(key);
console.log('nista od trasnforma')
objectParams = getObjectParams(getImageKey(file.name));
try {
return await s3.getObject(params).promise();
return await s3.getObject(objectParams).promise();
} catch (e) {
let imageTransformerLambdaResponse = await callImageTransformerLambda({
key: file.name,
transformations: sanitizedTransformations
key: file.name
});

if (imageTransformerLambdaResponse.error) {
throw Error(imageTransformerLambdaResponse.message);
}

return await s3.getObject(params).promise();
return await s3.getObject(objectParams).promise();
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ const SUPPORTED_IMAGE_RESIZE_WIDTHS = [100, 300, 500, 750, 1000, 1500, 2500];
* Takes only allowed transformations into consideration, and discards the rest.
*/
module.exports = args => {
const output = { transformations: {}, empty: true };
const transformations = {};

if (args) {
let width = parseInt(args.width);
if (width > 0) {
output.empty = false;
output.transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[0];
transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[0];
let i = SUPPORTED_IMAGE_RESIZE_WIDTHS.length;
while (i >= 0) {
if (width === SUPPORTED_IMAGE_RESIZE_WIDTHS[i]) {
output.transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];
transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];
break;
}

if (width > SUPPORTED_IMAGE_RESIZE_WIDTHS[i]) {
// Use next larger width. If there isn't any, use current.
output.transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[i + 1];
if (!output.transformations.width) {
output.transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];
transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[i + 1];
if (!transformations.width) {
transformations.width = SUPPORTED_IMAGE_RESIZE_WIDTHS[i];
}
break;
}
Expand All @@ -33,5 +32,9 @@ module.exports = args => {
}
}

return output;
if (Object.keys(transformations).length > 0) {
return transformations;
}

return null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports.handler = async ({ body: { transformations, key } }) => {
optimizedTransformed: getObjectParams(getImageKey(key, transformations))
};

console.log("svi moguci paramsi", params);
// 1. Get optimized image.
try {
optimizedImageObject = await s3.getObject(params.optimized).promise();
Expand All @@ -26,37 +27,31 @@ module.exports.handler = async ({ body: { transformations, key } }) => {
optimizedImageObject = await s3.getObject(params.initial).promise();

await s3
.createObject({
params: {
...params.optimized,
ContentType: optimizedImageObject.ContentType,
Body: await optimizeImage(
optimizedImageObject.Body,
optimizedImageObject.ContentType
)
}
.putObject({
...params.optimized,
ContentType: optimizedImageObject.ContentType,
Body: await optimizeImage(
optimizedImageObject.Body,
optimizedImageObject.ContentType
)
})
.promise();

console.log("ponovo dohvacam params.optimized", params.optimized);
optimizedImageObject = await s3.getObject(params.optimized);
}

// 2. If no transformations requested, just exit.
if (transformations.empty) {
if (!transformations) {
return { error: false, message: "" };
}

// 3. If transformations requested, apply them in save it into the bucket.
await s3
.createObject({
params: {
...params.optimizedTransformed,
ContentType: optimizedImageObject.ContentType,
Body: await processImage(
optimizedImageObject.Body,
transformations.transformations
)
}
.putObject({
...params.optimizedTransformed,
ContentType: optimizedImageObject.ContentType,
Body: await processImage(optimizedImageObject.Body, transformations)
})
.promise();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const sharp = require("sharp");

/**
* Only processing "width" at the moment.
* Check "sanitizeImageOptions.js" to allow additional image processing options.
* Check "sanitizeImagetransformations.js" to allow additional image processing transformations.
* @param buffer
* @param options
* @param transformations
* @returns {Promise<void>}
*/
module.exports = async (buffer, options) => {
const { width } = options;
module.exports = async (buffer, transformations) => {
const { width } = transformations;
return await sharp(buffer)
.resize({ width })
.toBuffer();
Expand Down
2 changes: 1 addition & 1 deletion components/serverless-files/functions/images/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getImageKey = (key, options) => {
return `${OPTIMIZED_IMAGE_PREFIX}${objectHash(key)}_${key}`;
}

return `${OPTIMIZED_TRANSFORMED_IMAGE_PREFIX}${objectHash(key)}_${objectHash(options)}_{${key}`;
return `${OPTIMIZED_TRANSFORMED_IMAGE_PREFIX}${objectHash(key)}_${objectHash(options)}_${key}`;
};

module.exports = {
Expand Down

0 comments on commit 3f93a76

Please sign in to comment.