-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(add_image): enable image upload for article
- add middlewares to generate slug for articles - create functions to validate article details - update migrations file for articles - create middleware to add and upload images [Delivers #164139686]
- Loading branch information
Showing
11 changed files
with
205 additions
and
19 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,28 @@ | ||
import multer from 'multer'; | ||
import cloudinary from 'cloudinary'; | ||
import cloudinaryStorage from 'multer-storage-cloudinary'; | ||
|
||
cloudinary.config({ | ||
cloud_name: process.env.CLOUD_NAME, | ||
api_key: process.env.API_KEY, | ||
api_secret: process.env.API_SECRET | ||
}); | ||
|
||
const storage = cloudinaryStorage({ | ||
cloudinary, | ||
folder: 'demo', | ||
allowedFormats: ['jpg', 'jpeg', 'png', 'gif'], | ||
}); | ||
|
||
const imageParser = multer({ storage }).array('articleImages', 5); | ||
|
||
export default async (req, res, next) => { | ||
await imageParser(req, res, (error) => { | ||
if (error) return res.json({ success: false, error }); | ||
if (req.files) { | ||
req.images = req.files.map(image => image.url); | ||
return next(); | ||
} | ||
return next(); | ||
}); | ||
}; |
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,14 +1,15 @@ | ||
import { User } from '../models'; | ||
import { Article } from '../models'; | ||
import generateSlug from '../helpers/slug'; | ||
|
||
export default async (req, res, next) => { | ||
const { title } = req.body; | ||
const slug = generateSlug(req.body.title); | ||
try { | ||
const user = await User.findOne({ where: { slug } }); | ||
req.slug = user ? (generateSlug(title)) : slug; | ||
const articleExist = await Article.findOne({ where: { slug } }); | ||
req.body.slug = articleExist ? (generateSlug(title)) : slug; | ||
req.body.slug = slug; | ||
} catch (error) { | ||
return res.status(500).json({ success: false, error: [error.message] }); | ||
} | ||
return next; | ||
return next(); | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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