From a081588f394eacfae41937b4e1d65df53a428cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=AF=85=E9=94=8B?= Date: Thu, 28 May 2015 14:11:09 +0800 Subject: [PATCH] node init --- .gitignore | 2 ++ package.json | 30 +++++++++++++++++++++ release.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ server.js | 7 +++++ 4 files changed, 112 insertions(+) create mode 100644 package.json create mode 100644 release.js create mode 100644 server.js diff --git a/.gitignore b/.gitignore index 123ae94..d3cc3b0 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..affd517 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/release.js b/release.js new file mode 100644 index 0000000..5c6e720 --- /dev/null +++ b/release.js @@ -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); + }); +}); diff --git a/server.js b/server.js new file mode 100644 index 0000000..b7b88c5 --- /dev/null +++ b/server.js @@ -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);