-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
161 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { v2 as cloudinary } from 'cloudinary'; | ||
|
||
/** | ||
* @Author - Deschant Kounou | ||
*/ | ||
class uploadController { | ||
/** | ||
* Upload a single image | ||
* @param {Object} req - Request | ||
* @param {Object} res - Response | ||
* @returns {Object} uploaded image url | ||
*/ | ||
static async uploadImage(req, res) { | ||
const { file } = req; | ||
try { | ||
const image = await cloudinary.uploader.upload(file.path); | ||
return res.status(200).json({ status: 200, url: image.secure_url }); | ||
} catch (error) { | ||
return res.status(500).json({ error }); | ||
} | ||
} | ||
} | ||
|
||
export default uploadController; |
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,13 @@ | ||
import { Router } from 'express'; | ||
|
||
import UploadController from '../controllers/uploadController'; | ||
import Auth from '../../middleware/auth'; | ||
import upload from '../../handlers/multer'; | ||
|
||
const uploadRouter = Router(); | ||
const { uploadImage } = UploadController; | ||
const { verifyToken } = Auth; | ||
|
||
uploadRouter.post('/image', verifyToken, upload.single('image'), uploadImage); | ||
|
||
export default uploadRouter; |
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,46 @@ | ||
import chaiHttp from 'chai-http'; | ||
import chai from 'chai'; | ||
import dotenv from 'dotenv'; | ||
import fs from 'fs'; | ||
|
||
import app from '../src/index'; | ||
import tokenHelper from '../src/helpers/Token.helper'; | ||
import models from '../src/sequelize/models'; | ||
import mockUsers from './mock/users'; | ||
|
||
dotenv.config(); | ||
chai.use(chaiHttp); | ||
|
||
let testUser, userToken; | ||
|
||
describe('Image Upload', () => { | ||
before(async () => { | ||
testUser = await models.User.create(mockUsers.user3); | ||
userToken = await tokenHelper.generateToken({ id: testUser.dataValues.id }); | ||
}); | ||
|
||
it('should upload and image', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/upload/image') | ||
.set('token', userToken) | ||
.attach('image', fs.readFileSync(`${__dirname}/mock/sample.png`), 'sample.png') | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.be.a('object'); | ||
done(); | ||
}); | ||
}).timeout(10000); | ||
|
||
it('should not upload and image with invalid path', (done) => { | ||
chai | ||
.request(app) | ||
.post('/api/upload/image') | ||
.set('token', userToken) | ||
.attach('imsage', fs.readFileSync(`${__dirname}/mock/sample.png`), 'sample.png') | ||
.end((err, res) => { | ||
res.should.have.status(500); | ||
done(); | ||
}); | ||
}).timeout(10000); | ||
}); |
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