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