T-954157790482076: moved bucket names, and file types to appConfig

- code review comments
This commit is contained in:
Okechi Onyeje 2019-01-09 19:00:42 -05:00
parent e2fc21b5c6
commit b4c93b96ec
4 changed files with 75 additions and 68 deletions

View File

@ -50,6 +50,13 @@
}
}
},
"buckets": {
"profile": "tagfer-inc_profile-images"
},
"imageFormat": {
"image/jpeg": "jpg",
"image/png": "png"
},
"dbPath": {
"users": "users",
"profiles": "profiles"

View File

@ -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
};

View File

@ -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
}
updateUserProfile,
updateUserProfileImage,
getUserProfile
};

View File

@ -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;