Permalink
Browse files

fixed reset use not active bug; redefined upload controllers;

  • Loading branch information...
1 parent 086626c commit 0906e6347ea2daf529e63685882b33c40257b428 @fengmk2 fengmk2 committed Jun 13, 2012
View
@@ -8,12 +8,21 @@
var path = require('path');
var express = require('express');
+var ndir = require('ndir');
var config = require('./config').config;
-// 兼容旧版本的 host: http://127.0.0.1
+// host: http://127.0.0.1
var urlinfo = require('url').parse(config.host);
config.hostname = urlinfo.hostname || config.host;
var routes = require('./routes');
+config.upload_dir = config.upload_dir || path.join(__dirname, 'public', 'user_data', 'images');
+// ensure upload dir exists
+ndir.mkdir(config.upload_dir, function (err) {
+ if (err) {
+ throw err;
+ }
+});
+
var app = express.createServer();
// configuration in all env
@@ -22,7 +31,9 @@ app.configure(function () {
app.set('view engine', 'html');
app.set('views', viewsRoot);
app.register('.html', require('ejs'));
- app.use(express.bodyParser());
+ app.use(express.bodyParser({
+ uploadDir: config.upload_dir
+ }));
app.use(express.cookieParser());
app.use(express.session({
secret: config.session_secret
@@ -59,15 +70,19 @@ app.dynamicHelpers({
}
});
-var static_dir = path.join(__dirname, 'public');
+var maxAge = 3600000 * 24 * 30;
+app.use('/upload/', express.static(config.upload_dir, { maxAge: maxAge }));
+// old image url: http://host/user_data/images/xxxx
+app.use('/user_data/', express.static(path.join(__dirname, 'public', 'user_data'), { maxAge: maxAge }));
+
+var staticDir = path.join(__dirname, 'public');
app.configure('development', function () {
- app.use(express.static(static_dir));
+ app.use(express.static(staticDir));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
- var maxAge = 3600000 * 24 * 30;
- app.use(express.static(static_dir, { maxAge: maxAge }));
+ app.use(express.static(staticDir, { maxAge: maxAge }));
app.use(express.errorHandler());
app.set('view cache', true);
});
View
@@ -2,7 +2,10 @@
* config
*/
+var path = require('path');
+
exports.config = {
+ debug: true,
name: 'Node Club',
description: 'Node Club 是用Node.js开发的社区软件',
version: '0.2.2',
@@ -20,6 +23,8 @@ exports.config = {
site_static_host: '', // 静态文件存储域名
site_enable_search_preview: false, // 开启google search preview
+ upload_dir: path.join(__dirname, 'public', 'user_data', 'images'),
+
db: 'mongodb://127.0.0.1/node_club_dev',
session_secret: 'node_club',
auth_cookie_name: 'node_club',
View
@@ -78,8 +78,10 @@ exports.signup = function(req,res,next){
user.avatar = avatar_url;
user.active = false;
user.save(function(err){
- if(err) return next(err);
- mail_ctrl.send_active_mail(email,md5(email+config.session_secret),name,email,function(err,success){
+ if (err) {
+ return next(err);
+ }
+ mail_ctrl.send_active_mail(email,md5(email + config.session_secret), name,email,function(err,success){
if(success){
res.render('sign/signup', {success:'欢迎加入 ' + config.name + '!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号。'});
return;
@@ -257,6 +259,7 @@ exports.reset_pass = function(req,res,next) {
user.pass = md5(psw);
user.retrieve_key = null;
user.retrieve_time = null;
+ user.active = true; // 用户激活
user.save(function(err) {
if(err) {
return next(err);
View
@@ -1,46 +1,32 @@
var fs = require('fs');
var path = require('path');
var ndir = require('ndir');
+var config = require('../config').config;
-var mod = require('express/node_modules/connect/node_modules/formidable');
-var upload_path = path.join(path.dirname(__dirname), 'public/user_data/images');
-ndir.mkdir(upload_path, function (err) {
- if (err) {
- throw err;
- }
- mod.IncomingForm.UPLOAD_DIR = upload_path;
-});
-
-exports.upload_image = function (req, res, next) {
+exports.uploadImage = function (req, res, next) {
if (!req.session || !req.session.user) {
- res.send('forbidden!');
+ res.send({ status: 'forbidden' });
+ return;
+ }
+ var file = req.files && req.files.userfile;
+ if (!file) {
+ res.send({ status: 'failed', message: 'no file' });
return;
}
- var host = req.headers.host;
- var file = req.files.userfile;
- // sould use async
- if (file) {
- var name = file.name;
- var ext = name.substr(name.lastIndexOf('.'), 4);
- var uid = req.session.user._id.toString();
- var time = new Date().getTime();
- var new_name = uid + time + ext;
- var userDir = path.join(upload_path, uid);
- ndir.mkdir(userDir, function (err) {
+ var uid = req.session.user._id.toString();
+ var filename = Date.now() + '_' + file.name;
+ var userDir = path.join(config.upload_dir, uid);
+ ndir.mkdir(userDir, function (err) {
+ if (err) {
+ return next(err);
+ }
+ var savepath = path.join(userDir, filename);
+ fs.rename(file.path, savepath, 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) {
- return next(err);
- }
- var url = 'http://' + host + '/user_data/images/' + uid + '/' + new_name;
- res.json({ status: 'success', url: url });
- });
+ var url = '/upload/' + uid + '/' + encodeURIComponent(filename);
+ res.send({ status: 'success', url: url });
});
- } else {
- res.json({ status: 'failed' });
- return;
- }
+ });
};
Oops, something went wrong.

0 comments on commit 0906e63

Please sign in to comment.