T-954157790482076: Add image upload endpoint for a user's profile

This commit is contained in:
Okechi Onyeje 2018-12-29 23:15:25 -05:00
parent 0607f6d06e
commit c3fb3c8b52
3 changed files with 62 additions and 0 deletions

View File

@ -27,6 +27,7 @@ function router(app) {
// Profile Endpoints
app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile);
app.get('/profiles/:profileNumber', ProfileHandlers.getUserProfile);
app.put('/profiles/uploadImage/:profileNumber', ProfileHandlers.updateUserProfileImage);
}
module.exports = router;

View File

@ -1,4 +1,5 @@
const database = require('firebase-admin').database();
const storage = require('firebase-admin').storage();
/**
* Blind initializing function for a new user's default profile
@ -34,8 +35,43 @@ function getProfile(profileNumber, tagferId) {
});
}
/**
* Updates a user's profile image
* @param {object} profileObj JSON object containing all image data for a profile captured from the frontend
* @param {number} profileNumber Number used to identify which profile a user wants to update/add image to
* @param {string} tagferId tagferId obtained by extracting from authorization header
* @returns {object} Object containing a Promise that contains the result of uploading profile image, and the image url | {promise, imageURL}
*/
function updateProfileImage(profileImageObj, profileNumber, tagferId) {
//persist profile image data to firebase storage
var profileImageRef = storage.ref(`images/profiles/${tagferId}/profile${profileNumber}/${profileImageObj.name}`);
var uploadTask = profileImageRef.put(profileImageObj.file, profileImageObj.metaData);
return uploadTask.on('state_changed',(snapshot) => {
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case 'paused': // or 'paused'
console.log('Upload is paused');
break;
case 'running': // or 'running'
console.log('Upload is running');
break;
}
}, (error) => {throw error}
, () => {
return uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
//save image url to user's selected profile
return {promise: database.ref(`/profiles/${tagferId}/profile${profileNumber}/imageURL`).set(downloadURL), imageURL: downloadURL};
}).catch( (error) => {throw error});
})
}
module.exports = {
updateProfile,
createInitialProfiles,
updateProfileImage
getProfile
};

View File

@ -32,6 +32,30 @@ async function updateUserProfile(req, res) {
}
}
/**
* Endpoints: PUT profiles/uploadImage/:profileNumber
* Updates user's profile with a profile image based on profile number given.
* This endpoint is called seperately in order to add an image to a profile.
* @param {Object} req {profileNumber}
* @param {Object} res
*/
async function updateUserProfileImage() {
const profileImageObj = req.body;
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
const result = profileDao.updateProfileImage(profileImageObj, req.params.profileNumber, tagferId)
result.promise.then( () => {
res.status(http.OK).json({imageURL: result.imageURL});
}).catch( (error) => {
res.status(http.INTERNAL_SERVER_ERROR).json({error})
});
} catch (error) {
res.status(http.BAD_REQUEST).json({error})
}
}
async function getUserProfile(req, res) {
const profileNumber = req.params.profileNumber;
if (!utils.isProfileNumberValid(profileNumber)) {
@ -52,5 +76,6 @@ async function getUserProfile(req, res) {
module.exports = {
updateUserProfile,
updateUserProfileImage
getUserProfile
}