From b4c93b96ecdde54ab6a4bbb131cf7414fa164c2e Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Wed, 9 Jan 2019 19:00:42 -0500 Subject: [PATCH] T-954157790482076: moved bucket names, and file types to appConfig - code review comments --- src/config/app.json | 7 +++ src/ups/profiles/dao.js | 29 +++++------ src/ups/profiles/handlers.js | 98 ++++++++++++++++++------------------ src/ups/utils/utils.js | 9 ++-- 4 files changed, 75 insertions(+), 68 deletions(-) diff --git a/src/config/app.json b/src/config/app.json index 15341e0..d2e6cd4 100644 --- a/src/config/app.json +++ b/src/config/app.json @@ -50,6 +50,13 @@ } } }, + "buckets": { + "profile": "tagfer-inc_profile-images" + }, + "imageFormat": { + "image/jpeg": "jpg", + "image/png": "png" + }, "dbPath": { "users": "users", "profiles": "profiles" diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 063b4d6..501bd88 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -1,5 +1,6 @@ const database = require('firebase-admin').database(); const utils = require('../utils/utils'); +const appConfig = require('../../config/app.json'); /** * Blind initializing function for a new user's default profile @@ -7,8 +8,8 @@ const utils = require('../utils/utils'); * @param {string} tagferId */ function createInitialProfiles(profileObj, tagferId) { - //persist profile data to firebase - return database.ref(`/profiles/${tagferId}/profile1`).set(profileObj); + //persist profile data to firebase + return database.ref(`/profiles/${tagferId}/profile1`).set(profileObj); } /** @@ -19,8 +20,8 @@ function createInitialProfiles(profileObj, tagferId) { * @returns {Boolean} Boolean result of whether the */ function updateProfile(profileObj, profileNumber, tagferId) { - //persist profile data to firebase - return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).set(profileObj); + //persist profile data to firebase + return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).set(profileObj); } /** @@ -30,9 +31,9 @@ function updateProfile(profileObj, profileNumber, tagferId) { * @returns {Object} Profile object containg information for a specific user's profile */ function getProfile(profileNumber, tagferId) { - return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) { - return (snapshot.exists() ? snapshot.val() : {}); - }); + return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function (snapshot) { + return (snapshot.exists() ? snapshot.val() : {}); + }); } /** @@ -44,14 +45,14 @@ function getProfile(profileNumber, tagferId) { */ async function updateProfileImage(profileImageData, profileNumber, tagferId, res) { - //persist profile image data to firebase storage - var downloadURL = await utils.uploadImage(profileImageData, `images/${tagferId}/profile${profileNumber}/${profileImageData.metaData.name}`, 'tagfer-inc_profile-images', res) - return {promise: database.ref(`/profiles/${tagferId}/profile${profileNumber}/imageURL`).set(downloadURL), imageURL: downloadURL}; + //persist profile image data to firebase storage + var downloadURL = await utils.uploadImage(profileImageData, `${tagferId}-profile${profileNumber}`, appConfig.buckets.profile, res); + return { promise: database.ref(`/profiles/${tagferId}/profile${profileNumber}/photoURL`).set(downloadURL), imageURL: downloadURL }; } module.exports = { - updateProfile, - createInitialProfiles, - updateProfileImage, - getProfile + updateProfile, + createInitialProfiles, + updateProfileImage, + getProfile }; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 04eb27e..1c4e300 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -1,7 +1,7 @@ const profileDao = require('./dao'); const authDao = require('../auth/dao'); const utils = require('../utils/utils'); -const errors = require('../../config/errors'); +const errors = require('../../config/errors'); const http = require('../../config/http'); const _ = require('lodash'); @@ -13,23 +13,23 @@ const _ = require('lodash'); * @param {Object} res {result: Boolean} | {error: String} */ async function updateUserProfile(req, res) { - const profileObj = req.body; - const profileNumber = req.params.profileNumber; + const profileObj = req.body; + const profileNumber = req.params.profileNumber; - if (!utils.isProfileNumberValid(profileNumber, res)) { - return; - } - const sessionId = utils.getSessionIdFromAuthHeader(req, res); - try { - const tagferId = authDao.getSession(sessionId).tagferId; - profileDao.updateProfile(profileObj, profileNumber, tagferId).then( () => { - res.status(http.CREATED).json({}) - }).catch( (error) => { - res.status(http.INTERNAL_SERVER_ERROR).json({error: errors.APP_FIREBASE_DATABASE_ERROR}) - }); - } catch (error) { - res.status(http.UNAUTHORIZED).json({error}) - } + if (!utils.isProfileNumberValid(profileNumber, res)) { + return; + } + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + try { + const tagferId = authDao.getSession(sessionId).tagferId; + profileDao.updateProfile(profileObj, profileNumber, tagferId).then(() => { + res.status(http.CREATED).json({}); + }).catch((error) => { + res.status(http.INTERNAL_SERVER_ERROR).json({ error: errors.APP_FIREBASE_DATABASE_ERROR }); + }); + } catch (error) { + res.status(http.UNAUTHORIZED).json({ error }); + } } /** @@ -40,42 +40,42 @@ async function updateUserProfile(req, res) { * @param {Object} res */ async function updateUserProfileImage(req, res) { - const profileImageObj = req.body; - const sessionId = utils.getSessionIdFromAuthHeader(req, res); + const profileImageObj = req.body; + const sessionId = utils.getSessionIdFromAuthHeader(req, res); - try { - const tagferId = authDao.getSession(sessionId).tagferId; - const result = await profileDao.updateProfileImage(profileImageObj, req.params.profileNumber, tagferId, res) - 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}) - } + try { + const tagferId = authDao.getSession(sessionId).tagferId; + const result = await profileDao.updateProfileImage(profileImageObj, req.params.profileNumber, tagferId, res) + 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)) { - return; - } - const sessionId = utils.getSessionIdFromAuthHeader(req, res); - try { - const tagferId = authDao.getSession(sessionId).tagferId; - profileDao.getProfile(profileNumber,tagferId).then((profile) => { - res.status(http.OK).json({profile}) - }).catch(error => { - res.status(http.INTERNAL_SERVER_ERROR).json({error: error.code}) - }) - } catch (error) { - res.status(http.UNAUTHORIZED).json({ error }) - } + const profileNumber = req.params.profileNumber; + if (!utils.isProfileNumberValid(profileNumber)) { + return; + } + const sessionId = utils.getSessionIdFromAuthHeader(req, res); + try { + const tagferId = authDao.getSession(sessionId).tagferId; + profileDao.getProfile(profileNumber, tagferId).then((profile) => { + res.status(http.OK).json({ profile }); + }).catch(error => { + res.status(http.INTERNAL_SERVER_ERROR).json({ error: error.code }); + }); + } catch (error) { + res.status(http.UNAUTHORIZED).json({ error }); + } } module.exports = { - updateUserProfile, - updateUserProfileImage, - getUserProfile -} \ No newline at end of file + updateUserProfile, + updateUserProfileImage, + getUserProfile +}; \ No newline at end of file diff --git a/src/ups/utils/utils.js b/src/ups/utils/utils.js index 631f935..9284d17 100644 --- a/src/ups/utils/utils.js +++ b/src/ups/utils/utils.js @@ -4,7 +4,7 @@ const crypto = require('crypto'); const appConfig = require('../../config/app.json'); const http = require('../../config/http'); const errors = require('../../config/errors'); -const UUID = require("uuid/v4"); +const UUID = require('uuid/v4'); const firebase = require('firebase-admin'); /** @@ -77,15 +77,15 @@ function createOAuthHeader(request, app) { * * @param {*} image as raw bytes * @param {*} path to saving image in firebase - * @param {*} bucket + * @param {*} bucketName bucketName * * @returns {*} imageURL */ async function uploadImage(imageData, path, bucketName, res) { try { - var bucket = await firebase.storage().bucket(`gs://${bucketName}`); + const bucket = await firebase.storage().bucket(`gs://${bucketName}`); - var file = bucket.file(path); + var file = bucket.file(`${path}.${appConfig.imageFormat[imageData.metaData.contentType]}`); const uuid = UUID(); var imageBuffer = new Buffer(imageData.base64Data, 'base64'); @@ -101,7 +101,6 @@ async function uploadImage(imageData, path, bucketName, res) { }); const fileMetaData = await file.getMetadata(); - //const signedURL = await file.getSignedUrl(); return fileMetaData[0].mediaLink;