Skip to content

Commit

Permalink
Merge branch 'development-v2' into prodution
Browse files Browse the repository at this point in the history
  • Loading branch information
nilupulmadhawa committed May 11, 2024
2 parents 392c7ed + 560a6a8 commit 44dd80f
Show file tree
Hide file tree
Showing 8 changed files with 4,114 additions and 9,172 deletions.
11,929 changes: 4,081 additions & 7,848 deletions item_service/package-lock.json

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions item_service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-s3": "^3.572.0",
"@aws-sdk/s3-request-presigner": "^3.572.0",
"aws-sdk": "^2.1603.0",
"bcrypt": "^5.1.0",
"celebrate": "^15.0.1",
"cors": "^2.8.5",
Expand All @@ -27,7 +24,6 @@
"node-fetch": "^2.6.8",
"nodemon": "^2.0.20",
"path": "^0.12.7",
"sharp": "^0.33.3",
"winston": "^3.8.2"
},
"devDependencies": {
Expand Down
13 changes: 0 additions & 13 deletions item_service/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ const { errors } = require('celebrate');

dotenv.config();

const bucketName = process.env.AWS_BUCKET_NAME
const region = process.env.AWS_BUCKET_REGION
const accessKeyId = process.env.AWS_ACCESS_KEY
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY

const s3Client = new S3Client({
region,
credentials: {
accessKeyId,
secretAccessKey
}
})

const app = express();

app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
Expand Down
20 changes: 10 additions & 10 deletions item_service/src/controllers/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ export const getActiveCategories = asyncHandler(async (req, res) => {
})


export const uploadFile = (req, res) => {
if (req.file) {
res.status(200).json({
message: "File uploaded successfully",
filename: req.file.filename
});
} else {
res.status(400).json({ message: "File upload failed" });
}
};
// export const uploadFile = (req, res) => {
// if (req.file) {
// res.status(200).json({
// message: "File uploaded successfully",
// filename: req.file.filename
// });
// } else {
// res.status(400).json({ message: "File upload failed" });
// }
// };

70 changes: 0 additions & 70 deletions item_service/src/controllers/item.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import asyncHandler from '../middleware/async'
import { makeResponse } from '../utils/response'
import Item from '../models/item.model.js';
import sharp from 'sharp';
import crypto from 'crypto'
import { GetObjectCommand } from "@aws-sdk/client-s3"
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"

export const create = asyncHandler(async (req, res) => {
try {
Expand Down Expand Up @@ -114,69 +110,3 @@ export const getSellerItems = asyncHandler(async (req, res) => {
});
}
});

const generateFileName = (bytes = 32) => crypto.randomBytes(bytes).toString('hex')

export const uploadFile = asyncHandler(async (req, res) => {
const file = req.file
const image = req.body.name

const fileBuffer = sharp(file.buffer)
.resize({ height: 1920, width: 1080, fit: "contain" })
.toBuffer()

// Configure the upload details to send to S3
const fileName = generateFileName()
const uploadParams = {
Bucket: bucketName,
Body: fileBuffer,
Key: fileName,
ContentType: file.mimetype
}

// Send the upload to S3
await s3Client.send(new PutObjectCommand(uploadParams));

// Save the image name to the database. Any other req.body data can be saved here too but we don't need any other image data.
const post = await prisma.posts.create({
data: {
image,
}
})

res.send(post)
});

export const getProductImage = asyncHandler(async (req, res) => {

const posts = await prisma.posts.findMany({ orderBy: [{ created: 'desc' }] }) // Get all posts from the database

for (let post of posts) { // For each post, generate a signed URL and save it to the post object
post.imageUrl = await getSignedUrl(
s3Client,
new GetObjectCommand({
Bucket: bucketName,
Key: imageName
}),
{ expiresIn: 60 }// 60 seconds
)
}

res.send(posts)
});

export const deleteProductImage = asyncHandler(async (req, res) => {

const id = +req.params.id
const post = await prisma.posts.findUnique({where: {id}})

const deleteParams = {
Bucket: bucketName,
Key: post.imageName,
}

return s3Client.send(new DeleteObjectCommand(deleteParams))

await prisma.posts.delete({where: {id}})
res.send(post)
});
24 changes: 12 additions & 12 deletions item_service/src/routes/category.routes.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import express from 'express';
import { uploadFile, create, getAll, getById, update, remove, getSellerCategories, getActiveCategories } from '../controllers/category';
import { create, getAll, getById, update, remove, getSellerCategories, getActiveCategories } from '../controllers/category';
import multer from 'multer';

const categoryRouter = express.Router();


const storage = multer.diskStorage({
// const storage = multer.diskStorage({

destination: (req, file, cb) => {
console.log(req);
cb(null, "icon");
},
filename: (req, file, cb) => {
cb(null, req.body.name);
},
});
// destination: (req, file, cb) => {
// console.log(req);
// cb(null, "icon");
// },
// filename: (req, file, cb) => {
// cb(null, req.body.name);
// },
// });


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


categoryRouter.post('/', create);
Expand All @@ -27,7 +27,7 @@ categoryRouter.patch('/:id', update);
categoryRouter.delete('/:id', remove);
categoryRouter.get('/seller/:id', getSellerCategories);
categoryRouter.get('/active/seller/:id', getActiveCategories);
categoryRouter.post('/upload/icons', upload.single('file'), uploadFile);
// categoryRouter.post('/upload/icons', upload.single('file'), uploadFile);


// categoryRouter.post('/', celebrate({ [Segments.BODY]: addLocationSchema }), create);
Expand Down
10 changes: 4 additions & 6 deletions item_service/src/routes/item.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { celebrate, Segments } from 'celebrate'

import { protect } from '../middleware/auth'

import multer from 'multer'
// import multer from 'multer'

const storage = multer.memoryStorage()
const upload = multer({ storage: storage })
// const storage = multer.memoryStorage()
// const upload = multer({ storage: storage })

const itemRouter = express.Router();

Expand All @@ -18,9 +18,7 @@ itemRouter.get('/:id', getById);
itemRouter.patch('/:id', update);
itemRouter.delete('/:id', remove);
itemRouter.get('/seller/:id', getSellerItems);
itemRouter.post('/upload/images', upload.single('file'),uploadFile);
itemRouter.get('/', getProductImage);
itemRouter.delete('/image/:id', deleteProductImage);


// itemRouter.post('/', celebrate({ [Segments.BODY]: addLocationSchema }), create);
// itemRouter.get('/', celebrate({ [Segments.QUERY]: itemViewSchema }), getAll);
Expand Down
Loading

0 comments on commit 44dd80f

Please sign in to comment.