- Notifications
You must be signed in to change notification settings - Fork 0
uploade image to cloudinary using multer#6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| node_modules | ||
| .env |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,47 @@ | ||
| import express from 'express'; | ||
| import Reflection from './src/controllers/reflection'; | ||
| import Reflection from './src/usingDB/controllers/reflection'; | ||
| import { multerUploads, dataUri } from './src/usingDB/multer'; | ||
| import { | ||
| uploader, | ||
| cloudinaryConfig | ||
| } from './src/usingDB/config/cloudinaryConfig'; | ||
| const app = express(); | ||
| app.use(express.json()); | ||
| app.use('*', cloudinaryConfig); | ||
| const port = process.env.PORT || 3380; | ||
| app.get('/', (req, res) => { | ||
| res.status(200).send({ message: 'hello worldn MY jamiu talking' }); | ||
| }); | ||
| app.post('/upload', multerUploads, (req, res) => { | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (req.file) { | ||
| const file = dataUri(req).content; | ||
| return uploader | ||
| .upload(file) | ||
| .then(result => { | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const image = result.url; | ||
| return res.status(200).json({ | ||
| messge: 'Your image has been uploded successfully to cloudinary', | ||
| data: { | ||
| image | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }); | ||
| }) | ||
| .catch(err => | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| res.status(400).json({ | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| messge: 'someting went wrong while processing your request', | ||
| data: { | ||
| err | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }) | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ); | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| }); | ||
| app.post('/api/v1/reflections', Reflection.create); | ||
| app.get('/api/v1/reflections', Reflection.getAll); | ||
| app.get('/api/v1/reflections/:id', Reflection.getOne); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| const { Pool } = require('pg'); | ||
| const dotenv = require('dotenv'); | ||
| dotenv.config(); | ||
| const connectionString = | ||
| 'postgresql://postgres:postgres@localhost:5432/reflection_db'; | ||
| const pool = new Pool({ connectionString: connectionString }); | ||
| pool.on('connect', () => { | ||
| console.log('Connected to the database'); | ||
| }); | ||
| // Create table | ||
| const createTable = () => { | ||
| const queryText = `CREATE TABLE IF NOT EXISTS | ||
| reflections( | ||
| id UUID PRIMARY KEY, | ||
| success TEXT NOT NULL, | ||
| low_point TEXT NOT NULL, | ||
| take_away TEXT NOT NULL, | ||
| created_date TIMESTAMP, | ||
| modified_date TIMESTAMP | ||
| )`; | ||
| pool | ||
| .query(queryText) | ||
| .then(res => { | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| console.log(res); | ||
| console.log('table created'); | ||
| pool.end(); | ||
| }) | ||
| .catch(err => { | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| console.log(err); | ||
| pool.end(); | ||
| }); | ||
| }; | ||
| // Drop table | ||
| const dropTable = () => { | ||
| const queryText = 'DROP TABLE IF EXISTS reflections'; | ||
| pool | ||
| .query(queryText) | ||
| .then(res => { | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| console.log(res); | ||
| pool.end(); | ||
| }) | ||
| .catch(err => { | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| console.log(err); | ||
| pool.end(); | ||
| }); | ||
| }; | ||
| pool.on('remove', () => { | ||
| console.log('Client removed'); | ||
| process.exit(0); | ||
| }); | ||
| module.exports = { | ||
| createTable, | ||
| dropTable | ||
phantware marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }; | ||
| require('make-runnable'); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { config, uploader } from 'cloudinary'; | ||
| import dotenv from 'dotenv'; | ||
| dotenv.config(); | ||
| const cloudinaryConfig = (req, res, next) => { | ||
| config({ | ||
| // cloud_name: 'phantware-nigeria', | ||
| // api_key: '328459338566541', | ||
| // api_secret: '9r5CjO6did7WqUSeOk8cS-WdgoU' | ||
| cloud_name: process.env.CLOUDINARY_CLOUD_NAME, | ||
| api_key: process.env.CLOUDINARY_API_KEY, | ||
| api_secret: process.env.CLOUDINARY_API_SECRET | ||
| }); | ||
| next(); | ||
| // console.log(p); | ||
| }; | ||
| export { cloudinaryConfig, uploader }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import momemnt from 'moment'; | ||
| const uuid = require('uuid'); | ||
| import db from '../db/index'; | ||
| const Reflection = { | ||
| // Create a reflection | ||
| async create(req, res) { | ||
| const text = `INSERT INTO reflections(id, success, low_point, take_away, created_date, modified_date) VALUES($1,$2,$3,$4,$5,$6) returning *`; | ||
| const { low_point, take_away } = req.body; | ||
| const values = [ | ||
| uuidv4(), | ||
| success, | ||
| low_point, | ||
| take_away, | ||
| momemnt(new Date()), | ||
| momemnt(new Date()) | ||
| ]; | ||
| try { | ||
| const { rows } = await db.query(text, values); | ||
| return res.status(201).send(rows[0]); | ||
| } catch (error) { | ||
| return res.status(400).send(error); | ||
| } | ||
| }, | ||
| // Get all reflections | ||
| async getAll(req, res) { | ||
| const findAllAllQuery = 'SELECT * FROM reflections'; | ||
| try { | ||
| const { rows } = await db.query(findAllAllQuery); | ||
| return res.status(200).send({ rows, rowCount }); | ||
| } catch (error) { | ||
| return res.status(400).send(error); | ||
| } | ||
| }, | ||
| // Get a reflection | ||
| async getOne(req, res) { | ||
| const text = 'SELECT * FROM reflections WHERE id = $1'; | ||
| try { | ||
| const value = [req.params.id]; | ||
| const { rows } = await db.query(text, value); | ||
| if (!rows[0]) { | ||
| return res.status(404).send({ message: 'reflection not found' }); | ||
| } | ||
| return res.status(200).send(rows[0]); | ||
| } catch (error) { | ||
| return res.status(400).send(error); | ||
| } | ||
| }, | ||
| // Update a reflection | ||
| async update(req, res) { | ||
| const findOneQuery = 'SELECT * FROM reflections where id = $1'; | ||
| const updateOneQuery = | ||
| 'UPDATE reflections SET success = $1, low_point = $2, take_away = $3, modified_date = $4, created_date = $5 returning *'; | ||
| try { | ||
| const value = [req.body.id]; | ||
| const { rows } = await db.query(findOneQuery, value); | ||
| if (!rows[0]) { | ||
| return res.status(404).send({ message: 'reflection not found' }); | ||
| } | ||
| const values = [ | ||
| req.body.success || rows[0].success, | ||
| req.body.low_point || rows[0].low_point, | ||
| req.body.take_away || rows[0].take_away, | ||
| momemnt(new Date()), | ||
| req.params.id | ||
| ]; | ||
| const response = await db.query(updateOneQuery, values); | ||
| return res.status(200).send(response.rows[0]); | ||
| } catch (error) { | ||
| return res.status(400).send(error); | ||
| } | ||
| }, | ||
| // Delete a reflection | ||
| async delete(req, res) { | ||
| const deleteQuery = 'DELETE FROM reflections where id = $1 returning *'; | ||
| try { | ||
| const value = [req.params.id]; | ||
| const { rows } = await db.query(deleteQuery, value); | ||
| if (!rows[0]) { | ||
| return res.status(404).send({ message: 'reflection not found' }); | ||
| } | ||
| return res.status(200).send({ message: 'Deleted' }); | ||
| } catch (error) { | ||
| return res.status(400).send(error); | ||
| } | ||
| } | ||
| }; | ||
| export default Reflection; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { Pool } from 'pg'; | ||
| import dotenv from 'dotenv'; | ||
| dotenv.config(); | ||
| const pool = new Pool({ | ||
| connectionString: process.env.DATABASE_URL | ||
| }); | ||
| // const connectionString = | ||
| // 'postgresql://postgres:postgres@localhost:5432/reflection_db'; | ||
| // const pool = new Pool({ connectionString: connectionString }); | ||
| pool.on('connect', () => { | ||
| console.log('Connected to the database'); | ||
| }); | ||
| // Query Database | ||
| export default { | ||
| query(text, params) { | ||
| return new Promise((resolve, reject) => { | ||
| pool | ||
| .query(text, params) | ||
| .then(res => { | ||
| resolve(res); | ||
| }) | ||
| .catch(err => { | ||
| reject(err); | ||
| }); | ||
| }); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import multer from 'multer'; | ||
| import Datauri from 'datauri'; | ||
| import path from 'path'; | ||
| const storage = multer.memoryStorage(); | ||
| const multerUploads = multer({ storage }).single('image'); | ||
| const dUri = new Datauri(); | ||
| const dataUri = req => | ||
| dUri.format(path.extname(req.file.originalname).toString(), req.file.buffer); | ||
| export { multerUploads, dataUri }; |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.