Permalink
Browse files

add image upload. fixes #22

  • Loading branch information...
madhums committed Dec 10, 2012
1 parent fc76f82 commit 0789e38738efab9b359899e0dacfd3eb934e77e2
Showing with 117 additions and 46 deletions.
  1. +1 −0 .gitignore
  2. +21 −11 app/controllers/articles.js
  3. +4 −0 app/models/article.js
  4. +33 −23 app/views/articles/form.jade
  5. +17 −12 app/views/articles/show.jade
  6. +40 −0 config/imager.exmple.js
  7. +1 −0 package.json
View
@@ -1,3 +1,4 @@
node_modules/
.monitor
config/config.js
+config/imager.js
@@ -1,6 +1,7 @@
var mongoose = require('mongoose')
, Article = mongoose.model('Article')
+ , Imager = require('imager')
, _ = require('underscore')
// New article
@@ -15,20 +16,29 @@ exports.new = function(req, res){
// Create an article
exports.create = function (req, res) {
var article = new Article(req.body)
+ , imagerConfig = require('../../config/imager')
+ , imager = new Imager(imagerConfig, 'Rackspace')
+
article.user = req.user
- article.save(function(err){
- if (err) {
- res.render('articles/new', {
- title: 'New Article'
- , article: article
- , errors: err.errors
- })
+ imager.upload(req.files.image, function (err, cdnUri, files) {
+ if (err) return res.render('400')
+ if (files.length) {
+ article.image = { cdnUri : cdnUri, files : files }
}
- else {
- res.redirect('/articles/'+article._id)
- }
- })
+ article.save(function(err){
+ if (err) {
+ res.render('articles/new', {
+ title: 'New Article'
+ , article: article
+ , errors: err.errors
+ })
+ }
+ else {
+ res.redirect('/articles/'+article._id)
+ }
+ })
+ }, 'article')
}
View
@@ -17,6 +17,10 @@ var ArticleSchema = new Schema({
, user: {type : Schema.ObjectId, ref : 'User'}
, comments: [{type : Schema.ObjectId, ref : 'Comment'}]
, tags: {type: [], get: getTags, set: setTags}
+ , image: {
+ cdnUri: String
+ , files: []

This comment has been minimized.

Show comment Hide comment
@2color

2color Jan 12, 2013

@madhums Why is files an array here?

@2color

2color Jan 12, 2013

@madhums Why is files an array here?

This comment has been minimized.

Show comment Hide comment
@madhums

madhums Jan 12, 2013

Owner

Coz you can upload multiple files for an article. Currently there is only one file being uploaded in the demo, but if you want more, you can push it to files array.

image: { 
  cdnUri: "http://fudge.s3.amazonaws.com", 
  files: [ "1357871973107.png", "1357871973190.png" ]
}
@madhums

madhums Jan 12, 2013

Owner

Coz you can upload multiple files for an article. Currently there is only one file being uploaded in the demo, but if you want more, you can push it to files array.

image: { 
  cdnUri: "http://fudge.s3.amazonaws.com", 
  files: [ "1357871973107.png", "1357871973190.png" ]
}
+ }
, categories: []
, createdAt : {type : Date, default : Date.now}
})
@@ -12,26 +12,36 @@ block content
each error in errors
li= error.type
- form.form-horizontal(method="post", action=action)
- if (!article.isNew)
- input(type="hidden", name="_method", value="PUT")
-
- .control-group
- label.control-label(for='title') Title
- .controls
- input#title.input-xlarge(type='text', name="title", value=article.title, placeholder='Enter the title')
-
- .control-group
- label.control-label(for='desc') Body
- .controls
- textarea#desc.input-xxlarge(type='text', rows="5", name="body", placeholder='Enter the article description')=article.body
-
- .control-group
- label.control-label(for='desc') Tags
- .controls
- input#tags(type='text', name="tags", value=article.tags, placeholder='Enter the tags')
-
- .form-actions
- button.btn.btn-primary(type='submit') Save changes
-  
- a.btn(href='/articles', title="cancel") Cancel
+ .row
+ .span7
+ form.form-horizontal(method="post", action=action, enctype="multipart/form-data")
+ if (!article.isNew)
+ input(type="hidden", name="_method", value="PUT")
+
+ .control-group
+ label.control-label(for='title') Title
+ .controls
+ input#title.input-xlarge(type='text', name="title", value=article.title, placeholder='Enter the title')
+
+ .control-group
+ label.control-label(for='title') Image
+ .controls
+ input(type='file', name="image[]")
+
+ .control-group
+ label.control-label(for='desc') Body
+ .controls
+ textarea#desc.input-xlarge(type='text', rows="5", name="body", placeholder='Enter the article description')=article.body
+
+ .control-group
+ label.control-label(for='desc') Tags
+ .controls
+ input#tags(type='text', name="tags", value=article.tags, placeholder='Enter the tags')
+
+ .form-actions
+ button.btn.btn-primary(type='submit') Save changes
+  
+ a.btn(href='/articles', title="cancel") Cancel
+ .span5
+ if (!article.isNew && article.image)
+ img(src=article.image.cdnUri + '/mini_' + article.image.files[0])
@@ -4,18 +4,23 @@ block main
h1= article.title
block content
- p=article.body
- p.author
- span Author : 
- a(href="/users/"+article.user._id)=article.user.name
- .date= formatDate(article.createdAt, "%b %d, %Y at %I:%M %p")
- if (article.tags)
- .tags
- | Tags : 
- each tag in article.tags.split(',')
- a.tag(href="/tags/"+tag)
- i.icon-tags
- | #{tag}
+ .row
+ .span9
+ p=article.body
+ p.author
+ span Author : 
+ a(href="/users/"+article.user._id)=article.user.name
+ .date= formatDate(article.createdAt, "%b %d, %Y at %I:%M %p")
+ if (article.tags)
+ .tags
+ | Tags : 
+ each tag in article.tags.split(',')
+ a.tag(href="/tags/"+tag)
+ i.icon-tags
+ | #{tag}
+ .span3
+ if (!article.isNew && article.image)
+ img(src=article.image.cdnUri + '/mini_' + article.image.files[0])
p
br
View
@@ -0,0 +1,40 @@
+module.exports = {
+ variants: {
+ article: {
+ resize: {
+ detail: "x440"
+ },
+ crop: {
+ thumb: "160000@"
+ },
+ resizeAndCrop: {
+ mini: {resize: "63504@", crop: "252x210"}
+ }
+ },
+
+ gallery: {
+ crop: {
+ thumb: "100x100"
+ }
+ }
+ },
+
+ storage: {
+ Rackspace: {
+ auth: {
+ username: "USERNAME",
+ apiKey: "API_KEY",
+ host: "lon.auth.api.rackspacecloud.com"
+ },
+ container: "CONTAINER_NAME"
+ },
+ S3: {
+ key: 'API_KEY',
+ secret: 'SECRET',
+ bucket: 'BUCKET_NAME',
+ region: 'REGION'
+ }
+ },
+
+ debug: true
+}
View
@@ -23,6 +23,7 @@
, "passport-twitter": "latest"
, "passport-github": "latest"
, "passport-google-oauth": "latest"
+ , "imager": "latest"
, "underscore": "latest"
, "gzippo": "latest"
, "async": "latest"

0 comments on commit 0789e38

Please sign in to comment.