Permalink
Browse files

Merge branch 'master' of github.com:koostudios/couchpress

* 'master' of github.com:koostudios/couchpress:
  Updated style.css to style new admin page
  Updated install script to add new vies
  Added support for file uploads
  • Loading branch information...
2 parents 714a72c + a25feee commit 3c78b5bf5449347af2eb7fb6ee64e6a45b16847c @alejandro alejandro committed May 13, 2012
Showing with 170 additions and 2 deletions.
  1. +61 −0 controllers/uploads.coffee
  2. +39 −1 main.coffee
  3. +4 −1 setup/install.js
  4. +2 −0 views/admin/layout.jade
  5. +46 −0 views/admin/public/css/style.css
  6. +18 −0 views/admin/uploads.jade
View
@@ -0,0 +1,61 @@
+cradle = require 'cradle'
+moment = require 'moment'
+fs = require 'fs'
+config = require('../config').config
+
+class Upload
+ constructor: (host, port) ->
+ @connect = new cradle.Connection config.db.host, config.db.port, {
+ cache: true
+ raw: false
+ }
+ @db = @connect.database 'couchpress'
+
+ findAll: (callback) ->
+ @db.view 'couchpress/uploads_all', {descending: true}, (err, res) ->
+ if (err)
+ callback err
+ else
+ docs = []
+ res.forEach (row) ->
+ row.created_at = moment(row.created_at).fromNow()
+ docs.push row
+ callback null, docs
+
+ findById: (slug, filename, callback) ->
+ thatdb = @db
+ thatdb.get slug, (err, doc) ->
+ stream = thatdb.getAttachment slug, filename
+ data = []
+ dataLen = 0
+ stream.on 'data', (chunk) ->
+ data.push chunk
+ dataLen += chunk.length
+ stream.on 'end', ->
+ buf = new Buffer(dataLen)
+ pos = 0
+ for chunk in data
+ chunk.copy buf, pos
+ pos += chunk.length
+ doc.data = buf;
+ callback(null, doc)
+
+ save: (article, reqfile, callback) ->
+ thatdb = @db
+ article.type = 'upload'
+ article.created_at = new Date()
+ article.size = reqfile.size
+ fs.readFile reqfile.path, (err, data) ->
+ article._attachments = {}
+ article._attachments[article.filename] = {
+ content_type: reqfile.type
+ data: data.toString('base64')
+ }
+ thatdb.save article, (err, res) ->
+ if (err)
+ callback(err)
+ else
+ callback(null, res)
+
+
+exports.uploads = new Upload()
View
@@ -9,6 +9,7 @@ config = require('./config').config
# Controllers
posts = require('./controllers/posts').posts
users = require('./controllers/users').users
+uploads = require('./controllers/uploads').uploads
pass.serializeUser (user, done) ->
done null, user._id
@@ -113,6 +114,43 @@ app.get '/admin', users.check, (req, res) ->
title: 'Posts'
articles: docs
+app.get '/upload/:slug/:filename', (req, res, next) ->
+ uploads.findById req.params.slug, req.params.filename, (err, file) ->
+ if err
+ next()
+ else
+ res.header "Content-type", file._attachments[file.filename].content_type
+ res.header "Content-Encoding", "identity"
+ res.header "Content-length", file.size
+ res.end file.data
+
+app.get '/admin/uploads', users.check, (req, res) ->
+ uploads.findAll (err, files) ->
+ res.render admin + 'uploads',
+ layout: admin + 'layout',
+ locals:
+ title: 'Uploads',
+ articles: files
+
+app.post '/admin/uploads', users.check, (req, res) ->
+ console.log(req.body)
+ console.log(req.files)
+
+ slug = sluggify req.param 'title'
+ filename = sluggify req.files.upload.name
+ uploads.save {
+ _id: slug
+ title: req.param 'title'
+ filename: filename
+ type: 'upload'
+ user: req.user.name
+
+ }, req.files.upload, (err, docs) ->
+ if (err)
+ console.warn("ERROR UPLOADING: ", err)
+ else
+ res.redirect('/admin/uploads')
+
app.get '/admin/settings', users.check, (req, res) ->
adminConfig = require('./config').admin
res.render admin + 'settings',
@@ -211,4 +249,4 @@ renderError = (res, err) ->
layout: admin + 'layout'
locals:
title: 'Error'
- error: err
+ error: err
View
@@ -205,7 +205,10 @@ function installViews(data,cb){
db.save('_design/couchpress', {
views: {
all: {
- map: 'function(doc) {\n emit(doc.created_at, doc);\n}\n'
+ map: 'function(doc) {\n if (doc.type !== \'upload\')\n emit(doc.created_at, doc);\n}\n'
+ },
+ uploads_all: {
+ map: 'function(doc) {\n if (doc.type === \'upload\')\n emit(doc.created_at, doc);\n}'
}
}
}, function(error,res){
View
@@ -15,6 +15,8 @@ html(lang="en")
li= 'Posts'
a(href= '/admin/settings')
li= 'Settings'
+ a(href='/admin/uploads')
+ li= 'Uploads'
nav.right
ul
a(href= '/logout')
@@ -103,6 +103,22 @@ nav.right {
font-size: 0.9em;
color: #AAA;
}
+
+ .article h2 {
+ margin:0;
+ font-size: 1.2em;
+ }
+
+ .article span.slug {
+ color: #777;
+ }
+
+ .article span.type {
+ float:right;
+ font-family:serif;
+ font-style:oblique;
+ color:#888;
+ }
.article .right {
@@ -435,6 +451,36 @@ form input {
font-size: 14px;
}
+
+ form#uploader {
+
+ }
+ form#uploader input[type='file'] {
+ padding:5px 20px;
+ background-color:#eee;
+ }
+ form#uploader #go {
+ /*float: right;*/
+ display: inline;
+ padding: 10px 15px;
+ width: 100px;
+ font-size: 0.8em;
+ background: #DDD;
+ border: 1px solid #AAA;
+ cursor: pointer;
+ box-shadow: inset 0 1px 1px #FFF;
+ }
+
+ form#uploader #go:hover {
+ background: #EEE;
+ }
+
+ form#uploader #go:active {
+ background: #555;
+ color: #FFF;
+ box-shadow: inset 0 1px 1px #222;
+ }
+
#dialog {
display: none;
opacity: 0;
View
@@ -0,0 +1,18 @@
+h2 New File
+form(method='post', action='/admin/uploads', id='uploader', enctype='multipart/form-data')
+ input(type='text', placeholder='File Title', name='title')
+ input(type='file', placeholder='Upload a file', name='upload')
+ input(type='submit', value='Upload', id='go')
+h2 Existing Files
+- if(typeof(articles) != 'undefined')
+ - each article in articles
+ - var slug = '/upload/' + article._id + '/' + article.filename
+ .article
+ h1= article.filename
+ h2= article.title
+ span.type= article._attachments[article.filename].content_type
+ span.slug
+ a(href= slug, target='_blank')= slug
+ span.created= article.created_at + ' by ' + article.user
+- else
+ .article='No uploads'

0 comments on commit 3c78b5b

Please sign in to comment.