Skip to content

Commit ecaf534

Browse files
committed
Merge pull request #124 from SunLn/develop
少量重构、新增上传策略参数
2 parents f60d960 + 9de5372 commit ecaf534

File tree

6 files changed

+78
-50
lines changed

6 files changed

+78
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $ npm install qiniu
4141

4242
## 许可证
4343

44-
Copyright (c) 2013 qiniu.com
44+
Copyright (c) 2014 qiniu.com
4545

4646
基于 MIT 协议发布:
4747

qiniu/conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports.ACCESS_KEY = '<PLEASE APPLY YOUR ACCESS KEY>';
66
exports.SECRET_KEY = '<DONT SEND YOUR SECRET KEY TO ANYONE>';
77

88
var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json')));
9-
var ua = function(){
9+
var ua = function() {
1010
return 'QiniuNodejs/' + pkg.version + ' (' + os.type() + '; ' + os.platform() + '; ' + os.arch() + '; )';
1111
}
1212

@@ -15,4 +15,5 @@ exports.USER_AGENT = ua();
1515
exports.UP_HOST = 'http://upload.qiniu.com';
1616
exports.RS_HOST = 'http://rs.qbox.me';
1717
exports.RSF_HOST = 'http://rsf.qbox.me';
18+
exports.API_HOST = 'http://api.qiniu.com';
1819
exports.RPC_TIMEOUT = 3600000; // default rpc timeout: one hour

qiniu/fop.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var util = require('./util');
22
var rpc = require('./rpc');
3+
var conf = require('./conf');
34

45
var querystring = require('querystring');
56

@@ -38,15 +39,13 @@ ImageView.prototype.makeRequest = function(url) {
3839
return url;
3940
}
4041

41-
function ImageInfo() {
42-
}
42+
function ImageInfo() {}
4343

4444
ImageInfo.prototype.makeRequest = function(url) {
4545
return url + '?imageInfo'
4646
}
4747

48-
function Exif() {
49-
}
48+
function Exif() {}
5049

5150
Exif.prototype.makeRequest = function(url) {
5251
return url + '?exif'
@@ -74,9 +73,8 @@ function pfop(bucket, key, fops, opts, onret) {
7473
param.pipeline = opts.pipeline;
7574
}
7675

77-
var uri = 'http://api.qiniu.com/pfop/';
76+
var uri = conf.API_HOST + '/pfop/';
7877
var body = querystring.stringify(param);
7978
var auth = util.generateAccessToken(uri, body);
8079
rpc.postWithForm(uri, body, auth, onret);
8180
}
82-

qiniu/rs.js

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
var url = require('url');
32
var crypto = require('crypto');
43
var formstream = require('formstream');
@@ -59,11 +58,11 @@ Client.prototype.copy = function(bucketSrc, keySrc, bucketDest, keyDest, onret)
5958
}
6059

6160
Client.prototype.fetch = function(url, bucket, key, onret) {
62-
var bucketUri = getEncodedEntryUri(bucket, key);
63-
var fetchUrl = util.urlsafeBase64Encode(url);
64-
var uri = 'http://iovip.qbox.me/fetch/' + fetchUrl + '/to/' + bucketUri;
65-
var digest = util.generateAccessToken(uri, null);
66-
rpc.postWithoutForm(uri, digest, onret);
61+
var bucketUri = getEncodedEntryUri(bucket, key);
62+
var fetchUrl = util.urlsafeBase64Encode(url);
63+
var uri = 'http://iovip.qbox.me/fetch/' + fetchUrl + '/to/' + bucketUri;
64+
var digest = util.generateAccessToken(uri, null);
65+
rpc.postWithoutForm(uri, digest, onret);
6766
}
6867

