|
@@ -3,7 +3,7 @@ var request = require("requestretry");
|
|
|
var fs = require("fs");
|
|
|
var path = require("path");
|
|
|
|
|
|
-module.exports = util.Class(function () {
|
|
|
+module.exports = util.Class(function() {
|
|
|
return {
|
|
|
/**
|
|
|
* 默认接口域名
|
|
@@ -22,7 +22,7 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} password [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- init: function (bucketname, username, password) {
|
|
|
+ init: function(bucketname, username, password) {
|
|
|
this.bucketname = bucketname;
|
|
|
this.username = username;
|
|
|
this.password = util.md5(password);
|
|
@@ -32,7 +32,7 @@ module.exports = util.Class(function () {
|
|
|
* 设置接口的domain
|
|
|
* @param {[type]} domain [description]
|
|
|
*/
|
|
|
- setDomain: function (domain) {
|
|
|
+ setDomain: function(domain) {
|
|
|
this.domain = domain;
|
|
|
return this;
|
|
|
},
|
|
@@ -44,7 +44,7 @@ module.exports = util.Class(function () {
|
|
|
* @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;
|
|
|
return 'UpYun ' + this.username + ':' + util.md5(sign);
|
|
|
},
|
|
@@ -53,8 +53,8 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} file [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- getInfo: function (file) {
|
|
|
- return this.request(file, 'HEAD').then(function (response) {
|
|
|
+ getInfo: function(file) {
|
|
|
+ return this.request(file, 'HEAD').then(function(response) {
|
|
|
var headers = response.headers;
|
|
|
return {
|
|
|
type: headers['x-upyun-file-type'],
|
|
@@ -67,9 +67,9 @@ module.exports = util.Class(function () {
|
|
|
* 查看空间占用信息
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- getUsage: function (path) {
|
|
|
+ getUsage: function(path) {
|
|
|
path = path || "/";
|
|
|
- return this.request(path + "?usage").then(function (response) {
|
|
|
+ return this.request(path + "?usage").then(function(response) {
|
|
|
return parseInt(response.body, 10);
|
|
|
});
|
|
|
},
|
|
@@ -78,7 +78,7 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} response [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- getPicInfo: function (response) {
|
|
|
+ getPicInfo: function(response) {
|
|
|
var headers = response.headers;
|
|
|
return {
|
|
|
width: headers['x-upyun-width'],
|
|
@@ -93,7 +93,7 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} filePath [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- upload: function (savePath, filePath, headers) {
|
|
|
+ upload: function(savePath, filePath, headers) {
|
|
|
var defaultHeaders = {
|
|
|
mkdir: true
|
|
|
};
|
|
@@ -112,9 +112,9 @@ module.exports = util.Class(function () {
|
|
|
filename = filename[filename.length - 1];
|
|
|
savePath += '/' + filename;
|
|
|
}
|
|
|
- return this.request(savePath, 'PUT', stream, defaultHeaders).then(function (response) {
|
|
|
+ return this.request(savePath, 'PUT', stream, defaultHeaders).then(function(response) {
|
|
|
return self.getPicInfo(response);
|
|
|
- }).then(function (data) {
|
|
|
+ }).then(function(data) {
|
|
|
if (filename) {
|
|
|
data.filename = filename;
|
|
|
}
|
|
@@ -129,7 +129,7 @@ module.exports = util.Class(function () {
|
|
|
}
|
|
|
var promises = [];
|
|
|
var files = fs.readdirSync(filePath);
|
|
|
- files.forEach(function (item) {
|
|
|
+ files.forEach(function(item) {
|
|
|
var nFilePath = filePath + item;
|
|
|
var state = fs.statSync(nFilePath);
|
|
|
if (state.isFile() || state.isDirectory()) {
|
|
@@ -143,7 +143,7 @@ module.exports = util.Class(function () {
|
|
|
return self.mkDir(savePath);
|
|
|
}
|
|
|
} else { //普通内容上传
|
|
|
- return this.request(savePath, 'PUT', filePath, defaultHeaders).then(function (response) {
|
|
|
+ return this.request(savePath, 'PUT', filePath, defaultHeaders).then(function(response) {
|
|
|
return self.getPicInfo(response);
|
|
|
});
|
|
|
}
|
|
@@ -154,21 +154,21 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} savePath [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- download: function (sourcePath, savePath, typeData) {
|
|
|
+ download: function(sourcePath, savePath, typeData) {
|
|
|
sourcePath = sourcePath || "/";
|
|
|
//if (savePath && savePath.slice(-1) !== "/") {
|
|
|
// savePath += "/";
|
|
|
//}
|
|
|
var self = this;
|
|
|
var promise = typeData ? util.getPromise(typeData) : this.getInfo(sourcePath);
|
|
|
- return promise.then(function (data) {
|
|
|
+ return promise.then(function(data) {
|
|
|
if (data.type === 'folder') {
|
|
|
if (sourcePath.slice(-1) !== "/") {
|
|
|
sourcePath += "/";
|
|
|
}
|
|
|
- return self.readDir(sourcePath).then(function (data) {
|
|
|
+ return self.readDir(sourcePath).then(function(data) {
|
|
|
var promises = [];
|
|
|
- data.forEach(function (item) {
|
|
|
+ data.forEach(function(item) {
|
|
|
var nPath = sourcePath + item.name;
|
|
|
var promise;
|
|
|
//文件夹
|
|
@@ -189,7 +189,7 @@ module.exports = util.Class(function () {
|
|
|
//单个文件
|
|
|
return self.request(sourcePath, 'GET', '', {}, {
|
|
|
encoding: null
|
|
|
- }).then(function (response) {
|
|
|
+ }).then(function(response) {
|
|
|
if (!savePath) {
|
|
|
return response.body;
|
|
|
}
|
|
@@ -213,7 +213,7 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} force [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- rm: function (path, force) {
|
|
|
+ rm: function(path, force) {
|
|
|
if (!path) {
|
|
|
return util.getPromise(new Error("path can't empty"), true);
|
|
|
}
|
|
@@ -221,16 +221,16 @@ module.exports = util.Class(function () {
|
|
|
path += '/';
|
|
|
}
|
|
|
var self = this;
|
|
|
- return this.getInfo(path).then(function (data) {
|
|
|
+ return this.getInfo(path).then(function(data) {
|
|
|
if (data.type === 'folder') {
|
|
|
if (!force) {
|
|
|
- return self.request(path, 'DELETE').then(function (response) {
|
|
|
+ return self.request(path, 'DELETE').then(function(response) {
|
|
|
return response.body;
|
|
|
});
|
|
|
}
|
|
|
- return self.readDir(path).then(function (data) {
|
|
|
+ return self.readDir(path).then(function(data) {
|
|
|
var promises = [];
|
|
|
- data.forEach(function (item) {
|
|
|
+ data.forEach(function(item) {
|
|
|
var nPath = path + item.name;
|
|
|
var promise;
|
|
|
//文件夹
|
|
@@ -244,11 +244,11 @@ module.exports = util.Class(function () {
|
|
|
if (promises.length) {
|
|
|
return util.Promise.all(promises);
|
|
|
}
|
|
|
- }).then(function () {
|
|
|
+ }).then(function() {
|
|
|
return self.rm(path, false);
|
|
|
});
|
|
|
} else {
|
|
|
- return self.request(path, 'DELETE').then(function (response) {
|
|
|
+ return self.request(path, 'DELETE').then(function(response) {
|
|
|
return response.body;
|
|
|
});
|
|
|
}
|
|
@@ -259,11 +259,11 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} path [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- mkDir: function (path) {
|
|
|
+ mkDir: function(path) {
|
|
|
return this.request(path, 'PUT', '', {
|
|
|
mkdir: true,
|
|
|
folder: true
|
|
|
- }).then(function (response) {
|
|
|
+ }).then(function(response) {
|
|
|
return response.body;
|
|
|
});
|
|
|
},
|
|
@@ -272,13 +272,13 @@ module.exports = util.Class(function () {
|
|
|
* @param {[type]} dir [description]
|
|
|
* @return {[type]} [description]
|
|
|
*/
|
|
|
- readDir: function (path, recursive) {
|
|
|
+ readDir: function(path, recursive) {
|
|
|
path = path || "/";
|
|
|
if (path.slice(-1) !== '/') {
|
|
|
path += '/';
|
|
|
}
|
|
|
var self = this;
|
|
|
- return this.request(path, "GET").then(function (response) {
|
|
|
+ return this.request(path, "GET").then(function(response) {
|
|
|
var dirs = response.body.split("\n");
|
|
|
var result = [];
|
|
|
var promises = [];
|
|
@@ -292,7 +292,7 @@ module.exports = util.Class(function () {
|
|
|
time: attrs[3]
|
|
|
};
|
|
|
if (recursive && dir.type === 'F') {
|
|
|
- var promise = self.readDir(path + dir.name, true).then(function (data) {
|
|
|
+ var promise = self.readDir(path + dir.name, true).then(function(data) {
|
|
|
dir.children = data;
|
|
|
});
|
|
|
promises.push(promise);
|
|
@@ -300,7 +300,7 @@ module.exports = util.Class(function () {
|
|
|
result.push(dir);
|
|
|
}
|
|
|
if (promises.length) {
|
|
|
- return util.Promise.all(promises).then(function () {
|
|
|
+ return util.Promise.all(promises).then(function() {
|
|
|
return result;
|
|
|
});
|
|
|
} else {
|
|
@@ -317,7 +317,7 @@ module.exports = util.Class(function () {
|
|
|
* @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;
|
|
|
method = method || "GET";
|
|
|
headers = headers || {};
|
|
@@ -343,13 +343,13 @@ module.exports = util.Class(function () {
|
|
|
url: url,
|
|
|
method: method,
|
|
|
body: data || "",
|
|
|
- timeout: 10000,
|
|
|
+ timeout: 120000,
|
|
|
headers: headers,
|
|
|
maxAttempts: 5,
|
|
|
retryDelay: 1000,
|
|
|
retryStrategy: request.RetryStrategies.HTTPOrNetworkError
|
|
|
}, options);
|
|
|
- request(opts, function (error, response, body) {
|
|
|
+ request(opts, function(error, response, body) {
|
|
|
if (error || response == undefined || response.statusCode !== 200) {
|
|
|
deferred.reject({
|
|
|
code: (response && response.statusCode) ? response.statusCode : -1,
|