Skip to content

Commit

Permalink
Merge pull request #140 from longbai/rollback_policy
Browse files Browse the repository at this point in the history
Rollback policy
  • Loading branch information
longbai committed May 13, 2015
2 parents 1870bae + 56d3cbd commit a0fbfc5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
## CHANGE LOG

### v6.1.8

2015-05-13

- 上传增加putpolicy2

### v6.1.7

2015-05-09

- 上传策略增加 callbackHost、persistentPipeline, callbackFetchKey
- 上传putpolicy2增加 callbackHost、persistentPipeline, callbackFetchKey
- 增加fetch 函数
- imageview -> imageview2

Expand All @@ -13,7 +19,7 @@

2014-10-31

- 上传策略增加fsizelimit, insertonly
- 上传putpolicy2增加fsizelimit, insertonly


### v6.1.5
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
TESTS = test/*.test.js
TIMEOUT = 25000
REPORTER = spec
MOCHA_OPTS =
test:
MOCHA_OPTS =
test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require should \
--reporter $(REPORTER) \
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qiniu",
"version": "6.1.7",
"version": "6.1.8",
"description": "Node wrapper for Qiniu Resource (Cloud) Storage API",
"main": "index.js",
"directories": {
Expand Down
49 changes: 45 additions & 4 deletions qiniu/rs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.BatchItemRet = BatchItemRet;
exports.BatchStatItemRet = BatchStatItemRet;

exports.PutPolicy = PutPolicy;
exports.PutPolicy2 = PutPolicy2;
exports.GetPolicy = GetPolicy;
exports.makeBaseUrl = makeBaseUrl;

Expand Down Expand Up @@ -141,7 +142,48 @@ function getEncodedEntryUri(bucket, key) {

// ----- token --------
// @gist PutPolicy
function PutPolicy(putPolicyObj) {
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;
}
// @endgist

PutPolicy.prototype.token = function(mac) {
if (mac == null) {
mac = new Mac(conf.ACCESS_KEY, conf.SECRET_KEY);
}
var flags = this.getFlags();
var encodedFlags = util.urlsafeBase64Encode(JSON.stringify(flags));
var encoded = util.hmacSha1(encodedFlags, mac.secretKey);
var encodedSign = util.base64ToUrlSafe(encoded);
var uploadToken = mac.accessKey + ':' + encodedSign + ':' + encodedFlags;
return uploadToken;
}

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

for (var i = attrs.length - 1; i >= 0; i--) {
if (this[attrs[i]] !== null) {
flags[attrs[i]] = this[attrs[i]];
}
}

flags['deadline'] = this.expires + Math.floor(Date.now() / 1000);

return flags;
}

function PutPolicy2(putPolicyObj) {

if (typeof putPolicyObj !== 'object') {
return false;
Expand Down Expand Up @@ -171,9 +213,8 @@ function PutPolicy(putPolicyObj) {

this.mimeLimit = putPolicyObj.mimeLimit || null;
}
// @endgist

PutPolicy.prototype.token = function(mac) {
PutPolicy2.prototype.token = function(mac) {
if (mac == null) {
mac = new Mac(conf.ACCESS_KEY, conf.SECRET_KEY);
}
Expand All @@ -185,7 +226,7 @@ PutPolicy.prototype.token = function(mac) {
return uploadToken;
}

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

Expand Down
8 changes: 4 additions & 4 deletions test/io.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ describe('test start step1:', function() {
describe('upload#', function() {
var uptoken = null;
beforeEach(function(done) {
var putPolicy = new qiniu.rs.PutPolicy({
scope: TEST_BUCKET
});
uptoken = putPolicy.token();
var putPolicy = new qiniu.rs.PutPolicy(
TEST_BUCKET
);
uptoken = putPolicy.token();
done();
});

Expand Down
6 changes: 3 additions & 3 deletions test/rs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ describe('test start step2:', function() {
describe('single file handle', function() {

before(function(done) {
var putPolicy = new qiniu.rs.PutPolicy({
scope: TEST_BUCKET
});
var putPolicy = new qiniu.rs.PutPolicy(
TEST_BUCKET
);
var uptoken = putPolicy.token();
qiniu.io.putFile(uptoken, logo2, imageFile, null, function(err, ret) {
should.not.exist(err);
Expand Down

0 comments on commit a0fbfc5

Please sign in to comment.