Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from travist/master

Need to update this example to work with the latest versions of everything.
  • Loading branch information...
commit 931cfd50679074de73117e782617c0a5081cc561 2 parents 2513d72 + 2139ede
@1602 1602 authored
View
15 app/controllers/files_controller.js
@@ -1,6 +1,6 @@
load('application');
-before(loadFile, {only: ['show', 'edit', 'update', 'destroy']});
+before(loadFile, {only: ['create', 'show', 'edit', 'update', 'destroy']});
action('new', function () {
this.file = new File;
@@ -10,7 +10,7 @@ action('new', function () {
action('create', function () {
this.file = new File();
- var tmpFile = req.form.files.file;
+ var tmpFile = req.files.file;
this.file.upload(tmpFile.name, tmpFile.path, function (err) {
if (err) {
console.log(err);
@@ -26,9 +26,14 @@ action('create', function () {
action('index', function () {
File.find(function (err, files) {
- this.files = files;
- this.title = 'Files index';
- render();
+ if (err) {
+ send(err);
+ }
+ else {
+ this.files = files;
+ this.title = 'Files index';
+ render();
+ }
}.bind(this));
});
View
24 app/models/file.js
@@ -4,13 +4,23 @@ function File(name) {
this.id = this.name = name;
}
+// The directory to upload files too.
+File.directory = app.root + '/data';
+
File.find = function (cb) {
var files = [];
- fs.readdir(app.root + '/data', function (err, fileNames) {
- fileNames.forEach(function (file) {
- files.push(new File(file));
- });
- cb(err, files);
+ fs.exists(File.directory, function(exists) {
+ if (exists) {
+ fs.readdir(File.directory, function (err, fileNames) {
+ fileNames.forEach(function (file) {
+ files.push(new File(file));
+ });
+ cb(err, files);
+ });
+ }
+ else {
+ cb('You must create the directory ' + File.directory + '.');
+ }
});
};
@@ -23,7 +33,7 @@ File.prototype.remove = function (cb) {
};
File.prototype.filename = function () {
- return app.root + '/data/' + this.name;
+ return File.directory + '/' + this.name;
};
File.prototype.upload = function (name, path, cb) {
@@ -37,4 +47,4 @@ File.prototype.rename = function (name, cb) {
fs.rename(oldPath, this.filename(), cb);
};
-export('File', File);
+module.exports = File
View
6 config/environment.js
@@ -1,16 +1,14 @@
var express = require('express');
-var form = require('connect-form-sync');
-
app.configure(function(){
var cwd = process.cwd();
app.use(express.static(cwd + '/public', {maxAge: 86400000}));
app.set('views', cwd + '/app/views');
app.set('view engine', 'ejs');
+ app.set('view options', {complexNames: true});
app.set('jsDirectory', '/javascripts/');
app.set('cssDirectory', '/stylesheets/');
- app.use(form({ keepExtensions: true })); // https://github.com/anatoliychakkaev/connect-form-sync
app.use(express.bodyParser());
- app.use(express.cookieParser());
+ app.use(express.cookieParser('secret'));
app.use(express.session({secret: 'secret'}));
app.use(express.methodOverride());
app.use(app.router);
Please sign in to comment.
Something went wrong with that request. Please try again.