-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
204 lines (201 loc) · 6.55 KB
/
index.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
201
202
203
204
var qiniu = require('qiniu')
var fs = require('fs')
module.exports = class Qiniu {
constructor(option) {
this.mac = new qiniu.auth.digest.Mac(option.ak, option.sk)
if (option.remote.url) {
if (!option.remote.url.endsWith('/')) {
option.remote.url += '/'
}
}
this.option = option
}
getFiles() {
var options = {
prefix: this.option._DIR || this.option.remote.prefix.default
}
var config = new qiniu.conf.Config()
//config.useHttpsDomain = true;
config.zone = qiniu.zone[this.option.zone]
var bucketManager = new qiniu.rs.BucketManager(this.mac, config)
return new Promise((resolve, reject) => {
bucketManager.listPrefix(this.option.bucket, options, function (
err,
respBody,
respInfo
) {
if (err) {
console.log(err)
throw err
}
if (respInfo.statusCode == 200) {
//如果这个nextMarker不为空,那么还有未列举完毕的文件列表,下次调用listPrefix的时候,
//指定options里面的marker为这个值
var nextMarker = respBody.marker
var commonPrefixes = respBody.commonPrefixes
// console.log(nextMarker);
// console.log(commonPrefixes);
var items = respBody.items
resolve({
items,
bucketManager
})
} else {
console.log(respInfo.statusCode)
console.log(respBody)
reject(respBody)
}
})
})
}
remove() {
this.option._DIR = this.option.remote.prefix.remove
return new Promise((resovle, reject) => this.getFiles(this.option).then((data) => {
if (data.items.length) {
var deleteOperations = [];
data.items.map((item) => {
deleteOperations.push(qiniu.rs.deleteOp(this.option.bucket, item.key))
})
data.bucketManager.batch(deleteOperations, function (err, respBody, respInfo) {
if (err) {
console.log(err);
//throw err;
} else {
// 200 is success, 298 is part success
if (parseInt(respInfo.statusCode / 100) == 2) {
respBody.forEach(function (item, i) {
if (item.code == 200) {
console.log("remove success" + "\t" + item.code + "\t" + data.items[i].key);
} else {
console.log(item.data.error + "\t" + item.code + "\t" + data.items[i].key);
}
});
resovle(true)
} else {
console.log(respInfo.deleteusCode);
console.log(respBody);
reject()
}
}
});
} else {
console.log("There is no file in this removeDir!")
resovle(true)
}
}))
}
prefetch() {
this.option._DIR = this.option.remote.prefix.prefetch
return new Promise((resovle, reject) => this.getFiles(this.option).then(data => {
var urls = []
var cdnManager = new qiniu.cdn.CdnManager(this.mac)
data.items.map((item) => {
console.log(this.option.remote.url + item.key)
urls.push(this.option.remote.url + item.key)
})
cdnManager.prefetchUrls(urls, function (err, respBody, respInfo) {
if (err) {
reject()
throw err
}
// console.log("prefetch "+respInfo.statusCode);
if (respInfo.statusCode == 200) {
var jsonBody = JSON.parse(respBody)
// console.log(jsonBody.code);
console.log('Prefetch ' + jsonBody.error + '\t' + jsonBody.code)
resovle(data.items)
}
})
}))
}
refresh() {
this.option._DIR = this.option.remote.prefix.refresh
return new Promise((resovle, reject) => this.getFiles(this.option).then(data => {
var urls = []
var cdnManager = new qiniu.cdn.CdnManager(this.mac);
data.items.map((item) => {
console.log(this.option.remote.url + item.key)
urls.push(this.option.remote.url + item.key)
})
cdnManager.refreshUrls(urls, function (err, respBody, respInfo) {
if (err) {
reject()
throw err;
}
// console.log("Refresh "+respInfo.statusCode);
if (respInfo.statusCode == 200) {
var jsonBody = JSON.parse(respBody);
// console.log(jsonBody.code);
console.log("Refresh " + jsonBody.error + '\t' + jsonBody.code);
resovle(data.items)
}
})
}))
}
upload() {
var bucket = this.option.bucket
var config = new qiniu.conf.Config()
// 空间对应的机房
config.zone = qiniu.zone[this.option.zone]
function Traversal(path) {
if (path.endsWith('/')) {
path = path.substr(0, path.length - 1)
}
var files = fs.readdirSync(path)
files
.map(function (file) {
var stat = fs.statSync(path + '/' + file)
if (stat.isDirectory()) {
return Traversal(path + '/' + file)
} else {
return path + '/' + file
}
})
.reduce((a, b) => a + ',' + b);
var dd = files.toString().split(',');
return dd;
}
// 文件上传
return Promise.all(
Traversal(this.option.upload.dir).filter(l => !l.match(this.option.upload.except)).map(localFile => {
var key = (this.option.upload.prefix ? this.option.upload.prefix : "") +
(localFile[0] == '.' ?
localFile
.split('/')
.filter((x, i) => i > 0)
.join('/') :
localFile)
var options = {
scope: bucket + ":" + key
}
var putPolicy = new qiniu.rs.PutPolicy(options)
var uploadToken = putPolicy.uploadToken(this.mac)
var formUploader = new qiniu.form_up.FormUploader(config)
var putExtra = new qiniu.form_up.PutExtra()
localFile = this.option.upload.dir +"\\"+ localFile;
return new Promise((resolve, reject) =>
formUploader.putFile(
uploadToken,
key,
localFile,
putExtra,
function (respErr, respBody, respInfo) {
if (respErr) {
throw respErr
}
if (respInfo.statusCode == 200) {
console.log("upload success" + "\t" + respInfo.statusCode + "\t" + localFile + "\t" + respInfo.data.key)
resolve({
local: localFile,
remote: respInfo.data.key
})
} else {
reject(respBody.error)
}
}
)
)
})
)
}
}