Skip to content

Commit

Permalink
Merge pull request #12 from pesto-students/feature/user-gallery
Browse files Browse the repository at this point in the history
Done the api changes
  • Loading branch information
anilmsaini authored Sep 8, 2024
2 parents 477a1ef + 45ac600 commit 72c0b2c
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 216 deletions.
7 changes: 6 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const path = require('path');
app.use(bodyParser.json());
const config = require('./config/config');

app.use(bodyParser.urlencoded({ extended: false }));
// Increase limit for JSON payloads
app.use(bodyParser.json({ limit: '50mb' })); // Adjust the limit as needed

// Increase limit for URL-encoded payloads
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
//app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(helmet.frameguard());
app.use(helmet.hsts({maxAge: 5184000}));
Expand Down
159 changes: 130 additions & 29 deletions src/controllers/commonController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const AWS = require('aws-sdk');
const AWS = require("aws-sdk");
const { v4: uuidv4 } = require("uuid");
const config = require("../config/config.js");
const S3 = new AWS.S3({region: config.REGION});
const S3 = new AWS.S3({ region: config.REGION });
const ReligionModel = require("../models/religionModel");
const CasteModel = require("../models/casteModel");
const MotherTongueModel = require("../models/motherTongueModel");
Expand All @@ -10,7 +10,12 @@ const PlansModel = require("../models/plansModel");
const AdminModel = require("../models/adminDetailsModel");
const UserModel = require("../models/userModel.js");

const { buildQueryFilter, sendEmail, uploadFileInS3 } = require("../utils/helpers");
const {
buildQueryFilter,
sendEmail,
uploadFileInS3,
getFileFromS3,
} = require("../utils/helpers");
const { mongoose } = require("mongoose");
const fs = require("fs").promises;
const filePath = "../plans.json";
Expand Down Expand Up @@ -285,49 +290,145 @@ module.exports = {
});
}
},
s3FileUpload: async (req, res, next) => {
try {
const keys = Object.keys(req.files);
let s3Key = keys.length ? keys[0] : "";
console.log("s3Key____________", s3Key);
if (!s3Key || !config.S3_UPLOAD_FOR.includes(s3Key)) {
return res.status(401).json({
status: "error",
message: "Please provide correct file option.",
});
}
const files = req.files[s3Key];
const splitFileName = files.name.split(".");
const fileExt = splitFileName[splitFileName.length - 1];
let newFileName = uuidv4();
newFileName = `${newFileName}.${fileExt}`;
const S3Key = `${config[s3Key]}/${req.user.id}/${newFileName}`;
const fileBody = Buffer.from(files.data, "base64"); // Assuming base64 encoded file

let s3Object = {
Bucket: config.S3_BUCKET,
Key: S3Key,
ContentType: files.mimetype,
ContentEncoding: "base64",
Body: fileBody,
};
await uploadFileInS3(s3Object);

let s3GetObject = {
Bucket: config.S3_BUCKET,
Key: S3Key,
Expires: config.SIGNED_URL_EXPIRE_SECONDS,
};
let fileAccessUrl = await getFileFromS3(s3GetObject);
let listOfFiles = {
fileUrl: fileAccessUrl,
file_name: newFileName,
};

res.status(201).json({
status: "success",
message: "File(s) uploaded successfully.",
data: listOfFiles,
});
} catch (error) {
console.log("error:: ", JSON.stringify(error));
res.status(401).json({
status: "error",
message:
error?.message || "There is some problem, please try again later.",
});
}
},
uploadFiles: async (req, res, next) => {
try {
console.log(req.files)
console.log(req.files);
if (!req?.files) {
res.status(401).json({
status: "error",
message: "No file selected"
message: "No file selected",
});
}
if(req.files?.pan){

}
if(req.files?.aadhar){

}
if(req.files?.profile){

}
if(req.files?.astro){

}
if(req.files?.gallery){

const keys = Object.keys(req.files);
let s3Key = keys.length ? keys[0] : "";
console.log("s3Key____________", s3Key);
if (!s3Key || !config.S3_UPLOAD_FOR.includes(s3Key)) {
return res.status(401).json({
status: "error",
message: "Please provide correct file option.",
});
}
const files = req.files[s3Key];
let listOfFiles;

if(req.files?.success_stories){
const files = req.files?.success_stories

if (files.length) {
listOfFiles = [];
for (const filePath of files) {
const splitFileName = filePath.name.split(".");
const fileExt = splitFileName[splitFileName.length - 1];
const S3Key = `success_stories/${req.user.id}/${filePath.name}`;
let newFileName = uuidv4();
newFileName = `${newFileName}.${fileExt}`;
const S3Key = `${config[s3Key]}/${req.user.id}/${newFileName}`;
const fileBody = Buffer.from(filePath.data, "base64");

let s3Object = {
Bucket: config.S3_BUCKET,
Key : S3Key,
Key: S3Key,
ContentType: filePath.mimetype,
Body : filePath.data,
ACL: "public-read"
}
console.log("s3Object_______", s3Object)
ContentEncoding: "base64",
Body: fileBody,
};
console.log("s3Object_______________________", s3Object);
await uploadFileInS3(s3Object);

let s3GetObject = {
Bucket: config.S3_BUCKET,
Key: S3Key,
Expires: config.SIGNED_URL_EXPIRE_SECONDS,
};
let fileAccessUrl = await getFileFromS3(s3GetObject);
listOfFiles.push({
fileUrl: fileAccessUrl,
file_name: newFileName,
});
}
}
} else {
const splitFileName = files.name.split(".");
const fileExt = splitFileName[splitFileName.length - 1];
let newFileName = uuidv4();
newFileName = `${newFileName}.${fileExt}`;
const S3Key = `${config[s3Key]}/${req.user.id}/${newFileName}`;
const fileBody = Buffer.from(files.data, "base64");

let s3Object = {
Bucket: config.S3_BUCKET,
Key: S3Key,
ContentType: files.mimetype,
ContentEncoding: "base64",
Body: fileBody,
};
console.log("s3Object_______________________", s3Object);
await uploadFileInS3(s3Object);

let s3GetObject = {
Bucket: config.S3_BUCKET,
Key: S3Key,
Expires: config.SIGNED_URL_EXPIRE_SECONDS,
};
let fileAccessUrl = await getFileFromS3(s3GetObject);
listOfFiles = {
fileUrl: fileAccessUrl,
file_name: newFileName,
};
}
return res.status(201).json({
status: "success",
message: "File(s) uploaded successfully.",
data: listOfFiles,
});
} catch (error) {
console.log("error:: ", JSON.stringify(error));
res.status(401).json({
Expand Down
Loading

0 comments on commit 72c0b2c

Please sign in to comment.