Commit 167ebc1d authored by Junming Liu's avatar Junming Liu

The latest version of QiniuStorage

This is the lastest QiniuStorage file. Because of Qiniu not support resource manage operations, so it's only have putAttachment and getAttachment.
The method to upload or download file are using URiTemplate via Ajax. Besides, i use CryptoJS to do HMAC-SHA1 algorithm.
parent 391e48d5
/* /*
* Copyright 2013, Nexedi SA * Copyright 2015, Nexedi SA
* Released under the LGPL license. * Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html * http://www.gnu.org/licenses/lgpl.html
*/ */
...@@ -7,22 +7,20 @@ ...@@ -7,22 +7,20 @@
/** /**
* JIO Qiniu Storage. Type = "qiniu". * JIO Qiniu Storage. Type = "qiniu".
* Qiniu "database" storage. * Qiniu "database" storage.
* Qiniu storage not support resource manage operation,
* so remove,removeAttachment,allDocs,allAttachment isn't working
*/ */
/*global FormData, btoa, Blob, CryptoJS, define, jIO */
/*jslint indent: 2, maxlen: 80, nomen: true, unparam: true, bitwise: true */ /*global JSON, FormData, btoa, Blob, CryptoJS, define,
(function (dependencies, module) { jIO, RSVP, console, UriTemplate */
"use strict"; /*jslint indent: 2, maxlen: 80, nomen: true, bitwise: true */
if (typeof define === 'function' && define.amd) { (function (jIO, RSVP, Blob, UriTemplate) {
return define(dependencies, module);
}
// if (typeof exports === 'object') {
// return module(exports, require('jio'));
// }
module(jIO);
}([
'jio'
], function (jIO) {
"use strict"; "use strict";
var METADATA_URL = "http://{+bucket}/{+key}?e=" +
"{+DEADLINE}&token={+access_key}:{+token}",
metadata_template = UriTemplate.parse(METADATA_URL),
UPLOAD_URL = "http://up.qiniu.com/",
DEADLINE = 2451491200;
function urlsafe_base64_encode(string) { function urlsafe_base64_encode(string) {
return string return string
...@@ -40,6 +38,7 @@ ...@@ -40,6 +38,7 @@
// b2, b3 to b3, b2, b1, b0. // b2, b3 to b3, b2, b1, b0.
var encodedArray = [], var encodedArray = [],
i, i,
//change CrytoJS to rusha
encoded = CryptoJS.HmacSHA1(message, secret_key), encoded = CryptoJS.HmacSHA1(message, secret_key),
encodedString; encodedString;
for (i = 0; i < 5; i = i + 1) { for (i = 0; i < 5; i = i + 1) {
...@@ -55,8 +54,6 @@ ...@@ -55,8 +54,6 @@
return urlsafe_base64_encode(btoa(encodedString)); return urlsafe_base64_encode(btoa(encodedString));
} }
var UPLOAD_URL = "http://up.qiniu.com/",
DEADLINE = 2451491200;
/** /**
* The JIO QiniuStorage extension * The JIO QiniuStorage extension
...@@ -93,15 +90,16 @@ ...@@ -93,15 +90,16 @@
data = new FormData(); data = new FormData();
if (update === true) { if (update === true) {
put_policy = JSON.stringify({ put_policy = JSON.stringify({
"scope": this._bucket + ':' + key, "scope": "bucket" + ':' + key,
"deadline": DEADLINE "deadline": DEADLINE
}); });
} else { } else {
put_policy = JSON.stringify({ put_policy = JSON.stringify({
"scope": this._bucket, "scope": "bucket",
"deadline": DEADLINE "deadline": DEADLINE
}); });
} }
encoded = btoa(put_policy); encoded = btoa(put_policy);
encode_signed = b64_hmac_sha1(this._secret_key, encoded); encode_signed = b64_hmac_sha1(this._secret_key, encoded);
upload_token = this._access_key + ":" + encode_signed + ":" + encoded; upload_token = this._access_key + ":" + encode_signed + ":" + encoded;
...@@ -110,10 +108,7 @@ ...@@ -110,10 +108,7 @@
data.append("token", upload_token); data.append("token", upload_token);
data.append( data.append(
"file", "file",
// new Blob([JSON.stringify(doc)], {type: "application/json"}),
// new Blob([doc], {type: "application/json"}),
blob, blob,
// new Blob([], {type: "application/octet-stream"}),
key key
); );
...@@ -125,248 +120,101 @@ ...@@ -125,248 +120,101 @@
}; };
/** /** * Add an attachment to a document
* Create a document.
*
* @method post
* @param {Object} command The JIO command
* @param {Object} metadata The metadata to store
*/
QiniuStorage.prototype.post = function (command, metadata) {
var doc = jIO.util.deepClone(metadata),
doc_id = metadata._id;
if (!doc_id) {
doc_id = jIO.util.generateUuid();
doc._id = doc_id;
}
return this._put(
doc_id,
new Blob([JSON.stringify(doc)], {type: "application/json"})
).then(function (doc) {
if (doc !== null) {
command.success({"id": doc_id});
} else {
command.error(
"not_found",
"missing",
"Cannot find document"
);
}
}).fail(function (event) {
command.error(
event.target.status,
event.target.statusText,
"Unable to post doc"
);
});
};
/**
* Update/create a document.
* *
* @method put * @method putAttachment
* @param {Object} command The JIO command * @id {Object} Document id
* @param {Object} metadata The metadata to store * @param {Object} param The given parameters
* @blob {Object} attachment packaged into a blob
*/ */
QiniuStorage.prototype.put = function (command, metadata) { QiniuStorage.prototype.putAttachment = function (id, param, blob) {
return this._put( var gadget = this;
metadata._id, return new RSVP.Queue()
new Blob([JSON.stringify(metadata)], {type: "application/json"}), .push(function () {
true return gadget._put(
).then(function (doc) { id + "/" + param,
if (doc !== null) { blob,
command.success({"data": doc}); true
} else {
command.error(
"not_found",
"missing",
"Cannot find document"
); );
} });
}).fail(function (event) {
command.error(
event.target.status,
event.target.statusText,
"Unable to put doc"
);
});
}; };
QiniuStorage.prototype._get = function (key) { QiniuStorage.prototype._get = function (key) {
var download_url = 'http://' + this._bucket + '.u.qiniudn.com/' + key var download_url = 'http://' + this._bucket + '/' + key
// var download_url = 'http://' + this._bucket + '.dn.qbox.me/' + key + '?e=' + DEADLINE,
+ '?e=' + DEADLINE, token = b64_hmac_sha1(this._secret_key, download_url),
token = b64_hmac_sha1(this._secret_key, download_url); gadget = this;
return jIO.util.ajax({ return new RSVP.Queue()
"type": "GET", .push(function () {
"url": download_url + "&token=" + this._access_key + ':' + token return jIO.util.ajax({
// "dataType": "blob" type: "GET",
}); url: metadata_template.expand({
}; bucket: gadget._bucket,
key: key,
/** DEADLINE: DEADLINE,
* Get a document or attachment access_key: gadget._access_key,
* @method get token: token
* @param {object} command The JIO command })
**/ });
QiniuStorage.prototype.get = function (command, param) {
return this._get(param._id)
.then(function (doc) {
if (doc.target.responseText !== undefined) {
command.success({"data": JSON.parse(doc.target.responseText)});
} else {
command.error(
"not_found",
"missing",
"Cannot find document"
);
}
}).fail(function (event) {
command.error(
event.target.status,
event.target.statusText,
"Unable to get doc"
);
}); });
}; };
/** /**
* Get an attachment * Get an attaURITemplatechment
* *
* @method getAttachment * @method getAttachment
* @param {Object} command The JIO command
* @param {Object} param The given parameters * @param {Object} param The given parameters
* @param {Object} options The command options * @param {Object} attachment attachment name
*/ */
QiniuStorage.prototype.getAttachment = function (command, param) { QiniuStorage.prototype.getAttachment = function (param, attachment) {
return this._get(param._id + "/" + param._attachment) var gadget = this;
.then(function (doc) {
if (doc.target.response) { return new RSVP.Queue()
command.success({"data": doc.target.response}); .push(function () {
} else { return gadget._get(param + "/" + attachment);
// XXX Handle error })
command.error( .push(function (doc) {
"not_found", if (doc.target.response !== undefined) {
"missing", return new Blob([doc.target.response]);
"Cannot find document"
);
} }
}).fail(function (event) { if (doc.target !== undefined) {
command.error( doc.id = param;
event.target.status, doc.target.attachment = attachment;
event.target.statusText, return new Blob([JSON.stringify(doc)], {type: "application/json"});
"Unable to get attachment" }
);
}).push(undefined, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find attachment: "
+ param._id + " , " + param._attachment,
404);
}
throw error;
}); });
}; };
/** /**
* Add an attachment to a document * Add an attachment to a document
* *
* @method putAttachment * @method putAttachment
* @param {Object} command The JIO command * @id {Object} Document id
* @param {Object} param The given parameters * @param {Object} param The given parameters
* @param {Object} options The command options * @blob {Object} attachment packaged into a blob
*/ */
QiniuStorage.prototype.putAttachment = function (command, param) { QiniuStorage.prototype.putAttachment = function (id, param, blob) {
return this._put( var gadget = this;
param._id + "/" + param._attachment, return new RSVP.Queue()
param._blob, .push(function () {
true return gadget._put(
).then(function (doc) { id + "/" + param,
if (doc !== null) { blob,
command.success({"data": doc}); true
} else {
command.error(
"not_found",
"missing",
"Cannot find document"
); );
} });
}).fail(function (event) {
command.error(
event.target.status,
event.target.statusText,
"Unable to put attachment"
);
});
};
/**
* Remove a document
*
* @method remove
* @param {Object} command The JIO command
* @param {Object} param The given parameters
*/
QiniuStorage.prototype.remove = function (command, param) {
var DELETE_HOST = "http://rs.qiniu.com",
DELETE_PREFIX = "/delete/",
encoded_entry_uri = urlsafe_base64_encode(btoa(
this._bucket + ':' + param._id
)),
delete_url = DELETE_HOST + DELETE_PREFIX + encoded_entry_uri,
data = DELETE_PREFIX + encoded_entry_uri + '\n',
token = b64_hmac_sha1(this._secret_key, data);
jIO.util.ajax({
"type": "POST",
"url": delete_url,
"headers": {
Authorization: "QBox " + this._access_key + ':' + token,
"Content-Type": 'application/x-www-form-urlencoded'
}
}).then(
command.success
).fail(function (error) {
command.error(
"not_found",
"missing",
"Unable to delete doc"
);
});
};
QiniuStorage.prototype.allDocs = function (command, param, options) {
var LIST_HOST = "http://rsf.qiniu.com",
LIST_PREFIX = "/list?bucket=" + this._bucket,
list_url = LIST_HOST + LIST_PREFIX,
token = b64_hmac_sha1(this._secret_key, LIST_PREFIX + '\n');
jIO.util.ajax({
"type": "POST",
"url": list_url,
"headers": {
Authorization: "QBox " + this._access_key + ':' + token,
"Content-Type": 'application/x-www-form-urlencoded'
}
}).then(function (response) {
var data = JSON.parse(response.target.responseText),
count = data.items.length,
result = [],
item,
i;
for (i = 0; i < count; i += 1) {
item = data.items[i];
result.push({
id: item.key,
key: item.key,
doc: {},
value: {}
});
}
command.success({"data": {"rows": result, "total_rows": count}});
}).fail(function (error) {
command.error(
"error",
"did not work as expected",
"Unable to call allDocs"
);
});
}; };
jIO.addStorage('qiniu', QiniuStorage); jIO.addStorage('qiniu', QiniuStorage);
})); }(jIO, RSVP, Blob, UriTemplate));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment