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.
* http://www.gnu.org/licenses/lgpl.html
*/
......@@ -7,22 +7,20 @@
/**
* JIO Qiniu Storage. Type = "qiniu".
* 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 */
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
// if (typeof exports === 'object') {
// return module(exports, require('jio'));
// }
module(jIO);
}([
'jio'
], function (jIO) {
/*global JSON, FormData, btoa, Blob, CryptoJS, define,
jIO, RSVP, console, UriTemplate */
/*jslint indent: 2, maxlen: 80, nomen: true, bitwise: true */
(function (jIO, RSVP, Blob, UriTemplate) {
"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) {
return string
......@@ -40,6 +38,7 @@
// b2, b3 to b3, b2, b1, b0.
var encodedArray = [],
i,
//change CrytoJS to rusha
encoded = CryptoJS.HmacSHA1(message, secret_key),
encodedString;
for (i = 0; i < 5; i = i + 1) {
......@@ -55,8 +54,6 @@
return urlsafe_base64_encode(btoa(encodedString));
}
var UPLOAD_URL = "http://up.qiniu.com/",
DEADLINE = 2451491200;
/**
* The JIO QiniuStorage extension
......@@ -93,15 +90,16 @@
data = new FormData();
if (update === true) {
put_policy = JSON.stringify({
"scope": this._bucket + ':' + key,
"scope": "bucket" + ':' + key,
"deadline": DEADLINE
});
} else {
put_policy = JSON.stringify({
"scope": this._bucket,
"scope": "bucket",
"deadline": DEADLINE
});
}
encoded = btoa(put_policy);
encode_signed = b64_hmac_sha1(this._secret_key, encoded);
upload_token = this._access_key + ":" + encode_signed + ":" + encoded;
......@@ -110,10 +108,7 @@
data.append("token", upload_token);
data.append(
"file",
// new Blob([JSON.stringify(doc)], {type: "application/json"}),
// new Blob([doc], {type: "application/json"}),
blob,
// new Blob([], {type: "application/octet-stream"}),
key
);
......@@ -125,248 +120,101 @@
};
/**
* Create a document.
/** * Add an attachment to 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
* @param {Object} command The JIO command
* @param {Object} metadata The metadata to store
* @method putAttachment
* @id {Object} Document id
* @param {Object} param The given parameters
* @blob {Object} attachment packaged into a blob
*/
QiniuStorage.prototype.put = function (command, metadata) {
return this._put(
metadata._id,
new Blob([JSON.stringify(metadata)], {type: "application/json"}),
QiniuStorage.prototype.putAttachment = function (id, param, blob) {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget._put(
id + "/" + param,
blob,
true
).then(function (doc) {
if (doc !== null) {
command.success({"data": doc});
} 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) {
var download_url = 'http://' + this._bucket + '.u.qiniudn.com/' + key
// var download_url = 'http://' + this._bucket + '.dn.qbox.me/' + key
var download_url = 'http://' + this._bucket + '/' + key
+ '?e=' + DEADLINE,
token = b64_hmac_sha1(this._secret_key, download_url);
token = b64_hmac_sha1(this._secret_key, download_url),
gadget = this;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": "GET",
"url": download_url + "&token=" + this._access_key + ':' + token
// "dataType": "blob"
type: "GET",
url: metadata_template.expand({
bucket: gadget._bucket,
key: key,
DEADLINE: DEADLINE,
access_key: gadget._access_key,
token: token
})
});
};
/**
* Get a document or attachment
* @method get
* @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
* @param {Object} command The JIO command
* @param {Object} param The given parameters
* @param {Object} options The command options
* @param {Object} attachment attachment name
*/
QiniuStorage.prototype.getAttachment = function (command, param) {
return this._get(param._id + "/" + param._attachment)
.then(function (doc) {
if (doc.target.response) {
command.success({"data": doc.target.response});
} else {
// XXX Handle error
command.error(
"not_found",
"missing",
"Cannot find document"
);
}
}).fail(function (event) {
command.error(
event.target.status,
event.target.statusText,
"Unable to get attachment"
);
QiniuStorage.prototype.getAttachment = function (param, attachment) {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget._get(param + "/" + attachment);
})
.push(function (doc) {
if (doc.target.response !== undefined) {
return new Blob([doc.target.response]);
}
if (doc.target !== undefined) {
doc.id = param;
doc.target.attachment = attachment;
return new Blob([JSON.stringify(doc)], {type: "application/json"});
}
}).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
*
* @method putAttachment
* @param {Object} command The JIO command
* @id {Object} Document id
* @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) {
return this._put(
param._id + "/" + param._attachment,
param._blob,
QiniuStorage.prototype.putAttachment = function (id, param, blob) {
var gadget = this;
return new RSVP.Queue()
.push(function () {
return gadget._put(
id + "/" + param,
blob,
true
).then(function (doc) {
if (doc !== null) {
command.success({"data": doc});
} 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, 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