Skip to content

Commit a0fbfc5

Browse files
committed
Merge pull request #140 from longbai/rollback_policy
Rollback policy
2 parents 1870bae + 56d3cbd commit a0fbfc5

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
## CHANGE LOG
22

3+
### v6.1.8
4+
5+
2015-05-13
6+
7+
- 上传增加putpolicy2
8+
39
### v6.1.7
410

511
2015-05-09
612

7-
- 上传策略增加 callbackHost、persistentPipeline, callbackFetchKey
13+
- 上传putpolicy2增加 callbackHost、persistentPipeline, callbackFetchKey
814
- 增加fetch 函数
915
- imageview -> imageview2
1016

@@ -13,7 +19,7 @@
1319

1420
2014-10-31
1521

16-
- 上传策略增加fsizelimit, insertonly
22+
- 上传putpolicy2增加fsizelimit, insertonly
1723

1824

1925
### v6.1.5

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
TESTS = test/*.test.js
22
TIMEOUT = 25000
33
REPORTER = spec
4-
MOCHA_OPTS =
5-
test:
4+
MOCHA_OPTS =
5+
test:
66
@NODE_ENV=test ./node_modules/.bin/mocha \
77
--require should \
88
--reporter $(REPORTER) \

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qiniu",
3-
"version": "6.1.7",
3+
"version": "6.1.8",
44
"description": "Node wrapper for Qiniu Resource (Cloud) Storage API",
55
"main": "index.js",
66
"directories": {

qiniu/rs.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exports.BatchItemRet = BatchItemRet;
1616
exports.BatchStatItemRet = BatchStatItemRet;
1717

1818
exports.PutPolicy = PutPolicy;
19+
exports.PutPolicy2 = PutPolicy2;
1920
exports.GetPolicy = GetPolicy;
2021
exports.makeBaseUrl = makeBaseUrl;
2122

@@ -141,7 +142,48 @@ function getEncodedEntryUri(bucket, key) {
141142

142143
// ----- token --------
143144
// @gist PutPolicy
144-
function PutPolicy(putPolicyObj) {
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;
156+
}
157+
// @endgist
158+
159+
PutPolicy.prototype.token = function(mac) {
160+
if (mac == null) {
161+
mac = new Mac(conf.ACCESS_KEY, conf.SECRET_KEY);
162+
}
163+
var flags = this.getFlags();
164+
var encodedFlags = util.urlsafeBase64Encode(JSON.stringify(flags));
165+
var encoded = util.hmacSha1(encodedFlags, mac.secretKey);
166+
var encodedSign = util.base64ToUrlSafe(encoded);
167+
var uploadToken = mac.accessKey + ':' + encodedSign + ':' + encodedFlags;
168+
return uploadToken;
169+
}
170+
171+
PutPolicy.prototype.getFlags = function() {
172+
var flags = {};
173+
var attrs = ['scope', 'insertOnly', 'saveKey', 'endUser', 'returnUrl', 'returnBody', 'callbackUrl', 'callbackHost', 'callbackBody', 'callbackBodyType', 'callbackFetchKey', 'persistentOps', 'persistentNotifyUrl', 'persistentPipeline', 'fsizeLimit', 'detectMime', 'mimeLimit'];
174+
175+
for (var i = attrs.length - 1; i >= 0; i--) {
176+
if (this[attrs[i]] !== null) {
177+
flags[attrs[i]] = this[attrs[i]];
178+
}
179+
}
180+
181+
flags['deadline'] = this.expires + Math.floor(Date.now() / 1000);
182+
183+
return flags;
184+
}
185+
186+
function PutPolicy2(putPolicyObj) {
145187

146188
if (typeof putPolicyObj !== 'object') {
147189
return false;
@@ -171,9 +213,8 @@ function PutPolicy(putPolicyObj) {
171213

172214
this.mimeLimit = putPolicyObj.mimeLimit || null;
173215
}
174-
// @endgist
175216

176-
PutPolicy.prototype.token = function(mac) {
217+
PutPolicy2.prototype.token = function(mac) {
177218
if (mac == null) {
178219
mac = new Mac(conf.ACCESS_KEY, conf.SECRET_KEY);
179220
}
@@ -185,7 +226,7 @@ PutPolicy.prototype.token = function(mac) {
185226
return uploadToken;
186227
}
187228

188-
PutPolicy.prototype.getFlags = function() {
229+
PutPolicy2.prototype.getFlags = function() {
189230
var flags = {};
190231
var attrs = ['scope', 'insertOnly', 'saveKey', 'endUser', 'returnUrl', 'returnBody', 'callbackUrl', 'callbackHost', 'callbackBody', 'callbackBodyType', 'callbackFetchKey', 'persistentOps', 'persistentNotifyUrl', 'persistentPipeline', 'fsizeLimit', 'detectMime', 'mimeLimit'];
191232

test/io.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +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({
48-
scope: TEST_BUCKET
49-
});
50-
uptoken = putPolicy.token();
47+
var putPolicy = new qiniu.rs.PutPolicy(
48+
TEST_BUCKET
49+
);
50+
uptoken = putPolicy.token();
5151
done();
5252
});
5353

test/rs.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +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({
29-
scope: TEST_BUCKET
30-
});
28+
var putPolicy = new qiniu.rs.PutPolicy(
29+
TEST_BUCKET
30+
);
3131
var uptoken = putPolicy.token();
3232
qiniu.io.putFile(uptoken, logo2, imageFile, null, function(err, ret) {
3333
should.not.exist(err);

0 commit comments

Comments
 (0)