Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Need to update this example to work with the latest versions of everything. #2

Merged
merged 2 commits into from

2 participants

@travist

I was having some issues getting this example to work. I had to make the following changes to get it working with the latest versions of everything. I also added an error if the directory doesn't exist.

Thanks for adding this example. It was very helpful.

Travis.

@anatoliychakkaev

Thank you Travis!

@anatoliychakkaev anatoliychakkaev merged commit 931cfd5 into anatoliychakkaev:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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);
Something went wrong with that request. Please try again.