|
@@ -1,52 +1,58 @@
|
|
var util = require("thinkjs-util");
|
|
var util = require("thinkjs-util");
|
|
-var request = require("request");
|
|
|
|
|
|
+var request = require("requestretry");
|
|
var fs = require("fs");
|
|
var fs = require("fs");
|
|
var path = require("path");
|
|
var path = require("path");
|
|
|
|
|
|
module.exports = util.Class(function () {
|
|
module.exports = util.Class(function () {
|
|
return {
|
|
return {
|
|
- /**
|
|
|
|
- * 接口的域名
|
|
|
|
- * @type {String}
|
|
|
|
- */
|
|
|
|
- domain: "v0.api.upyun.com",
|
|
|
|
- /**
|
|
|
|
- * 初始化
|
|
|
|
- * @param {[type]} bucketname [description]
|
|
|
|
- * @param {[type]} username [description]
|
|
|
|
- * @param {[type]} password [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 默认接口域名
|
|
|
|
+ * @type {String}
|
|
|
|
+ */
|
|
|
|
+ DEFAULT_DOMAIN: "v0.api.upyun.com",
|
|
|
|
+ /**
|
|
|
|
+ * 接口的域名
|
|
|
|
+ * @type {String}
|
|
|
|
+ */
|
|
|
|
+ domain: this.DEFAULT_DOMAIN,
|
|
|
|
+ /**
|
|
|
|
+ * 初始化
|
|
|
|
+ * @param {[type]} bucketname [description]
|
|
|
|
+ * @param {[type]} username [description]
|
|
|
|
+ * @param {[type]} password [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
init: function (bucketname, username, password) {
|
|
init: function (bucketname, username, password) {
|
|
this.bucketname = bucketname;
|
|
this.bucketname = bucketname;
|
|
this.username = username;
|
|
this.username = username;
|
|
this.password = util.md5(password);
|
|
this.password = util.md5(password);
|
|
|
|
+ this.domain = this.DEFAULT_DOMAIN;
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 设置接口的domain
|
|
|
|
- * @param {[type]} domain [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设置接口的domain
|
|
|
|
+ * @param {[type]} domain [description]
|
|
|
|
+ */
|
|
setDomain: function (domain) {
|
|
setDomain: function (domain) {
|
|
this.domain = domain;
|
|
this.domain = domain;
|
|
return this;
|
|
return this;
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 签名
|
|
|
|
- * @param {[type]} method [description]
|
|
|
|
- * @param {[type]} uri [description]
|
|
|
|
- * @param {[type]} date [description]
|
|
|
|
- * @param {[type]} length [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 签名
|
|
|
|
+ * @param {[type]} method [description]
|
|
|
|
+ * @param {[type]} uri [description]
|
|
|
|
+ * @param {[type]} date [description]
|
|
|
|
+ * @param {[type]} length [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
sign: function (method, uri, date, length) {
|
|
sign: function (method, uri, date, length) {
|
|
var sign = method + '&' + uri + '&' + date + '&' + length + '&' + this.password;
|
|
var sign = method + '&' + uri + '&' + date + '&' + length + '&' + this.password;
|
|
return 'UpYun ' + this.username + ':' + util.md5(sign);
|
|
return 'UpYun ' + this.username + ':' + util.md5(sign);
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 获取文件或者文件夹的信息
|
|
|
|
- * @param {[type]} file [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取文件或者文件夹的信息
|
|
|
|
+ * @param {[type]} file [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
getInfo: function (file) {
|
|
getInfo: function (file) {
|
|
return this.request(file, 'HEAD').then(function (response) {
|
|
return this.request(file, 'HEAD').then(function (response) {
|
|
var headers = response.headers;
|
|
var headers = response.headers;
|
|
@@ -57,21 +63,21 @@ module.exports = util.Class(function () {
|
|
};
|
|
};
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 查看空间占用信息
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 查看空间占用信息
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
getUsage: function (path) {
|
|
getUsage: function (path) {
|
|
path = path || "/";
|
|
path = path || "/";
|
|
return this.request(path + "?usage").then(function (response) {
|
|
return this.request(path + "?usage").then(function (response) {
|
|
return parseInt(response.body, 10);
|
|
return parseInt(response.body, 10);
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 从返回的headers里获取图片的信息
|
|
|
|
- * @param {[type]} response [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 从返回的headers里获取图片的信息
|
|
|
|
+ * @param {[type]} response [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
getPicInfo: function (response) {
|
|
getPicInfo: function (response) {
|
|
var headers = response.headers;
|
|
var headers = response.headers;
|
|
return {
|
|
return {
|
|
@@ -81,12 +87,12 @@ module.exports = util.Class(function () {
|
|
type: headers['x-upyun-file-type']
|
|
type: headers['x-upyun-file-type']
|
|
};
|
|
};
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 上传文件或者文件夹
|
|
|
|
- * @param {[type]} savePath [description]
|
|
|
|
- * @param {[type]} filePath [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 上传文件或者文件夹
|
|
|
|
+ * @param {[type]} savePath [description]
|
|
|
|
+ * @param {[type]} filePath [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
upload: function (savePath, filePath, headers) {
|
|
upload: function (savePath, filePath, headers) {
|
|
var defaultHeaders = {
|
|
var defaultHeaders = {
|
|
mkdir: true
|
|
mkdir: true
|
|
@@ -142,12 +148,12 @@ module.exports = util.Class(function () {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 文件或者文件夹下载
|
|
|
|
- * @param {[type]} path [description]
|
|
|
|
- * @param {[type]} savePath [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 文件或者文件夹下载
|
|
|
|
+ * @param {[type]} path [description]
|
|
|
|
+ * @param {[type]} savePath [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
download: function (sourcePath, savePath, typeData) {
|
|
download: function (sourcePath, savePath, typeData) {
|
|
sourcePath = sourcePath || "/";
|
|
sourcePath = sourcePath || "/";
|
|
//if (savePath && savePath.slice(-1) !== "/") {
|
|
//if (savePath && savePath.slice(-1) !== "/") {
|
|
@@ -201,12 +207,12 @@ module.exports = util.Class(function () {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 删除文件或者文件夹
|
|
|
|
- * @param {[type]} path [description]
|
|
|
|
- * @param {[type]} force [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 删除文件或者文件夹
|
|
|
|
+ * @param {[type]} path [description]
|
|
|
|
+ * @param {[type]} force [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
rm: function (path, force) {
|
|
rm: function (path, force) {
|
|
if (!path) {
|
|
if (!path) {
|
|
return util.getPromise(new Error("path can't empty"), true);
|
|
return util.getPromise(new Error("path can't empty"), true);
|
|
@@ -248,11 +254,11 @@ module.exports = util.Class(function () {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 递归创建目录
|
|
|
|
- * @param {[type]} path [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 递归创建目录
|
|
|
|
+ * @param {[type]} path [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
mkDir: function (path) {
|
|
mkDir: function (path) {
|
|
return this.request(path, 'PUT', '', {
|
|
return this.request(path, 'PUT', '', {
|
|
mkdir: true,
|
|
mkdir: true,
|
|
@@ -261,11 +267,11 @@ module.exports = util.Class(function () {
|
|
return response.body;
|
|
return response.body;
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 读取目录下的文件和子目录
|
|
|
|
- * @param {[type]} dir [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 读取目录下的文件和子目录
|
|
|
|
+ * @param {[type]} dir [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
readDir: function (path, recursive) {
|
|
readDir: function (path, recursive) {
|
|
path = path || "/";
|
|
path = path || "/";
|
|
if (path.slice(-1) !== '/') {
|
|
if (path.slice(-1) !== '/') {
|
|
@@ -302,15 +308,15 @@ module.exports = util.Class(function () {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- /**
|
|
|
|
- * 请求数据
|
|
|
|
- * @param {[type]} uri [description]
|
|
|
|
- * @param {[type]} method [description]
|
|
|
|
- * @param {[type]} data [description]
|
|
|
|
- * @param {[type]} headers [description]
|
|
|
|
- * @param {[type]} options [description]
|
|
|
|
- * @return {[type]} [description]
|
|
|
|
- */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 请求数据
|
|
|
|
+ * @param {[type]} uri [description]
|
|
|
|
+ * @param {[type]} method [description]
|
|
|
|
+ * @param {[type]} data [description]
|
|
|
|
+ * @param {[type]} headers [description]
|
|
|
|
+ * @param {[type]} options [description]
|
|
|
|
+ * @return {[type]} [description]
|
|
|
|
+ */
|
|
request: function (uri, method, data, headers, options) {
|
|
request: function (uri, method, data, headers, options) {
|
|
uri = "/" + this.bucketname + uri;
|
|
uri = "/" + this.bucketname + uri;
|
|
method = method || "GET";
|
|
method = method || "GET";
|
|
@@ -327,15 +333,28 @@ module.exports = util.Class(function () {
|
|
'Authorization': Authorization
|
|
'Authorization': Authorization
|
|
});
|
|
});
|
|
var deferred = util.getDefer();
|
|
var deferred = util.getDefer();
|
|
|
|
+ var url = '';
|
|
|
|
+ if (this.domain == this.DEFAULT_DOMAIN) {
|
|
|
|
+ url = "https://" + this.domain + uri;
|
|
|
|
+ } else {
|
|
|
|
+ url = "http://" + this.domain + uri;
|
|
|
|
+ }
|
|
var opts = util.extend({
|
|
var opts = util.extend({
|
|
- url: "http://" + this.domain + uri,
|
|
|
|
|
|
+ url: url,
|
|
method: method,
|
|
method: method,
|
|
body: data || "",
|
|
body: data || "",
|
|
- headers: headers
|
|
|
|
|
|
+ timeout: 10000,
|
|
|
|
+ headers: headers,
|
|
|
|
+ maxAttempts: 5,
|
|
|
|
+ retryDelay: 1000,
|
|
|
|
+ retryStrategy: request.RetryStrategies.HTTPOrNetworkError
|
|
}, options);
|
|
}, options);
|
|
request(opts, function (error, response, body) {
|
|
request(opts, function (error, response, body) {
|
|
- if (error || response.statusCode !== 200) {
|
|
|
|
- deferred.reject(error || "statusCode: " + response.statusCode + "; body: " + body);
|
|
|
|
|
|
+ if (error || response == undefined || response.statusCode !== 200) {
|
|
|
|
+ deferred.reject({
|
|
|
|
+ code: (response && response.statusCode) ? response.statusCode : -1,
|
|
|
|
+ message: error || "statusCode: " + response.statusCode + "; body: " + body
|
|
|
|
+ });
|
|
} else {
|
|
} else {
|
|
deferred.resolve(response);
|
|
deferred.resolve(response);
|
|
}
|
|
}
|
|
@@ -343,4 +362,4 @@ module.exports = util.Class(function () {
|
|
return deferred.promise;
|
|
return deferred.promise;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
-});
|
|
|
|
|
|
+});
|