adding pretteir and fixing post call so now clicking continue will upload to aws
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
|
||||
import { Buffer } from 'node:buffer'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
class AWSUtil {
|
||||
constructor() {
|
||||
this.s3 = new S3Client({ region: "us-east-2" });
|
||||
this.s3 = new S3Client({ region: 'us-east-2' })
|
||||
}
|
||||
|
||||
async uploadFile(base64, ACL = "public-read") {
|
||||
const base64Data = Buffer.from(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64');
|
||||
const type = base64.split(';')[0].split('/')[1];
|
||||
const uuid = uuidv4();
|
||||
async uploadFile(base64, ACL = 'public-read') {
|
||||
const base64Data = Buffer.from(base64.replace(/^data:image\/\w+;base64,/, ''), 'base64')
|
||||
const type = base64.split(';')[0].split('/')[1]
|
||||
const uuid = uuidv4()
|
||||
|
||||
const params = {
|
||||
Bucket: process.env.AWS_S3_BUCKET,
|
||||
@@ -18,21 +18,16 @@ class AWSUtil {
|
||||
Body: base64Data,
|
||||
ContentEncoding: 'base64', // required
|
||||
ContentType: `image/${type}` // required
|
||||
};
|
||||
}
|
||||
|
||||
// Create an object and upload it to the Amazon S3 bucket.
|
||||
try {
|
||||
const results = await this.s3.send(new PutObjectCommand(params));
|
||||
const results = await this.s3.send(new PutObjectCommand(params))
|
||||
console.log(
|
||||
"Successfully created " +
|
||||
params.Key +
|
||||
" and uploaded it to " +
|
||||
params.Bucket +
|
||||
"/" +
|
||||
params.Key
|
||||
);
|
||||
'Successfully created ' + params.Key + ' and uploaded it to ' + params.Bucket + '/' + params.Key
|
||||
)
|
||||
} catch (err) {
|
||||
console.log("Error", err);
|
||||
console.log('Error', err)
|
||||
}
|
||||
|
||||
return `https://tattletires.s3.us-east-2.amazonaws.com/${params.Key}`
|
||||
@@ -41,13 +36,13 @@ class AWSUtil {
|
||||
async deleteFile(Location) {
|
||||
const params = {
|
||||
Bucket: process.env.AWS_S3_BUCKET,
|
||||
Key: Location.split("s3.amazonaws.com/").pop()
|
||||
};
|
||||
Key: Location.split('s3.amazonaws.com/').pop()
|
||||
}
|
||||
|
||||
const data = await this.s3.deleteObject(params).promise();
|
||||
const data = await this.s3.deleteObject(params).promise()
|
||||
|
||||
return data;
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
export default AWSUtil;
|
||||
export default AWSUtil
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import AWSUtil from '../bin/aws.js';
|
||||
import Post from '../models/postModel.js';
|
||||
import AWSUtil from '../bin/aws.js'
|
||||
import Post from '../models/postModel.js'
|
||||
|
||||
export const getAllPosts = async (req, res, next) => {
|
||||
const allPosts = await Post.find({}).exec();
|
||||
const allPosts = await Post.find({}).exec()
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: allPosts
|
||||
@@ -10,61 +10,60 @@ export const getAllPosts = async (req, res, next) => {
|
||||
}
|
||||
|
||||
export const getPost = async (req, res, next) => {
|
||||
const post = await Post.findById(req.params.id).exec();
|
||||
const post = await Post.findById(req.params.id).exec()
|
||||
if (!post) {
|
||||
return next('No document found with that id', 404);
|
||||
return next('No document found with that id', 404)
|
||||
}
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: post
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export const getAllPostsByUser = async (req, res, next) => {
|
||||
const post = await Post.find({email: req.params.user}).exec();
|
||||
const post = await Post.find({ email: req.params.user }).exec()
|
||||
if (!post) {
|
||||
return next('No document found with that id', 404);
|
||||
return next('No document found with that id', 404)
|
||||
}
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: post
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export const createPost = async (req, res, next) => {
|
||||
const aws = new AWSUtil();
|
||||
const aws = new AWSUtil()
|
||||
|
||||
// Grab base64 photo from the req body
|
||||
const location = await aws.uploadFile(req.body.photo);
|
||||
const location = await aws.uploadFile(req.body.photo)
|
||||
|
||||
const payload = {
|
||||
...req.body,
|
||||
photo: location
|
||||
}
|
||||
|
||||
|
||||
Post.create(payload).then(result => {
|
||||
Post.create(payload).then((result) => {
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: result
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const updatePost = async (req, res, next) => {
|
||||
Post.updateOne({_id: req.params.id},{$set: req.body}).then(result => {
|
||||
Post.updateOne({ _id: req.params.id }, { $set: req.body }).then((result) => {
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: result
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const deletePost = async (req, res, next) => {
|
||||
Post.deleteOne({_id: req.params.id}).then(result => {
|
||||
Post.deleteOne({ _id: req.params.id }).then((result) => {
|
||||
res.status(200).json({
|
||||
status: 'success',
|
||||
data: result
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
5014
api/package-lock.json
generated
5014
api/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,24 +1,24 @@
|
||||
{
|
||||
"name": "api",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"aws-sdk": "^2.1692.0",
|
||||
"cookie-parser": "~1.4.4",
|
||||
"cors": "^2.8.5",
|
||||
"debug": "~2.6.9",
|
||||
"dotenv": "^17.2.1",
|
||||
"express": "~4.16.1",
|
||||
"http-errors": "~1.6.3",
|
||||
"jade": "~1.11.0",
|
||||
"mongodb": "^6.18.0",
|
||||
"mongoose": "^8.16.4",
|
||||
"morgan": "~1.9.1",
|
||||
"nanoid": "^5.1.5",
|
||||
"validator": "^13.15.15",
|
||||
"xss-clean": "^0.1.4"
|
||||
}
|
||||
"name": "api",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.850.0",
|
||||
"cookie-parser": "~1.4.4",
|
||||
"cors": "^2.8.5",
|
||||
"debug": "~2.6.9",
|
||||
"dotenv": "^17.2.1",
|
||||
"express": "~4.16.1",
|
||||
"http-errors": "~1.6.3",
|
||||
"jade": "~1.11.0",
|
||||
"mongodb": "^6.18.0",
|
||||
"mongoose": "^8.16.4",
|
||||
"morgan": "~1.9.1",
|
||||
"nanoid": "^5.1.5",
|
||||
"validator": "^13.15.15",
|
||||
"xss-clean": "^0.1.4"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user