-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into AssetCleanUp
- Loading branch information
Showing
27 changed files
with
1,496 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const AWS = require('aws-sdk'); | ||
require('dotenv').config(); | ||
const multer = require('multer'); | ||
const { v4: uuidv4 } = require('uuid'); | ||
const User = require('../models/User'); | ||
// const upload = multer({ storage: multer.memoryStorage() }); | ||
|
||
|
||
exports.uploadProfileImage = async (req, res) => { | ||
// Set up the S3 client for iDrive E2 (compatible with S3) | ||
const s3 = new AWS.S3({ | ||
endpoint: new AWS.Endpoint('https://k2y7.c15.e2-3.dev'), | ||
accessKeyId: process.env.IDRIVE_E2_ACCESS_KEY, | ||
secretAccessKey: process.env.IDRIVE_E2_SECRET_KEY, | ||
region: 'London', | ||
s3ForcePathStyle: true, | ||
}); | ||
|
||
try { | ||
const { id } = req.params; | ||
|
||
// Check if a file is provided | ||
if (!req.file) { | ||
return res.status(400).json({ message: '[USERS-013] No file uploaded' }); | ||
} | ||
|
||
// Generate a unique filename | ||
const filename = `${uuidv4()}-${req.file.originalname}`; | ||
|
||
// Find the user by ID | ||
const user = await User.findByPk(id); | ||
console.log("User: ", user); | ||
if (!user) { | ||
return res.status(404).json({ message: '[USERS-007] User not found' }); | ||
} | ||
|
||
// Upload the image to S3 | ||
const uploadResult = await s3.upload({ | ||
Bucket: 'profiles', // E2 bucket name | ||
Key: `images/${filename}`, // Folder within the bucket | ||
Body: req.file.buffer, // The image buffer | ||
ContentType: req.file.mimetype, // Set the content type for the image | ||
ACL: 'public-read', // Make the file publicly accessible | ||
}).promise(); | ||
|
||
// Store only the image URL in the PostgreSQL database | ||
await user.update({ | ||
photo: uploadResult.Location, | ||
}); | ||
|
||
res.status(200).json({ message: 'Image updated successfully!' }); | ||
} catch (error) { | ||
console.error("Error:",error.message); | ||
res.status(500).json({ message: "Can't upload image", error: error.message }); | ||
} | ||
}; | ||
|
||
|
||
exports.getProfileImage = async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
// Find the user by ID | ||
const user = await User.findByPk(id); | ||
if (!user) { | ||
return res.status(404).json({ message: '[USERS-007] User not found' }); | ||
} | ||
|
||
// Check if the user has a profile image | ||
if (!user.photo) { | ||
return res.status(404).json({ message: '[USERS-014] Profile image not found' }); | ||
} | ||
|
||
// Respond with the image URL | ||
res.status(200).json({ | ||
message: 'Profile image retrieved successfully!', | ||
imageUrl: user.photo, // This is the S3 URL of the image | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "[USERS-015] Can't retrieve profile image", error: error.message }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.