-
Notifications
You must be signed in to change notification settings - Fork 0
/
promiseHelper.js
200 lines (129 loc) · 3.55 KB
/
promiseHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*jshint node: true, bitwise:true, curly:true, forin:true, noarg:true, noempty:true, nonew:true, undef:true, strict:true, browser:true, node:true, asi:false, es5: true, evil: true, nomen: true */
'use strict';
/**
* 创造优雅的代码,享受开发的乐趣。
* 网站: swiftcafe.io
* 微信公众号: swift-cafe
*/
// 引入需要的库
var request = require("request");
var fs = require("fs");
var path = require("path");
var PromiseKit = require("promise");
/**
* Promise 工具类
* @constructor
*/
function PromiseHelper() {
}
/**
* 请求 URL 数据
* @param {string} 请求 URL
* @return {promise} Promise 对象
*/
PromiseHelper.prototype.requestAPI = function (url) {
return new PromiseKit(function(fullfill, reject) {
request(url, function(error, response, body){
if(!error && response.statusCode == 200) {
fullfill(body);
} else {
reject(error);
}
});
});
};
/**
* 请求数据并解析 JSON
* @param {[string]} 请求 URL
* @return {[promise]} Promise 对象
*/
PromiseHelper.prototype.requestJSON = function (url) {
return this.requestAPI(url).then(JSON.parse);
};
/**
* @param {jsonObject} JSON 对象
* @param {[string]} 图片保存路径
* @param {[string]} 图片 key
* @return {[promose]} Promise 对象
*/
PromiseHelper.prototype.downloadImage = function(item, imageFolder, funcImgURL, funcImgName, funcSetImgURL) {
return new PromiseKit(function(fullfill, reject) {
if(funcImgURL(item) !== null) {
// var ext = path.extname(item[imageKey]);
// var assetFolder;
// if(imageKey === "imgURL") {
// assetFolder = "videosImage";
// fileName = item.videoId + ext;
// } else {
// assetFolder = "newsImage";
// fileName = path.basename(item[imageKey]);
// }
var fileName = funcImgName(item);
var filePath = path.join(imageFolder, fileName);
console.log("download image " + funcImgURL(item));
request(funcImgURL(item)).pipe(fs.createWriteStream(filePath)).on("finish", function(){
console.log("download finished " + funcImgURL(item));
if(funcSetImgURL) {
funcSetImgURL(item);
}
// item[imageKey] = "assets:\/\/" + assetFolder + "\/" + fileName;
fullfill();
}).on("error", function(){
console.log("download error");
reject();
});
} else {
reject();
}
});
};
/**
* 读取指定目录下的所有文件
* @param {[string]} 文件所在目录的路径
* @return {[promise]} Promise 对象
*/
PromiseHelper.prototype.readDir = function(path) {
return new PromiseKit(function(fullfill, reject) {
fs.readdir(path, function(err, files){
if(err) {
reject(err);
} else {
fullfill(files);
}
});
});
};
/**
* 删除文件
* @param {[string]} 文件路径
* @return {[promise]} Promise 对象
*/
PromiseHelper.prototype.unlinkFile = function(filePath) {
return new PromiseKit(function(fullfill, reject) {
fs.unlink(filePath, function(err){
if(err) {
reject(err);
}else {
fullfill();
}
});
});
};
/**
* 清除图片目录
* @param {[string]} 要删除的目录路径
* @return {[promise]} Promise 对象
*/
PromiseHelper.prototype.clearFolder = function(folderPath) {
var that = this;
return new PromiseKit(function(fullfill, reject) {
that.readDir(folderPath).done(function(files) {
PromiseKit.all(files.map(function(fileName){
return that.unlinkFile(path.join(folderPath, fileName));
})).then(function(){
fullfill();
});
}, reject);
});
};
module.exports = PromiseHelper;