Browse files

ensure upload image dir exists

  • Loading branch information...
1 parent 7921025 commit ed5dac369065be3dc571a7e4fe53804e8d5ceaa6 @fengmk2 fengmk2 committed Mar 3, 2012
Showing with 8 additions and 23 deletions.
  1. +1 −1 .naeignore
  2. +1 −1 config.default.js
  3. +4 −20 controllers/upload.js
  4. +2 −1 package.json
View
2 .naeignore
@@ -1,2 +1,2 @@
public/user_data/*
-.git
+.git
View
2 config.default.js
@@ -46,5 +46,5 @@ exports.config = {
var host = exports.config.host;
if (host[host.length - 1] === '/') {
- exports.config.host = host.substring(0, host.length - 1)
+ exports.config.host = host.substring(0, host.length - 1);
}
View
24 controllers/upload.js
@@ -1,6 +1,6 @@
var fs = require('fs');
var path = require('path');
-var EventProxy = require('eventproxy').EventProxy;
+var ndir = require('ndir');
var mod = require('express/node_modules/connect/node_modules/formidable');
var upload_path = path.join(path.dirname(__dirname), 'public/user_data/images');
@@ -20,23 +20,9 @@ exports.upload_image = function(req, res, next) {
var uid = req.session.user._id.toString();
var time = new Date().getTime();
var new_name = uid + time + ext;
- var proxy = new EventProxy();
var userDir = path.join(upload_path, uid);
- function ensureDir() {
- path.exists(userDir, function(exists) {
- if (!exists) {
- fs.mkdir(userDir, function(err) {
- if (err) {
- return next(err);
- }
- proxy.emit('ensureDir');
- });
- } else {
- proxy.emit('ensureDir');
- }
- });
- }
- function moveImg() {
+ ndir.mkdir(userDir, function(err) {
+ if (err) return next(err);
var new_path = path.join(userDir, new_name);
fs.rename(file.path, new_path, function(err) {
if (err) {
@@ -45,9 +31,7 @@ exports.upload_image = function(req, res, next) {
var url = 'http://' + host + '/user_data/images/' + uid + '/' + new_name;
res.json({ status: 'success', url: url });
});
- }
- proxy.once('ensureDir', moveImg);
- ensureDir();
+ });
} else {
res.json({ status: 'failed' });
return;
View
3 package.json
@@ -1,6 +1,6 @@
{
"name": "nodeclub",
- "version": "0.2.0",
+ "version": "0.2.1",
"main": "./app.js",
"private": true,
"dependencies": {
@@ -10,6 +10,7 @@
"mongoose": "2.4.1",
"node-markdown": "0.1.0",
"validator": "0.3.7",
+ "ndir": "0.1.2",
"nodemailer": "0.3.5"
}
}

0 comments on commit ed5dac3

Please sign in to comment.