From 8d782d8f18eab6bbbd7cd75bc42a6ecc3d934699 Mon Sep 17 00:00:00 2001 From: liangchaoboy Date: Sun, 27 Mar 2016 01:31:13 +0800 Subject: [PATCH 1/5] add force --- qiniu/rs.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++-- test/rs.test.js | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/qiniu/rs.js b/qiniu/rs.js index c4ba2330..be0c921e 100644 --- a/qiniu/rs.js +++ b/qiniu/rs.js @@ -50,6 +50,15 @@ Client.prototype.move = function(bucketSrc, keySrc, bucketDest, keyDest, onret) rpc.postWithoutForm(uri, digest, onret); } +Client.prototype.forceMove = function(bucketSrc, keySrc, bucketDest, keyDest, force, onret) { + + var encodedEntryURISrc = getEncodedEntryUri(bucketSrc, keySrc); + var encodedEntryURIDest = getEncodedEntryUri(bucketDest, keyDest); + var uri = conf.RS_HOST + '/move/' + encodedEntryURISrc + '/' + encodedEntryURIDest +'/force/'+force; + var digest = util.generateAccessToken(uri, null); + rpc.postWithoutForm(uri, digest, onret); +} + Client.prototype.copy = function(bucketSrc, keySrc, bucketDest, keyDest, onret) { var encodedEntryURISrc = getEncodedEntryUri(bucketSrc, keySrc); var encodedEntryURIDest = getEncodedEntryUri(bucketDest, keyDest); @@ -58,6 +67,16 @@ Client.prototype.copy = function(bucketSrc, keySrc, bucketDest, keyDest, onret) rpc.postWithoutForm(uri, digest, onret); } +Client.prototype.forceCopy = function(bucketSrc, keySrc, bucketDest, keyDest, force, onret) { + + var encodedEntryURISrc = getEncodedEntryUri(bucketSrc, keySrc); + var encodedEntryURIDest = getEncodedEntryUri(bucketDest, keyDest); + var uri = conf.RS_HOST + '/copy/' + encodedEntryURISrc + '/' + encodedEntryURIDest +'/force/'+force; + var digest = util.generateAccessToken(uri, null); + rpc.postWithoutForm(uri, digest, onret); +} + + Client.prototype.fetch = function(url, bucket, key, onret) { var bucketUri = getEncodedEntryUri(bucket, key); var fetchUrl = util.urlsafeBase64Encode(url); @@ -94,8 +113,15 @@ function EntryPathPair(src, dest) { this.dest = dest || null; } -EntryPathPair.prototype.toStr = function(op) { - return 'op=/' + op + '/' + this.src.encode() + '/' + this.dest.encode() + '&'; +EntryPathPair.prototype.toStr = function(op, force) { + if(typeof(force)=='undefined'){ + + return 'op=/' + op + '/' + this.src.encode() + '/' + this.dest.encode() + '&'; + + }else{ + + return 'op=/' + op + '/' + this.src.encode() + '/' + this.dest.encode() + '/force/' + force + '&'; + } } function BatchItemRet(error, code) { @@ -121,10 +147,23 @@ Client.prototype.batchMove = function(entries, onret) { fileHandle('move', entries, onret); } +Client.prototype.forceBatchMove = function(entries, force, onret) { + + fileHandleForce('move', entries, force, onret); + +} + Client.prototype.batchCopy = function(entries, onret) { fileHandle('copy', entries, onret); } +Client.prototype.forceBatchCopy = function(entries, force, onret) { + + fileHandleForce('copy', entries, force, onret); + +} + + function fileHandle(op, entries, onret) { var body = ''; for (var i in entries) { @@ -136,6 +175,18 @@ function fileHandle(op, entries, onret) { rpc.postWithForm(uri, body, digest, onret); } +function fileHandleForce(op, entries, force, onret) { + var body = ''; + for (var i in entries) { + body += entries[i].toStr(op, force); + } + + console.log(body); + var uri = conf.RS_HOST + '/batch'; + var digest = util.generateAccessToken(uri, body); + rpc.postWithForm(uri, body, digest, onret); +} + function getEncodedEntryUri(bucket, key) { return util.urlsafeBase64Encode(bucket + (key ? ':' + key : '')); } diff --git a/test/rs.test.js b/test/rs.test.js index ef1e1cbd..7fdbb159 100644 --- a/test/rs.test.js +++ b/test/rs.test.js @@ -57,6 +57,16 @@ describe('test start step2:', function() { }); }); + describe('rs.Client#forceCopy()', function() { + it('copy logo.png to logo1.png', function(done) { + client.forceCopy(TEST_BUCKET, logo, TEST_BUCKET, logo1, 1, function(err, ret) { + should.not.exist(err); + done(); + }); + }); + }); + + describe('rs.Client#remove()', function() { it('remove logo.png', function(done) { client.remove(TEST_BUCKET, logo, function(err, ret) { @@ -74,6 +84,15 @@ describe('test start step2:', function() { }); }); }); + + describe('rs.Client#forceMove()', function() { + it('move logo1.png to logo.png', function(done) { + client.forceMove(TEST_BUCKET, logo1, TEST_BUCKET, logo, 1, function(err, ret) { + should.not.exist(err); + done(); + }); + }); + }); }); describe('batch file handle', function() { @@ -141,6 +160,20 @@ describe('test start step2:', function() { }); }); + describe('rs.Client#forceBatchCopy', function() { + var entries = []; + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo), new EntryPath(TEST_BUCKET, logo1))); + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo2), new EntryPath(TEST_BUCKET, logo3))); + + it('copy from logo, logo2 to logo1, logo3', function(done) { + client.forceBatchCopy(entries, 1, function(err, ret) { + should.not.exist(err); + ret.should.eql([ { code: 200 }, { code: 200 } ]); + done(); + }); + }); + }); + describe('rs.Client#batchDelete', function() { var entries = [new EntryPath(TEST_BUCKET, logo), new EntryPath(TEST_BUCKET, logo2)]; @@ -164,6 +197,20 @@ describe('test start step2:', function() { }); }); }); + + describe('rs.Client#forceBatchMove', function() { + var entries = []; + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo1), new EntryPath(TEST_BUCKET, logo))); + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo3), new EntryPath(TEST_BUCKET, logo2))); + + it('move from logo1.png, logo3.png to logo.png, logo2.png', function(done) { + client.forceBatchMove(entries, 1, function(err, ret) { + should.not.exist(err); + done(); + }); + }); + }); + }); describe('rs.isQiniuCallBack', function() { From b9d9623d5b5b64d2e5f3a1cd92a02e73d485ddaa Mon Sep 17 00:00:00 2001 From: liangchaoboy Date: Sun, 27 Mar 2016 02:02:09 +0800 Subject: [PATCH 2/5] add force --- test/rs.test.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/rs.test.js b/test/rs.test.js index 7fdbb159..3c8b3fc9 100644 --- a/test/rs.test.js +++ b/test/rs.test.js @@ -13,6 +13,9 @@ var logo = Math.random() + 'logo.png'; var logo1 = Math.random() + 'logo1.png'; var logo2 = Math.random() + 'logo2.png'; var logo3 = Math.random() + 'logo3.png'; +var logo4 = Math.random() + 'logo4.png'; +var logo5 = Math.random() + 'logo5.png'; + describe('test start step2:', function() { describe('rs.test.js', function() { @@ -77,8 +80,8 @@ describe('test start step2:', function() { }); describe('rs.Client#move()', function() { - it('move logo1.png to logo.png', function(done) { - client.move(TEST_BUCKET, logo1, TEST_BUCKET, logo, function(err, ret) { + it('move logo.png to logo3.png', function(done) { + client.move(TEST_BUCKET, logo, TEST_BUCKET, logo3, function(err, ret) { should.not.exist(err); done(); }); @@ -86,8 +89,8 @@ describe('test start step2:', function() { }); describe('rs.Client#forceMove()', function() { - it('move logo1.png to logo.png', function(done) { - client.forceMove(TEST_BUCKET, logo1, TEST_BUCKET, logo, 1, function(err, ret) { + it('move logo3.png to logo.png', function(done) { + client.forceMove(TEST_BUCKET, logo3, TEST_BUCKET, logo, 1, function(err, ret) { should.not.exist(err); done(); }); @@ -200,10 +203,10 @@ describe('test start step2:', function() { describe('rs.Client#forceBatchMove', function() { var entries = []; - entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo1), new EntryPath(TEST_BUCKET, logo))); - entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo3), new EntryPath(TEST_BUCKET, logo2))); + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo), new EntryPath(TEST_BUCKET, logo1))); + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo2), new EntryPath(TEST_BUCKET, logo3))); - it('move from logo1.png, logo3.png to logo.png, logo2.png', function(done) { + it('move from logo.png, logo2.png to logo1.png, logo3.png', function(done) { client.forceBatchMove(entries, 1, function(err, ret) { should.not.exist(err); done(); From be9edc62018db91b16b8ace679981f7b67fa8177 Mon Sep 17 00:00:00 2001 From: liangchaoboy Date: Sun, 27 Mar 2016 02:19:56 +0800 Subject: [PATCH 3/5] add force --- test/rs.test.js | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/test/rs.test.js b/test/rs.test.js index 3c8b3fc9..d13773b5 100644 --- a/test/rs.test.js +++ b/test/rs.test.js @@ -15,6 +15,10 @@ var logo2 = Math.random() + 'logo2.png'; var logo3 = Math.random() + 'logo3.png'; var logo4 = Math.random() + 'logo4.png'; var logo5 = Math.random() + 'logo5.png'; +var logo6 = Math.random() + 'logo6.png'; +var logo7 = Math.random() + 'logo7.png'; + + describe('test start step2:', function() { @@ -32,13 +36,25 @@ describe('test start step2:', function() { TEST_BUCKET ); var uptoken = putPolicy.token(); + qiniu.io.putFile(uptoken, logo, imageFile, null, function(err, ret) { + should.not.exist(err); + }); + + qiniu.io.putFile(uptoken, logo1, imageFile, null, function(err, ret) { + should.not.exist(err); + done(); + }); + qiniu.io.putFile(uptoken, logo2, imageFile, null, function(err, ret) { should.not.exist(err); + done(); }); - qiniu.io.putFile(uptoken, logo, imageFile, null, function(err, ret) { + + qiniu.io.putFile(uptoken, logo3, imageFile, null, function(err, ret) { should.not.exist(err); done(); }); + }); describe('rs.Client#stat()', function() { @@ -52,8 +68,8 @@ describe('test start step2:', function() { }); describe('rs.Client#copy()', function() { - it('copy logo.png to logo1.png', function(done) { - client.copy(TEST_BUCKET, logo, TEST_BUCKET, logo1, function(err, ret) { + it('copy logo.png to logo5.png', function(done) { + client.copy(TEST_BUCKET, logo, TEST_BUCKET, logo5, function(err, ret) { should.not.exist(err); done(); }); @@ -71,8 +87,8 @@ describe('test start step2:', function() { describe('rs.Client#remove()', function() { - it('remove logo.png', function(done) { - client.remove(TEST_BUCKET, logo, function(err, ret) { + it('remove logo5.png', function(done) { + client.remove(TEST_BUCKET, logo5, function(err, ret) { should.not.exist(err); done(); }); @@ -80,8 +96,8 @@ describe('test start step2:', function() { }); describe('rs.Client#move()', function() { - it('move logo.png to logo3.png', function(done) { - client.move(TEST_BUCKET, logo, TEST_BUCKET, logo3, function(err, ret) { + it('move logo.png to logo5.png', function(done) { + client.move(TEST_BUCKET, logo, TEST_BUCKET, logo5, function(err, ret) { should.not.exist(err); done(); }); @@ -89,8 +105,8 @@ describe('test start step2:', function() { }); describe('rs.Client#forceMove()', function() { - it('move logo3.png to logo.png', function(done) { - client.forceMove(TEST_BUCKET, logo3, TEST_BUCKET, logo, 1, function(err, ret) { + it('move logo5.png to logo.png', function(done) { + client.forceMove(TEST_BUCKET, logo5, TEST_BUCKET, logo, 1, function(err, ret) { should.not.exist(err); done(); }); @@ -151,10 +167,10 @@ describe('test start step2:', function() { describe('rs.Client#batchCopy', function() { var entries = []; - entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo), new EntryPath(TEST_BUCKET, logo1))); - entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo2), new EntryPath(TEST_BUCKET, logo3))); + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo), new EntryPath(TEST_BUCKET, logo6))); + entries.push(new EntryPathPair(new EntryPath(TEST_BUCKET, logo2), new EntryPath(TEST_BUCKET, logo7))); - it('copy from logo, logo2 to logo1, logo3', function(done) { + it('copy from logo, logo2 to logo6, logo7', function(done) { client.batchCopy(entries, function(err, ret) { should.not.exist(err); ret.should.eql([ { code: 200 }, { code: 200 } ]); From 1904cdd991e106a2babd6f11d782d3e6b96fc010 Mon Sep 17 00:00:00 2001 From: liangchaoboy Date: Sun, 27 Mar 2016 02:33:44 +0800 Subject: [PATCH 4/5] add force --- test/rs.test.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/test/rs.test.js b/test/rs.test.js index d13773b5..51088f17 100644 --- a/test/rs.test.js +++ b/test/rs.test.js @@ -36,13 +36,9 @@ describe('test start step2:', function() { TEST_BUCKET ); var uptoken = putPolicy.token(); - qiniu.io.putFile(uptoken, logo, imageFile, null, function(err, ret) { - should.not.exist(err); - }); - qiniu.io.putFile(uptoken, logo1, imageFile, null, function(err, ret) { + qiniu.io.putFile(uptoken, logo, imageFile, null, function(err, ret) { should.not.exist(err); - done(); }); qiniu.io.putFile(uptoken, logo2, imageFile, null, function(err, ret) { @@ -50,11 +46,6 @@ describe('test start step2:', function() { done(); }); - qiniu.io.putFile(uptoken, logo3, imageFile, null, function(err, ret) { - should.not.exist(err); - done(); - }); - }); describe('rs.Client#stat()', function() { @@ -68,7 +59,7 @@ describe('test start step2:', function() { }); describe('rs.Client#copy()', function() { - it('copy logo.png to logo5.png', function(done) { + it('copy logo.png to logo1.png', function(done) { client.copy(TEST_BUCKET, logo, TEST_BUCKET, logo5, function(err, ret) { should.not.exist(err); done(); @@ -77,7 +68,7 @@ describe('test start step2:', function() { }); describe('rs.Client#forceCopy()', function() { - it('copy logo.png to logo1.png', function(done) { + it('copy logo.png to logo3.png', function(done) { client.forceCopy(TEST_BUCKET, logo, TEST_BUCKET, logo1, 1, function(err, ret) { should.not.exist(err); done(); From 4ce199c7e3b4b98f6e781b12e41a9234d8d94e37 Mon Sep 17 00:00:00 2001 From: liangchaoboy Date: Mon, 28 Mar 2016 23:28:02 +0800 Subject: [PATCH 5/5] fixTest --- qiniu/rs.js | 3 +++ test/rs.test.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/qiniu/rs.js b/qiniu/rs.js index be0c921e..27a31229 100644 --- a/qiniu/rs.js +++ b/qiniu/rs.js @@ -55,6 +55,7 @@ Client.prototype.forceMove = function(bucketSrc, keySrc, bucketDest, keyDest, fo var encodedEntryURISrc = getEncodedEntryUri(bucketSrc, keySrc); var encodedEntryURIDest = getEncodedEntryUri(bucketDest, keyDest); var uri = conf.RS_HOST + '/move/' + encodedEntryURISrc + '/' + encodedEntryURIDest +'/force/'+force; + var digest = util.generateAccessToken(uri, null); rpc.postWithoutForm(uri, digest, onret); } @@ -63,6 +64,7 @@ Client.prototype.copy = function(bucketSrc, keySrc, bucketDest, keyDest, onret) var encodedEntryURISrc = getEncodedEntryUri(bucketSrc, keySrc); var encodedEntryURIDest = getEncodedEntryUri(bucketDest, keyDest); var uri = conf.RS_HOST + '/copy/' + encodedEntryURISrc + '/' + encodedEntryURIDest; + var digest = util.generateAccessToken(uri, null); rpc.postWithoutForm(uri, digest, onret); } @@ -72,6 +74,7 @@ Client.prototype.forceCopy = function(bucketSrc, keySrc, bucketDest, keyDest, fo var encodedEntryURISrc = getEncodedEntryUri(bucketSrc, keySrc); var encodedEntryURIDest = getEncodedEntryUri(bucketDest, keyDest); var uri = conf.RS_HOST + '/copy/' + encodedEntryURISrc + '/' + encodedEntryURIDest +'/force/'+force; + var digest = util.generateAccessToken(uri, null); rpc.postWithoutForm(uri, digest, onret); } diff --git a/test/rs.test.js b/test/rs.test.js index 51088f17..c22a5b0e 100644 --- a/test/rs.test.js +++ b/test/rs.test.js @@ -59,7 +59,7 @@ describe('test start step2:', function() { }); describe('rs.Client#copy()', function() { - it('copy logo.png to logo1.png', function(done) { + it('copy logo.png to logo5.png', function(done) { client.copy(TEST_BUCKET, logo, TEST_BUCKET, logo5, function(err, ret) { should.not.exist(err); done();