Skip to content

Commit 4d22961

Browse files
committedFeb 15, 2024
async bug fixes and thumbnail uploads
1 parent f205f3a commit 4d22961

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed
 

‎Pulumi.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { updateEnv } from './pulumi/env.mjs';
55

66
const { uploadsBucket, uploadsAccessKey } = createUploads();
77
const thumbnailsBucket = createThumbnails();
8-
const processUploadsFunctionUrl = createLambdaProcessUploads();
8+
const processUploadsFunctionUrl = createLambdaProcessUploads(thumbnailsBucket);
99
updateEnv({
1010
uploadsBucket,
1111
uploadsAccessKey,

‎aws/lambda/func/processUpload/index.mjs

+34-16
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,51 @@ import { finished } from 'stream/promises';
1111
import Jimp from "jimp";
1212

1313
export const handler = async (event) => {
14-
const input = JSON.parse(event.body) || event;
15-
const url = input.url;
16-
const fileType = input.fileType;
17-
const id = input.id;
14+
let input = {};
15+
let url = '';
16+
let fileType = '';
17+
let id = '';
18+
let out = { "statusCode": 500, "body": { "error": { "code": 500, message: "Failed to start process" } } };
19+
try {
20+
input = 'body' in event ? JSON.parse(event.body) : event;
21+
url = input.url;
22+
fileType = input.fileType;
23+
id = input.id;
24+
}
25+
catch {
26+
out = formatError('Unable to initialize post processing, perhaps you did not send a valid json body.')
27+
}
1828
try {
1929
if (['bmp', 'png', 'tiff', 'webp', 'tif', 'psd', 'svg', 'jpeg', 'jpg', 'gif', 'heic', 'heif', 'avci', 'avif', 'icns', 'ico', 'j2c', 'jp2', 'ktx', 'pnm', 'pam', 'pbm', 'pfm', 'pgm', 'ppm', 'tga', 'cur', 'dds'].includes(fileType)) {
20-
return formatResponse(await getImageInfo(url));
30+
out = formatResponse(await getImageInfo(url, id));
2131
}
2232
else if (['pdf'].includes(fileType)) {
23-
return formatResponse(await getPdfInfo(url));
33+
out = formatResponse(await getPdfInfo(url));
2434
}
2535
else if (['zip'].includes(fileType)) {
26-
return formatResponse(await getZipInfo(url));
36+
out = formatResponse(await getZipInfo(url));
37+
}
38+
else {
39+
out = formatError(`File type ${fileType} cannot be processed by this service.`);
2740
}
28-
return formatError(`File type ${fileType} cannot be processed by this service.`);
2941
}
30-
catch {
31-
return formatError(`Unable to process url ${url} of type ${fileType} for id ${id}.`);
42+
catch (e) {
43+
out = formatError(`Unable to process url ${url} of type ${fileType} for id ${id}, because ${JSON.stringify(e)}`);
3244
}
45+
return out;
3346
};
3447

48+
3549
function formatError(message, code = 500) {
36-
return context.succeed({
50+
return {
3751
statusCode: code,
3852
body: {
3953
error: {
4054
code,
4155
message
4256
}
4357
}
44-
})
58+
}
4559
}
4660

4761
function formatResponse(body) {
@@ -53,6 +67,8 @@ function formatResponse(body) {
5367

5468
async function getZipInfo(url) {
5569
const filePath = await downloadFile(url);
70+
if (!fs.existsSync(filePath))
71+
return formatError(`Could not download file from ${url}`);
5672
const zip = new AdmZip(filePath);
5773
const zipEntries = zip.getEntries();
5874
const out = { files: [], sizeInBytes: getFileSize(filePath) };
@@ -68,13 +84,15 @@ async function getZipInfo(url) {
6884
return out;
6985
}
7086

71-
async function getImageInfo(url) {
87+
async function getImageInfo(url, id) {
7288
const filePath = await downloadFile(url);
89+
if (!fs.existsSync(filePath))
90+
return formatError(`Could not download file from ${url}`);
7391
const out = sizeOf(filePath);
7492
if (['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tif', 'tiff'].includes(out.type)) {
75-
Jimp.read(filePath).then(image => {
76-
image.resize(out.width > out.height ? 300 : Jimp.AUTO, out.width < out.height ? 300 : Jimp.AUTO).write('/tmp/thumbnail.png');
77-
});
93+
const image = await Jimp.read(filePath);
94+
await image.resize(out.width > out.height ? 300 : Jimp.AUTO, out.width < out.height ? 300 : Jimp.AUTO).write('/tmp/thumbnail.png');
95+
await uploadThumbnail('/tmp/thumbnail.png', `${id}.png`, process.env.AWS_THUMBNAILS_BUCKET);
7896
out.thumbnail = true;
7997
}
8098
delete out.type;

‎pulumi/lambda-process-uploads.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { local } from "@pulumi/command";
44
//import * as archive from "@pulumi/archive";
55

66

7-
export const createLambdaProcessUploads = () => {
7+
export const createLambdaProcessUploads = (thumbnailsBucket) => {
88
const projectName = pulumi.getProject();
99

1010
const createNodeModsZip = new local.Command('createNodeModsZip', {
@@ -48,6 +48,11 @@ export const createLambdaProcessUploads = () => {
4848
}),
4949
timeout: 60,
5050
memorySize: 512,
51+
environment: {
52+
variables: {
53+
AWS_THUMBNAILS_BUCKET: thumbnailsBucket.id.apply(id => `"${id}"`),
54+
}
55+
},
5156
layers: [nodeModsLayer.arn],
5257
role: iamForLambda.arn,
5358
});

0 commit comments

Comments
 (0)
Please sign in to comment.