6968
function Entry(hash, fsize, putTime, mimeType, endUser) {
@@ -142,17 +141,35 @@ function getEncodedEntryUri(bucket, key) {
142141

143142
// ----- token --------
144143
// @gist PutPolicy
145-
function PutPolicy(scope, callbackUrl, callbackBody, returnUrl, returnBody,
146-
asyncOps, endUser, expires, persistentOps, persistentNotifyUrl) {
147-
this.scope = scope || null;
148-
this.callbackUrl = callbackUrl || null;
149-
this.callbackBody = callbackBody || null;
150-
this.returnUrl = returnUrl || null;
151-
this.returnBody = returnBody || null;
152-
this.endUser = endUser || null;
153-
this.expires = expires || 3600;
154-
this.persistentOps = persistentOps || null;
155-
this.persistentNotifyUrl = persistentNotifyUrl || null;
144+
function PutPolicy(putPolicyObj) {
145+
146+
if (typeof putPolicyObj !== 'object') {
147+
return false;
148+
}
149+
150+
this.scope = putPolicyObj.scope || null;
151+
this.expires = putPolicyObj.expires || 3600;
152+
this.insertOnly = putPolicyObj.insertOnly || null;
153+
154+
this.saveKey = putPolicyObj.saveKey || null;
155+
this.endUser = putPolicyObj.endUser || null;
156+
157+
this.returnUrl = putPolicyObj.returnUrl || null;
158+
this.returnBody = putPolicyObj.returnBody || null;
159+
160+
this.callbackUrl = putPolicyObj.callbackUrl || null;
161+
this.callbackHost = putPolicyObj.callbackHost || null;
162+
this.callbackBody = putPolicyObj.callbackBody || null;
163+
164+
this.persistentOps = putPolicyObj.persistentOps || null;
165+
this.persistentNotifyUrl = putPolicyObj.persistentNotifyUrl || null;
166+
this.persistentPipeline = putPolicyObj.persistentPipeline || null;
167+
168+
this.fsizeLimit = putPolicyObj.fsizeLimit || null;
169+
170+
this.detectMime = putPolicyObj.detectMime || null;
171+
172+
this.mimeLimit = putPolicyObj.mimeLimit || null;
156173
}
157174
// @endgist
158175

@@ -168,26 +185,40 @@ PutPolicy.prototype.token = function(mac) {
168185
return uploadToken;
169186
}
170187

171-
PutPolicy.prototype.getFlags = function(putPolicy) {
188+
PutPolicy.prototype.getFlags = function() {
172189
var flags = {};
173190
if (this.scope != null) {
174191
flags['scope'] = this.scope;
175192
}
176-
if (this.callbackUrl != null) {
177-
flags['callbackUrl'] = this.callbackUrl;
193+
flags['deadline'] = this.expires + Math.floor(Date.now() / 1000);
194+
if (this.insertOnly != null) {
195+
flags['insertOnly'] = this.insertOnly;
178196
}
179-
if (this.callbackBody != null) {
180-
flags['callbackBody'] = this.callbackBody;
197+
198+
if (this.saveKey != null) {
199+
flags['saveKey'] = this.saveKey;
200+
}
201+
if (this.endUser != null) {
202+
flags['endUser'] = this.endUser;
181203
}
204+
182205
if (this.returnUrl != null) {
183206
flags['returnUrl'] = this.returnUrl;
184207
}
185208
if (this.returnBody != null) {
186209
flags['returnBody'] = this.returnBody;
187210
}
188-
if (this.endUser != null) {
189-
flags['endUser'] = this.endUser;
211+
212+
if (this.callbackUrl != null) {
213+
flags['callbackUrl'] = this.callbackUrl;
214+
}
215+
if (this.callbackHost != null) {
216+
flags['callbackHost'] = this.callbackHost;
190217
}
218+
if (this.callbackBody != null) {
219+
flags['callbackBody'] = this.callbackBody;
220+
}
221+
191222
if (this.persistentOps != null) {
192223
flags['persistentOps'] = this.persistentOps;
193224
}
@@ -197,25 +228,19 @@ PutPolicy.prototype.getFlags = function(putPolicy) {
197228
if (this.persistentPipeline != null) {
198229
flags['persistentPipeline'] = this.persistentPipeline;
199230
}
200-
if (this.mimeLimit != null) {
201-
flags['mimeLimit'] = this.mimeLimit;
202-
}
203-
if (this.insertOnly != null) {
204-
flags['insertOnly'] = this.insertOnly;
231+
232+
if (this.fsizeLimit != null) {
233+
flags['fsizeLimit'] = this.fsizeLimit;
205234
}
235+
206236
if (this.detectMime != null) {
207237
flags['detectMime'] = this.detectMime;
208238
}
209-
if (this.saveKey != null) {
210-
flags['saveKey'] = this.saveKey;
211-
}
212-
flags['deadline'] = this.expires + Math.floor(Date.now() / 1000);
213-
if (this.fsizeLimit != null) {
214-
flags['fsizeLimit'] = this.fsizeLimit;
215-
}
216-
if (this.insertOnly != null) {
217-
flags['insertOnly'] = this.insertOnly;
239+
240+
if (this.mimeLimit != null) {
241+
flags['mimeLimit'] = this.mimeLimit;
218242
}
243+
219244
return flags;
220245
}
221246

@@ -246,5 +271,5 @@ GetPolicy.prototype.makeRequest = function(baseUrl, mac) {
246271
// query like '-thumbnail', '?imageMogr2/thumbnail/960x' and so on
247272
function makeBaseUrl(domain, key, query) {
248273
key = new Buffer(key);
249-
return 'http://' + domain + '/' + querystring.escape(key) + (query||'');
274+
return 'http://' + domain + '/' + querystring.escape(key) + (query || '');
250275
}

test/io.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ describe('test start step1:', function() {
4444
describe('upload#', function() {
4545
var uptoken = null;
4646
beforeEach(function(done) {
47-
var putPolicy = new qiniu.rs.PutPolicy(TEST_BUCKET);
48-
uptoken = putPolicy.token();
47+
var putPolicy = new qiniu.rs.PutPolicy({
48+
scope: TEST_BUCKET
49+
});
50+
uptoken = putPolicy.token();
4951
done();
5052
});
5153

test/rs.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ describe('test start step2:', function() {
2525
describe('single file handle', function() {
2626

2727
before(function(done) {
28-
var putPolicy = new qiniu.rs.PutPolicy(TEST_BUCKET);
28+
var putPolicy = new qiniu.rs.PutPolicy({
29+
scope: TEST_BUCKET
30+
});
2931
var uptoken = putPolicy.token();
3032
qiniu.io.putFile(uptoken, logo2, imageFile, null, function(err, ret) {
3133
should.not.exist(err);

0 commit comments

Comments
 (0)