Skip to content

Commit

Permalink
✨ Added S3 image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush-Vish committed Oct 29, 2024
1 parent 34bcbfd commit 4ff588e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
14 changes: 7 additions & 7 deletions db/src/lib/models/org.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ const orgSchema = new Schema({
linkedin: { type: String },
},
businessHours: {
monday: { start: String, end: String },
tuesday: { start: String, end: String },
wednesday: { start: String, end: String },
thursday: { start: String, end: String },
friday: { start: String, end: String },
saturday: { start: String, end: String },
sunday: { start: String, end: String },
Monday: { start: String, end: String },
Tuesday: { start: String, end: String },
Wednesday: { start: String, end: String },
Thursday: { start: String, end: String },
Friday: { start: String, end: String },
Saturday: { start: String, end: String },
Sunday: { start: String, end: String },
},
isVerified: { type: Boolean, default: false },
rating: { type: Number, default: 0 },
Expand Down
12 changes: 7 additions & 5 deletions shopkeeper/src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ const registerOrg = async (
if (existingOrg) {
return next(new ApiError('Owner can only register one organization', 400));
}

console.log("1");
// Handle logo upload
let logoUrl = '';
if (req.files && req.files.logo && req.files.logo[0]) {
const logoUpload = await uploadFileToS3(req.files.logo[0]);
logoUrl = logoUpload.url;
}
console.log("2");

// Handle multiple images upload
let imageUrls: string[] = [];
Expand All @@ -90,7 +91,7 @@ const registerOrg = async (
const uploadResults = await Promise.all(uploadPromises);
imageUrls = uploadResults.map(result => result.url);
}

console.log("3");
const newOrg = new Org({
name,
address,
Expand All @@ -99,12 +100,13 @@ const registerOrg = async (
website,
logo: logoUrl,
images: imageUrls,
location,
socialMedia,
businessHours,
location : JSON.parse(location) ,
socialMedia : JSON.parse(socialMedia),
businessHours : JSON.parse(businessHours),
isVerified,
ownerId: req.user.id,
});
console.log("4");

await newOrg.save();

Expand Down
4 changes: 4 additions & 0 deletions shopkeeper/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ router.post(
'/register-org',
verifyJWT,
isAuthorized(['SERVICE_PROVIDER']),
upload.fields([
{ name: 'logo', maxCount: 1 },
{ name: 'images', maxCount: 5 },
]),
registerOrg
);

Expand Down
35 changes: 34 additions & 1 deletion utils/src/lib/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export const uploadFileToS3 = async (
if (!file || !file.buffer) {
throw new Error('Invalid file data');
}
console.time();
const buffer = await sharp(file.buffer)
.resize({ height: 1920, width: 1080, fit: 'contain' })
.resize({})
.toBuffer();
console.timeEnd();
const params = {
Bucket: bucketName,
Key: crypto.randomBytes(32).toString('hex') + file.originalname,
Expand All @@ -45,3 +47,34 @@ export const uploadFileToS3 = async (
throw new Error('File upload failed');
}
};
import multer from 'multer';
import path from 'path';

// Define the storage configuration
const storage = multer.memoryStorage(); // Use memory storage to get the file buffer

// Define the file filter function
function sanitizeFile(file, cb) {
const fileExts = ['.png', '.jpg', '.jpeg', '.gif'];
const isAllowedExt = fileExts.includes(path.extname(file.originalname.toLowerCase()));
const isAllowedMimeType = file.mimetype.startsWith('image/');

if (isAllowedExt && isAllowedMimeType) {
return cb(null, true);
} else {
cb('Error: File type not allowed!');
}
}

// Configure multer
const upload = multer({
storage: storage,
fileFilter: (req, file, callback) => {
sanitizeFile(file, callback);
},
limits: {
fileSize: 1024 * 1024 * 2, // 2MB file size
},
});

export { upload};
12 changes: 0 additions & 12 deletions utils/src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ export const isAuthorized = (allowedRoles: UserRole[]) => {
};
};

// Multer configuration for file upload
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 1024 * 1024 * 5, // Limit to 5MB
},
});

export {
upload
};


export const loactionMiddleware = async (req :RequestWithUser , res : Response , next : NextFunction) => {
try {
Expand Down

0 comments on commit 4ff588e

Please sign in to comment.