Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GET/ PATCH/ DELETE/ for stickers #184

Closed
wants to merge 13 commits into from
3 changes: 2 additions & 1 deletion config.dev.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"ENVIRONMENT": "",
"REQUIRE_API_KEY": false
"REQUIRE_API_KEY": false,
"BUCKET": "staging"
}
3 changes: 2 additions & 1 deletion config.prod.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"ENVIRONMENT": "PROD",
"REQUIRE_API_KEY": true
"REQUIRE_API_KEY": true,
"BUCKET": "prod"
}
3 changes: 2 additions & 1 deletion config.staging.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"ENVIRONMENT": "",
"REQUIRE_API_KEY": true
"REQUIRE_API_KEY": true,
"BUCKET": "staging"
}
3 changes: 3 additions & 0 deletions constants/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const INTEGRATION_TEST_NON_EXISTANT_YEAR = 1234;
export const INTEGRATION_TEST_PRIZE_ID = '__INTEGRATION_TEST_PRIZE_POST';
export const INTEGRATION_TEST_PERSISTENT_PRIZE_ID = '__INTEGRATION_TEST_PRIZE';
export const INTEGRATION_TEST_NON_EXISTANT_PRIZE_ID = 'someRandomPrizeThatDoesNotExist123';
export const INTEGRATION_TEST_STICKER_ID = '__INTEGRATION_TEST_STICKER_POST';
export const INTEGRATION_TEST_PERSISTENT_STICKER_ID = '__INTEGRATION_TEST_STICKER';
export const INTEGRATION_TEST_NON_EXISTANT_STICKER_ID = 'someRandomStickerThatDoesNotExist123';

export const INTEGRATION_TEST_PERSISTENT_REGISTRATION_PARAMETERS = {
eventId: INTEGRATION_TEST_PERSISTENT_EVENT_ID_2, // has capacity of "1"
Expand Down
111 changes: 111 additions & 0 deletions lib/s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as fileType from 'file-type';
import * as AWS from 'aws-sdk';
import helpers from './handlerHelpers';

const allowedMimes = ['image/jpeg', 'image/png', 'image/jpg', 'image/svg'];

const imageUpload = async (body) => {

try {

if (!body || !body.image || !body.mime) {

return helpers.createResponse(400, {
message: 'incorrect body on request'
});

}

if (!allowedMimes.includes(body.mime)) {

return helpers.createResponse(400, {
message: 'mime is not allowed'
});

}

let imageData = body.image;
if (body.image.substr(0, 7) === 'base64,') {

imageData = body.image.substr(7, body.image.length);

}

const buffer = Buffer.from(imageData, 'base64');
const fileInfo = await fileType.fromBuffer(buffer);
const detectedExt = fileInfo.ext;
const detectedMime = fileInfo.mime;

if (detectedMime !== body.mime) {

return helpers.createResponse(400, {
message: `mime types don't match: ${detectedMime} vs ${body.mime}`
});

}

const id = body.id;
const key = 'stickers/' + id + '.' + detectedExt;

const s3 = new AWS.S3();
await s3
.putObject({
Body: buffer,
Key: key,
ContentType: body.mime,
Bucket: process.env.stickerImagesBucket,
ACL: 'public-read',
})
.promise();

const url = `https://${process.env.stickerImagesBucket}.s3-${process.env.region}.amazonaws.com/${key}`;
return helpers.createResponse(200, {
imageURL: url,
s3ObjectKey: key
});

} catch (error) {

console.log('error', error);

return helpers.createResponse(400, {
message: error.message || 'Failed to upload image'
});

}

};

const deleteObject = async (body) => {

try {

const key = body.key;

const s3 = new AWS.S3();

await s3
.deleteObject({
Key: key,
Bucket: process.env.stickerImagesBucket
})
.promise();

return helpers.createResponse(200, { message: 'Success' });

} catch (error) {

console.log('error', error);

return helpers.createResponse(400, {
message: error.message || 'Failed to upload image'
});

}

};

export default {
imageUpload,
deleteObject
};
86 changes: 82 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"type": "module",
"dependencies": {
"file-type": "^16.3.0",
"serverless": "^1.67.2"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions services/registrations/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ export const get = async (event, ctx, callback) => {

const queryString = event.queryStringParameters;
if(!queryString || (!queryString.eventID && !queryString.year && !queryString.email)) throw helpers.missingIdQueryResponse('eventID/year/user ');
const email = queryString.email

const email = queryString.email;
if((queryString.eventID && !queryString.year) || (!queryString.eventID && queryString.year)) {

throw helpers.missingIdQueryResponse('eventID or year (must have both or neither)');
Expand Down
Loading