-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
dbef84c
commit 180689c
Showing
2 changed files
with
57 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,57 @@ | ||
// for creating forms | ||
const formidable = require("formidable"); | ||
// to create slugs | ||
const slugify = require("slugify"); | ||
// stripping the html from the body to create excerpt | ||
const stripHtml = require("string-strip-html"); | ||
// lodash for updating the blogs | ||
const _ = require("lodash"); | ||
// models | ||
const Blog = require("../models/blogSchema"); | ||
const Category = require("../models/categorySchema"); | ||
const Tag = require("../models/tagSchema"); | ||
// Handling errors (sending mongoose error to the client) | ||
const {errorHandler} = require("../helpers/databaseErrorHandler"); | ||
// node js file system | ||
const fs = require("fs"); | ||
|
||
|
||
exports.create = (req,res)=>{ | ||
res.json({time: Date().toString()}) | ||
} | ||
let form = new formidable.IncomingForm() | ||
form.keepExtensions = true | ||
form.parse(req,(err,fields,files)=>{ | ||
if(err){ | ||
return res.status(400).json({ | ||
error: "Image Upload failed" | ||
}) | ||
} | ||
const {title,body,categories,taglists} = fields | ||
let blog = new Blog() | ||
blog.title = fields.title | ||
blog.body = fields.body | ||
blog.slug = slugify(title).toLowerCase() | ||
blog.mtitle = `${title} | ${process.env.BLOG_NAME}` | ||
blog.mdesc = stripHtml(body.substring(0,150)) | ||
blog.postedBy = req.user._id | ||
|
||
// blog.categories = fields.categories | ||
// blog.taglists = fields.taglists | ||
if(files.photo){ | ||
if(files.photo.size > 10000000){ | ||
return res.status(400).json({ | ||
error: "Image size tooo large" | ||
}); | ||
} | ||
blog.photo.data = fs.readFileSync(files.photo.path) | ||
blog.photo.contentType = files.photo.type | ||
} | ||
blog.save((err,result)=>{ | ||
if (err){ | ||
return res.status(400).json({ | ||
error: errorHandler(err) | ||
}); | ||
} | ||
res.json(result); | ||
}) | ||
}) | ||
} |
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