Skip to content

Commit

Permalink
BE #6 Add more information to S3 object
Browse files Browse the repository at this point in the history
  • Loading branch information
Satyajit Dey committed May 18, 2020
1 parent 08a968a commit 9181126
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
43 changes: 30 additions & 13 deletions controllers/binaries.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs');
const AWS = require('aws-sdk');
const mime = require('mime-types');
const {v4: uuidv4} = require('uuid');

AWS.config.update({
accessKeyId: process.env.IAM_ACCESS_KEY,
Expand All @@ -10,44 +12,59 @@ AWS.config.update({
const s3 = new AWS.S3();

exports.uploadImage = (req, res) => {
uploadFile(req.file.path, req.file.filename, res);
let fileInfo = {
source: req.file.path,
key: `${uuidv4()}---${req.file.filename}`,
contentType: mime.lookup(req.file.path),
author: req.jwt.name,
authorEmail: req.jwt.email
};
uploadFile(fileInfo, res);
};

exports.retrieveImage = (req, res) => {
retrieveFile(req.params.filename, res);
let key = `${req.params.id}---${req.params.filename}`;
retrieveFile(key, res);
};

let uploadFile = (source, target, res) => {
fs.readFile(source, (err, fileData) => {
let uploadFile = (fileInfo, res) => {
console.log(fileInfo)

fs.readFile(fileInfo.source, (err, fileData) => {
if (err) {
return res.status(500).send({error: err});
}

const putParams = {
const params = {
Bucket: process.env.AWS_S3_BUCKET_BINARIES,
Key: target,
Body: fileData
Key: fileInfo.key,
Body: fileData,
ContentType: fileInfo.contentType,
Metadata: {
'Author': fileInfo.author,
'Email': fileInfo.authorEmail
}
};

s3.upload(putParams, (err, data) => {
s3.upload(params, (err, data) => {
if (err) {
return res.status(500).send({error: err});
}

fs.unlink(source, (err) => {
fs.unlink(fileInfo.source, (err) => {
if (err) throw err;
console.log(`${source} deleted.`)
console.log(`${fileInfo.source} deleted.`)
});

return res.send({success: true});
return res.send(data);
});
});
};

let retrieveFile = (fileName, res) => {
let retrieveFile = (key, res) => {
const getParams = {
Bucket: process.env.AWS_S3_BUCKET_BINARIES,
Key: fileName
Key: key
};

s3.getObject(getParams, function (err, data) {
Expand Down
2 changes: 2 additions & 0 deletions models/images.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const mongoose = require('../services/db.service').mongoose;

const imageSchema = new mongoose.Schema({
imageId: {type: String, required: true, trim: true},
fileName: {type: String, required: true, trim: true},
storage: {type: String, required: true, trim: true},
caption: {type: String},
createdAt: {type: Date, default: Date.now},
});
15 changes: 10 additions & 5 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"express": "4.17.1",
"express-validator": "6.4.0",
"jsonwebtoken": "8.5.1",
"mime-types": "2.1.27",
"mongoose": "5.9.10",
"morgan": "1.10.0",
"multer": "1.4.2",
"passport": "0.4.1",
"slugify": "^1.4.0"
"slugify": "1.4.0",
"uuid": "8.0.0"
},
"devDependencies": {
"eslint": "6.8.0",
Expand Down
2 changes: 1 addition & 1 deletion routes/binaries.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const storage = multer.diskStorage({

const upload = multer({storage: storage});

router.get('/images/retrieve/:filename',
router.get('/images/:id/:filename',
AuthValidationMiddleware.verifyJwtToken,
AuthPermissionMiddleware.minimumPermissionLevelRequired(process.env.AUTH_PERMISSION_VIEWER),
BinariesController.retrieveImage);
Expand Down

0 comments on commit 9181126

Please sign in to comment.