From c3fb3c8b528a9230b37e729e82dda8ea99ab07bd Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Sat, 29 Dec 2018 23:15:25 -0500 Subject: [PATCH 1/7] T-954157790482076: Add image upload endpoint for a user's profile --- src/config/router.js | 1 + src/ups/profiles/dao.js | 36 ++++++++++++++++++++++++++++++++++++ src/ups/profiles/handlers.js | 25 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/config/router.js b/src/config/router.js index 2701a77..acd5407 100644 --- a/src/config/router.js +++ b/src/config/router.js @@ -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; \ No newline at end of file diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 4d3d1f3..6f40adc 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -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 }; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 6085056..145eafb 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -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 } \ No newline at end of file From 8e035a4b33b5f4ee8f8cfc5b5441d3eefb0ecb49 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Fri, 4 Jan 2019 19:14:49 -0500 Subject: [PATCH 2/7] T-954157790482076: Move image upload functionality to its own generic util function --- src/ups/profiles/dao.js | 32 ++++++---------------------- src/ups/profiles/handlers.js | 6 +++--- src/ups/utils/utils.js | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 6f40adc..063b4d6 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -1,5 +1,5 @@ const database = require('firebase-admin').database(); -const storage = require('firebase-admin').storage(); +const utils = require('../utils/utils'); /** * Blind initializing function for a new user's default profile @@ -37,41 +37,21 @@ 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 {object} profileImageData 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) { +async function updateProfileImage(profileImageData, profileNumber, tagferId, res) { //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}); - }) + 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}; } module.exports = { updateProfile, createInitialProfiles, - updateProfileImage + updateProfileImage, getProfile }; diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 145eafb..04eb27e 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -39,13 +39,13 @@ async function updateUserProfile(req, res) { * @param {Object} req {profileNumber} * @param {Object} res */ -async function updateUserProfileImage() { +async function updateUserProfileImage(req, res) { 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) + const result = await profileDao.updateProfileImage(profileImageObj, req.params.profileNumber, tagferId, res) result.promise.then( () => { res.status(http.OK).json({imageURL: result.imageURL}); }).catch( (error) => { @@ -76,6 +76,6 @@ async function getUserProfile(req, res) { module.exports = { updateUserProfile, - updateUserProfileImage + updateUserProfileImage, getUserProfile } \ No newline at end of file diff --git a/src/ups/utils/utils.js b/src/ups/utils/utils.js index 9c2a3f5..2d32f7d 100644 --- a/src/ups/utils/utils.js +++ b/src/ups/utils/utils.js @@ -4,6 +4,8 @@ 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 firebase = require('firebase-admin'); /** * Verifies if the request is valid by checking if the request has the right app secret. @@ -71,10 +73,49 @@ function createOAuthHeader(request, app) { return oauth.toHeader(oauth.authorize(request, app.token)); } +/** + * + * @param {*} image as raw bytes + * @param {*} path to saving image in firebase + * @param {*} bucket + * + * @returns {*} imageURL + */ +async function uploadImage(imageData, path, bucketName, res) { + try { + var bucket = await firebase.storage().bucket(`gs://${bucketName}`); + + var file = bucket.file(path); + const uuid = UUID() + var imageBuffer = new Buffer(imageData.base64Data, 'base64'); + + var result = await file.save(imageBuffer, { + metaData: { + contentType: imageData.metaData.contentType, + metadata: { + firebaseStorageDownloadTokens: uuid + } + }, + public: true, + validation: 'md5', + }); + + const fileMetaData = await file.getMetadata(); + //const signedURL = await file.getSignedUrl(); + + return fileMetaData[0].mediaLink; + + } catch (error) { + res.status(http.INTERNAL_SERVER_ERROR).json({error}); + return null; + } +} + module.exports = { isAppSecretValid, isBodyValid, getSessionIdFromAuthHeader, isProfileNumberValid, createOAuthHeader + uploadImage }; \ No newline at end of file From e2fc21b5c6f4068302769e0e990ea60271d939c2 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Wed, 9 Jan 2019 18:02:50 -0500 Subject: [PATCH 3/7] T-954157790482076: fix rebase errors --- src/ups/utils/utils.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ups/utils/utils.js b/src/ups/utils/utils.js index 2d32f7d..631f935 100644 --- a/src/ups/utils/utils.js +++ b/src/ups/utils/utils.js @@ -86,10 +86,10 @@ async function uploadImage(imageData, path, bucketName, res) { var bucket = await firebase.storage().bucket(`gs://${bucketName}`); var file = bucket.file(path); - const uuid = UUID() + const uuid = UUID(); var imageBuffer = new Buffer(imageData.base64Data, 'base64'); - var result = await file.save(imageBuffer, { + await file.save(imageBuffer, { metaData: { contentType: imageData.metaData.contentType, metadata: { @@ -116,6 +116,6 @@ module.exports = { isBodyValid, getSessionIdFromAuthHeader, isProfileNumberValid, - createOAuthHeader + createOAuthHeader, uploadImage }; \ No newline at end of file From b4c93b96ecdde54ab6a4bbb131cf7414fa164c2e Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Wed, 9 Jan 2019 19:00:42 -0500 Subject: [PATCH 4/7] 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; From b50fe3e9516e4bb5f7cda63e013c855b6671e50c Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Sat, 12 Jan 2019 18:37:08 -0500 Subject: [PATCH 5/7] T-954157790482076: Code review comments - removed response variable from utility and dao function signatures --- src/ups/profiles/dao.js | 10 +++++++--- src/ups/profiles/handlers.js | 3 +-- src/ups/utils/utils.js | 19 ++++--------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 501bd88..877891d 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -44,10 +44,14 @@ function getProfile(profileNumber, tagferId) { * @returns {object} Object containing a Promise that contains the result of uploading profile image, and the image url | {promise, imageURL} */ -async function updateProfileImage(profileImageData, profileNumber, tagferId, res) { +async function updateProfileImage(profileImageData, profileNumber, tagferId) { //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 }; + try { + var downloadURL = await utils.uploadImage(profileImageData, `${tagferId}-profile${profileNumber}`, appConfig.buckets.profile); + return { promise: database.ref(`/profiles/${tagferId}/profile${profileNumber}/photoURL`).set(downloadURL), imageURL: downloadURL }; + } catch (error) { + throw error; + } } module.exports = { diff --git a/src/ups/profiles/handlers.js b/src/ups/profiles/handlers.js index 1c4e300..2e64c3f 100644 --- a/src/ups/profiles/handlers.js +++ b/src/ups/profiles/handlers.js @@ -3,7 +3,6 @@ const authDao = require('../auth/dao'); const utils = require('../utils/utils'); const errors = require('../../config/errors'); const http = require('../../config/http'); -const _ = require('lodash'); // Handlers /** @@ -45,7 +44,7 @@ async function updateUserProfileImage(req, res) { try { const tagferId = authDao.getSession(sessionId).tagferId; - const result = await profileDao.updateProfileImage(profileImageObj, req.params.profileNumber, tagferId, res) + const result = await profileDao.updateProfileImage(profileImageObj, req.params.profileNumber, tagferId); result.promise.then(() => { res.status(http.OK).json({ imageURL: result.imageURL }); }).catch((error) => { diff --git a/src/ups/utils/utils.js b/src/ups/utils/utils.js index 9284d17..ac6c1d0 100644 --- a/src/ups/utils/utils.js +++ b/src/ups/utils/utils.js @@ -1,10 +1,8 @@ const OAuth = require('oauth-1.0a'); 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 firebase = require('firebase-admin'); /** @@ -81,22 +79,14 @@ function createOAuthHeader(request, app) { * * @returns {*} imageURL */ -async function uploadImage(imageData, path, bucketName, res) { +async function uploadImage(imageData, path, bucketName) { try { - const bucket = await firebase.storage().bucket(`gs://${bucketName}`); + const bucket = firebase.storage().bucket(`gs://${bucketName}`); var file = bucket.file(`${path}.${appConfig.imageFormat[imageData.metaData.contentType]}`); - const uuid = UUID(); - var imageBuffer = new Buffer(imageData.base64Data, 'base64'); + var imageBuffer = Buffer.from(imageData.base64Data, 'base64'); await file.save(imageBuffer, { - metaData: { - contentType: imageData.metaData.contentType, - metadata: { - firebaseStorageDownloadTokens: uuid - } - }, - public: true, validation: 'md5', }); @@ -105,8 +95,7 @@ async function uploadImage(imageData, path, bucketName, res) { return fileMetaData[0].mediaLink; } catch (error) { - res.status(http.INTERNAL_SERVER_ERROR).json({error}); - return null; + throw error; } } From e87a5afba91518890cdfbb5152412b8a29aa319f Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Sat, 12 Jan 2019 19:07:42 -0500 Subject: [PATCH 6/7] T-954157790482076: Replace vars with const --- src/ups/profiles/dao.js | 2 +- src/ups/utils/utils.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ups/profiles/dao.js b/src/ups/profiles/dao.js index 877891d..77a78b5 100644 --- a/src/ups/profiles/dao.js +++ b/src/ups/profiles/dao.js @@ -47,7 +47,7 @@ function getProfile(profileNumber, tagferId) { async function updateProfileImage(profileImageData, profileNumber, tagferId) { //persist profile image data to firebase storage try { - var downloadURL = await utils.uploadImage(profileImageData, `${tagferId}-profile${profileNumber}`, appConfig.buckets.profile); + const downloadURL = await utils.uploadImage(profileImageData, `${tagferId}-profile${profileNumber}`, appConfig.buckets.profile); return { promise: database.ref(`/profiles/${tagferId}/profile${profileNumber}/photoURL`).set(downloadURL), imageURL: downloadURL }; } catch (error) { throw error; diff --git a/src/ups/utils/utils.js b/src/ups/utils/utils.js index ac6c1d0..ef2e512 100644 --- a/src/ups/utils/utils.js +++ b/src/ups/utils/utils.js @@ -83,8 +83,8 @@ async function uploadImage(imageData, path, bucketName) { try { const bucket = firebase.storage().bucket(`gs://${bucketName}`); - var file = bucket.file(`${path}.${appConfig.imageFormat[imageData.metaData.contentType]}`); - var imageBuffer = Buffer.from(imageData.base64Data, 'base64'); + const file = bucket.file(`${path}.${appConfig.imageFormat[imageData.metaData.contentType]}`); + const imageBuffer = Buffer.from(imageData.base64Data, 'base64'); await file.save(imageBuffer, { validation: 'md5', From 1abda1f8fbcc15570209ae9b8e6f080eb008ccfa Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Sat, 12 Jan 2019 19:09:52 -0500 Subject: [PATCH 7/7] T-954157790482076: Remove checksum validation --- src/ups/utils/utils.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ups/utils/utils.js b/src/ups/utils/utils.js index ef2e512..eae19d1 100644 --- a/src/ups/utils/utils.js +++ b/src/ups/utils/utils.js @@ -86,9 +86,7 @@ async function uploadImage(imageData, path, bucketName) { const file = bucket.file(`${path}.${appConfig.imageFormat[imageData.metaData.contentType]}`); const imageBuffer = Buffer.from(imageData.base64Data, 'base64'); - await file.save(imageBuffer, { - validation: 'md5', - }); + await file.save(imageBuffer); const fileMetaData = await file.getMetadata();