-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from andela/ft-user-profile-image-168405336
#168405336 Allow users to update their profile image
- Loading branch information
Showing
19 changed files
with
445 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Datauri from 'datauri'; | ||
import path from 'path'; | ||
import response from '../utils/response'; | ||
import messages from '../utils/messages'; | ||
import { uploader } from '../config/cloudinaryConfig'; | ||
|
||
const dUri = new Datauri(); | ||
let ownerId = ''; | ||
|
||
/** | ||
* extract the content of a raw file | ||
* @param {Object} rawFile - the raw file | ||
* @returns {Object} - return the content of the raw file | ||
*/ | ||
const extractSingleFile = (rawFile) => { | ||
const { originalname, buffer } = rawFile; | ||
return dUri.format(path.extname(originalname).toString(), buffer).content; | ||
}; | ||
|
||
/** | ||
* extract the content of the supplied raw files | ||
* @param {Object} rawFiles - the list of the raw files | ||
* @returns {Object} - return the list of the content of the raw files | ||
*/ | ||
const extractFiles = (rawFiles) => rawFiles.map( | ||
(rawFile) => extractSingleFile(rawFile) | ||
); | ||
|
||
/** | ||
* a function that is used to upload the supplied image to the cloud | ||
* @param {Object} file - a proccessed file | ||
* @param {Object} index - the index of the file | ||
* @returns {Object} - return the uploaded image url | ||
*/ | ||
const uploadSingleFile = async (file, index) => { | ||
const { secure_url: imageUrl } = await uploader.upload(file, { | ||
public_id: `${ownerId}-${index}`, | ||
overwrite: true, | ||
folder: 'rk-items', | ||
transformation: [{ | ||
width: 500, height: 250, crop: 'scale', quality: 'auto' | ||
}], | ||
allowedFormats: ['jpg', 'jpeg', 'png', 'gif', 'svg'], | ||
}); | ||
|
||
return imageUrl; | ||
}; | ||
|
||
/** | ||
* a middleware to upload image | ||
* @param {Object} req - request object | ||
* @param {Oject} res - response object | ||
* @param {Function} next - next function | ||
* @returns {Object} - Returns Object | ||
*/ | ||
const uploadImage = async (req, res, next) => { | ||
try { | ||
const { files: rawFiles, decoded } = req; | ||
if (!rawFiles) { | ||
return next(); | ||
} | ||
const { id } = decoded; | ||
ownerId = id; | ||
const files = extractFiles(rawFiles); | ||
const imageUrls = await Promise.all(files.map((file, index) => uploadSingleFile(file, index))); | ||
req.imageUrls = imageUrls; | ||
|
||
return next(); | ||
} catch (error) { | ||
return response(res, 400, 'error', { message: messages.imageError }); | ||
} | ||
}; | ||
|
||
export default uploadImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import multer from 'multer'; | ||
import storage from '../utils/cloudinaryStorage'; | ||
|
||
const storage = multer.memoryStorage(); | ||
const multerUploads = multer({ storage }).array('images', 10); | ||
|
||
export default multerUploads; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.