Skip to content

Commit

Permalink
node init
Browse files Browse the repository at this point in the history
  • Loading branch information
梁毅锋 committed May 28, 2015
1 parent 19e687d commit a081588
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.idea
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name" : "kerugui.cn",
"version" : "1.0.0",
"description" : "客如归官网",
"main" : "release.js",
"scripts" : {
"start" : "nodemon server.js",
"release": "node release.js"
},
"repository" : {
"type": "git",
"url" : "git+https://github.com/4DKelvin/kerugui.cn.git"
},
"keywords" : [
"website"
],
"author" : "Kelvin",
"license" : "ISC",
"bugs" : {
"url": "https://github.com/4DKelvin/kerugui.cn/issues"
},
"homepage" : "https://github.com/4DKelvin/kerugui.cn#readme",
"dependencies" : {
"express": "^4.12.4"
},
"devDependencies": {
"qiniu" : "^6.1.2",
"nodemon": "^1.3.0"
}
}
73 changes: 73 additions & 0 deletions release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var fs = require("fs"),
qiniu = require("qiniu");
//七牛配置
qiniu.conf.ACCESS_KEY = "7dRQxdIWcAOGCeOxy8_-JqH0hkYMRwbeu5p6KZ0O";
qiniu.conf.SECRET_KEY = "DQXyFybgjmjqE0LQGPm4JkVNxNlvKCqsPT6Ukiff";
qiniu.conf.BUCKET_NAME = "imageload";

//上传的目录
var UPLOAD_DIR = 'resources';

var uploadFile = function (localFile, key) {
"use strict";
var extra = new qiniu.io.PutExtra(),
uptoken = new qiniu.rs.PutPolicy(qiniu.conf.BUCKET_NAME).token();

qiniu.io.putFile(uptoken, key, localFile, extra, function (err, ret) {
if (!err) {
// 上传成功, 处理返回值
console.log('[上传成功] [' + key + '] ');
} else {
// 上传失败, 处理返回代码
console.log('[上传失败] [' + key + '] ', err);
//if (err.code == 614) {
//文件存在先删除
var client = new qiniu.rs.Client();
client.remove(qiniu.conf.BUCKET_NAME, key, function (err, ret) {
if (!err) {
// ok
console.log('[删除成功] [' + key + '] 正在重新上传……');
uploadFile(localFile, key);
} else {
console.log('[删除失败] [' + key + '] ', err);
}
});
//}
}
});
};

var walk = function (dir, done) {
"use strict";
var results = [];
fs.readdir(dir, function (err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};

walk(UPLOAD_DIR, function (err, results) {
"use strict";
if (err) throw err;
console.log(results.length + '个文件正准备上传……');
results.forEach(function (file, index) {
var key = file.replace(UPLOAD_DIR + '/', '');
uploadFile(file, key);
});
});
7 changes: 7 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Created by Kelvin on 5/8/15.
*/
"use strict";
var express = require("express"),
app = express();
app.use(express.static('resources')).listen(4000);

0 comments on commit a081588

Please sign in to comment.