Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Files plugin

  • Loading branch information...
commit 060715e5f696c5cbf802f8a5b021e6a6952ba6be 1 parent 60d4adb
@vslinko vslinko authored
View
52 lib/rithis-stack/plugins/files.coffee
@@ -0,0 +1,52 @@
+module.exports = (stack, callback) ->
+ stack.app.get "/files/:filename", (req, res) ->
+ gs = new stack.mongodb.GridStore stack.connection.db, req.params.filename, "r"
+
+ gs.open (err) ->
+ if err and err.message is "#{req.params.filename} does not exist"
+ return res.send 404
+
+ if err
+ return res.send 500
+
+ compiledMetadata = []
+ for key, value of gs.metadata
+ compiledMetadata.push "#{key}=#{value}"
+
+ res.set "Content-Type", gs.contentType
+ res.set "X-Object-Metadata", compiledMetadata.join "; "
+
+ stream = gs.stream true
+ stream.pipe res
+
+
+ stack.app.post "/files", (req, res) ->
+ unless req.files.file and req.body.filename
+ return res.send 400
+
+ metadata = {}
+ for key, value of req.body
+ unless key is "filename" or key is "content-type"
+ metadata[key] = value
+
+ gs = new stack.mongodb.GridStore stack.connection.db, req.body.filename, "w",
+ content_type: req.body["content-type"] or "binary/octet-stream"
+ metadata: metadata
+
+ gs.open (err) ->
+ if err
+ return res.send 500
+
+ if gs.length > 0
+ return gs.close ->
+ res.send 409
+
+ gs.writeFile req.files.file.path, (err) ->
+ gs.close ->
+ if err
+ return res.send 500
+
+ res.send 201
+
+
+ callback()
View
1  lib/rithis-stack/plugins/index.coffee
@@ -1,4 +1,5 @@
module.exports.assets = require "./assets"
module.exports.bower = require "./bower"
module.exports.crud = require "./crud"
+module.exports.files = require "./files"
module.exports.mongoose = require "./mongoose"
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "rithis-stack",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "Our web stack: express, mongoose, rithis-crud",
"keywords": ["web", "stack", "express", "mongoose", "crud"],
"author": {
Please sign in to comment.
Something went wrong with that request. Please try again.