Skip to content

Commit

Permalink
Merge pull request #2 from travist/master
Browse files Browse the repository at this point in the history
Need to update this example to work with the latest versions of everything.
  • Loading branch information
1602 committed Aug 27, 2012
2 parents 2513d72 + 2139ede commit 931cfd5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
15 changes: 10 additions & 5 deletions 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;
Expand All @@ -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);
Expand All @@ -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));
});

Expand Down
24 changes: 17 additions & 7 deletions app/models/file.js
Expand Up @@ -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 + '.');
}
});
};

Expand All @@ -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) {
Expand All @@ -37,4 +47,4 @@ File.prototype.rename = function (name, cb) {
fs.rename(oldPath, this.filename(), cb);
};

export('File', File);
module.exports = File
6 changes: 2 additions & 4 deletions 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);
Expand Down

0 comments on commit 931cfd5

Please sign in to comment.