diff --git a/README.md b/README.md index e4850006..6b9d8f0e 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ $ npm install qiniu ## 许可证 -Copyright (c) 2013 qiniu.com +Copyright (c) 2014 qiniu.com 基于 MIT 协议发布: diff --git a/qiniu/conf.js b/qiniu/conf.js index 0bcb592e..f907781f 100644 --- a/qiniu/conf.js +++ b/qiniu/conf.js @@ -6,7 +6,7 @@ exports.ACCESS_KEY = ''; exports.SECRET_KEY = ''; var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json'))); -var ua = function(){ +var ua = function() { return 'QiniuNodejs/' + pkg.version + ' (' + os.type() + '; ' + os.platform() + '; ' + os.arch() + '; )'; } @@ -15,4 +15,5 @@ exports.USER_AGENT = ua(); exports.UP_HOST = 'http://upload.qiniu.com'; exports.RS_HOST = 'http://rs.qbox.me'; exports.RSF_HOST = 'http://rsf.qbox.me'; +exports.API_HOST = 'http://api.qiniu.com'; exports.RPC_TIMEOUT = 3600000; // default rpc timeout: one hour diff --git a/qiniu/fop.js b/qiniu/fop.js index 64ab72de..c353b177 100644 --- a/qiniu/fop.js +++ b/qiniu/fop.js @@ -1,5 +1,6 @@ var util = require('./util'); var rpc = require('./rpc'); +var conf = require('./conf'); var querystring = require('querystring'); @@ -38,15 +39,13 @@ ImageView.prototype.makeRequest = function(url) { return url; } -function ImageInfo() { -} +function ImageInfo() {} ImageInfo.prototype.makeRequest = function(url) { return url + '?imageInfo' } -function Exif() { -} +function Exif() {} Exif.prototype.makeRequest = function(url) { return url + '?exif' @@ -71,9 +70,8 @@ function pfop(bucket, key, fops, opts, onret) { param.force = 1; } - var uri = 'http://api.qiniu.com/pfop/'; + var uri = conf.API_HOST + '/pfop/'; var body = querystring.stringify(param); var auth = util.generateAccessToken(uri, body); rpc.postWithForm(uri, body, auth, onret); } - diff --git a/qiniu/rs.js b/qiniu/rs.js index 02a0bbf7..fabff24f 100644 --- a/qiniu/rs.js +++ b/qiniu/rs.js @@ -1,4 +1,3 @@ - var url = require('url'); var crypto = require('crypto'); var formstream = require('formstream'); @@ -59,11 +58,11 @@ Client.prototype.copy = function(bucketSrc, keySrc, bucketDest, keyDest, onret) } Client.prototype.fetch = function(url, bucket, key, onret) { - var bucketUri = getEncodedEntryUri(bucket, key); - var fetchUrl = util.urlsafeBase64Encode(url); - var uri = 'http://iovip.qbox.me/fetch/' + fetchUrl + '/to/' + bucketUri; - var digest = util.generateAccessToken(uri, null); - rpc.postWithoutForm(uri, digest, onret); + var bucketUri = getEncodedEntryUri(bucket, key); + var fetchUrl = util.urlsafeBase64Encode(url); + var uri = 'http://iovip.qbox.me/fetch/' + fetchUrl + '/to/' + bucketUri; + var digest = util.generateAccessToken(uri, null); + rpc.postWithoutForm(uri, digest, onret); } function Entry(hash, fsize, putTime, mimeType, endUser) { @@ -142,17 +141,35 @@ function getEncodedEntryUri(bucket, key) { // ----- token -------- // @gist PutPolicy -function PutPolicy(scope, callbackUrl, callbackBody, returnUrl, returnBody, - asyncOps, endUser, expires, persistentOps, persistentNotifyUrl) { - this.scope = scope || null; - this.callbackUrl = callbackUrl || null; - this.callbackBody = callbackBody || null; - this.returnUrl = returnUrl || null; - this.returnBody = returnBody || null; - this.endUser = endUser || null; - this.expires = expires || 3600; - this.persistentOps = persistentOps || null; - this.persistentNotifyUrl = persistentNotifyUrl || null; +function PutPolicy(putPolicyObj) { + + if (typeof putPolicyObj !== 'object') { + return false; + } + + this.scope = putPolicyObj.scope || null; + this.expires = putPolicyObj.expires || 3600; + this.insertOnly = putPolicyObj.insertOnly || null; + + this.saveKey = putPolicyObj.saveKey || null; + this.endUser = putPolicyObj.endUser || null; + + this.returnUrl = putPolicyObj.returnUrl || null; + this.returnBody = putPolicyObj.returnBody || null; + + this.callbackUrl = putPolicyObj.callbackUrl || null; + this.callbackHost = putPolicyObj.callbackHost || null; + this.callbackBody = putPolicyObj.callbackBody || null; + + this.persistentOps = putPolicyObj.persistentOps || null; + this.persistentNotifyUrl = putPolicyObj.persistentNotifyUrl || null; + this.persistentPipeline = putPolicyObj.persistentPipeline || null; + + this.fsizeLimit = putPolicyObj.fsizeLimit || null; + + this.detectMime = putPolicyObj.detectMime || null; + + this.mimeLimit = putPolicyObj.mimeLimit || null; } // @endgist @@ -168,26 +185,40 @@ PutPolicy.prototype.token = function(mac) { return uploadToken; } -PutPolicy.prototype.getFlags = function(putPolicy) { +PutPolicy.prototype.getFlags = function() { var flags = {}; if (this.scope != null) { flags['scope'] = this.scope; } - if (this.callbackUrl != null) { - flags['callbackUrl'] = this.callbackUrl; + flags['deadline'] = this.expires + Math.floor(Date.now() / 1000); + if (this.insertOnly != null) { + flags['insertOnly'] = this.insertOnly; } - if (this.callbackBody != null) { - flags['callbackBody'] = this.callbackBody; + + if (this.saveKey != null) { + flags['saveKey'] = this.saveKey; + } + if (this.endUser != null) { + flags['endUser'] = this.endUser; } + if (this.returnUrl != null) { flags['returnUrl'] = this.returnUrl; } if (this.returnBody != null) { flags['returnBody'] = this.returnBody; } - if (this.endUser != null) { - flags['endUser'] = this.endUser; + + if (this.callbackUrl != null) { + flags['callbackUrl'] = this.callbackUrl; + } + if (this.callbackHost != null) { + flags['callbackHost'] = this.callbackHost; } + if (this.callbackBody != null) { + flags['callbackBody'] = this.callbackBody; + } + if (this.persistentOps != null) { flags['persistentOps'] = this.persistentOps; } @@ -197,25 +228,19 @@ PutPolicy.prototype.getFlags = function(putPolicy) { if (this.persistentPipeline != null) { flags['persistentPipeline'] = this.persistentPipeline; } - if (this.mimeLimit != null) { - flags['mimeLimit'] = this.mimeLimit; - } - if (this.insertOnly != null) { - flags['insertOnly'] = this.insertOnly; + + if (this.fsizeLimit != null) { + flags['fsizeLimit'] = this.fsizeLimit; } + if (this.detectMime != null) { flags['detectMime'] = this.detectMime; } - if (this.saveKey != null) { - flags['saveKey'] = this.saveKey; - } - flags['deadline'] = this.expires + Math.floor(Date.now() / 1000); - if (this.fsizeLimit != null) { - flags['fsizeLimit'] = this.fsizeLimit; - } - if (this.insertOnly != null) { - flags['insertOnly'] = this.insertOnly; + + if (this.mimeLimit != null) { + flags['mimeLimit'] = this.mimeLimit; } + return flags; } @@ -246,5 +271,5 @@ GetPolicy.prototype.makeRequest = function(baseUrl, mac) { // query like '-thumbnail', '?imageMogr2/thumbnail/960x' and so on function makeBaseUrl(domain, key, query) { key = new Buffer(key); - return 'http://' + domain + '/' + querystring.escape(key) + (query||''); + return 'http://' + domain + '/' + querystring.escape(key) + (query || ''); } diff --git a/test/io.test.js b/test/io.test.js index b05137c7..05543ca2 100644 --- a/test/io.test.js +++ b/test/io.test.js @@ -44,8 +44,10 @@ describe('test start step1:', function() { describe('upload#', function() { var uptoken = null; beforeEach(function(done) { - var putPolicy = new qiniu.rs.PutPolicy(TEST_BUCKET); - uptoken = putPolicy.token(); + var putPolicy = new qiniu.rs.PutPolicy({ + scope: TEST_BUCKET + }); + uptoken = putPolicy.token(); done(); }); diff --git a/test/rs.test.js b/test/rs.test.js index 0c3d69b8..b35666d5 100644 --- a/test/rs.test.js +++ b/test/rs.test.js @@ -25,7 +25,9 @@ describe('test start step2:', function() { describe('single file handle', function() { before(function(done) { - var putPolicy = new qiniu.rs.PutPolicy(TEST_BUCKET); + var putPolicy = new qiniu.rs.PutPolicy({ + scope: TEST_BUCKET + }); var uptoken = putPolicy.token(); qiniu.io.putFile(uptoken, logo2, imageFile, null, function(err, ret) { should.not.exist(err);