Skip to content

Commit f9d5255

Browse files
committed
add qiniu
1 parent a7eab1d commit f9d5255

File tree

4 files changed

+343
-262
lines changed

4 files changed

+343
-262
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ let postcss = require('postcss')
44
let OSS = require('ali-oss')
55
let path = require('path')
66
let md5File = require('md5-file')
7+
let qiniuUpload = require('./qiniu')
78

8-
function upload (remotePath, localFile, client) {
9+
function aliossUpload (remotePath, localFile, client) {
910
return new Promise((resolve, reject) => {
1011
try {
1112
client
@@ -35,7 +36,11 @@ module.exports = postcss.plugin('postcss-assets-urls', opts => {
3536
decl.value = decl.value.replace(/\/assets\/\S*.(png|svg|jpg)/, match => {
3637
let file = path.join(process.cwd(), match)
3738
let hash = md5File.sync(file, match)
38-
client && uploadList.push(upload(hash, file, client))
39+
let upload = aliossUpload(hash, file, client)
40+
if (opts.qiniu) {
41+
upload = qiniuUpload(opts.qiniu, file)
42+
}
43+
client && uploadList.push(upload)
3944
return [opts.baseUrl || '', hash].join('/')
4045
})
4146
})

qiniu.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
let qiniu = require('qiniu')
2-
let md5File = require('md5-file')
32

4-
5-
function qiniuUpload ({ accessKey, secretKey, hash, bucket, localFile, zone }) {
3+
function qiniuUpload (
4+
{ accessKey, secretKey, hash, bucket, zone, domain },
5+
localFile
6+
) {
67
let mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
78

89
let options = {
@@ -22,24 +23,26 @@ function qiniuUpload ({ accessKey, secretKey, hash, bucket, localFile, zone }) {
2223
}
2324

2425
// file
25-
resumeUploader.putFile(
26-
uploadToken,
27-
null,
28-
localFile,
29-
putExtra,
30-
(respErr, respBody, respInfo) => {
31-
if (respErr) {
32-
throw respErr
33-
}
26+
return new Promise((resolve, reject) => {
27+
resumeUploader.putFile(
28+
uploadToken,
29+
null,
30+
localFile,
31+
putExtra,
32+
(respErr, respBody, respInfo) => {
33+
if (respErr) {
34+
throw respErr
35+
}
3436

35-
if (respInfo.statusCode === 200) {
36-
console.log(respBody)
37-
} else {
38-
console.log(respInfo.statusCode)
39-
console.log(respBody)
37+
if (respInfo.statusCode === 200) {
38+
resolve(domain + '/' + respInfo.data.hash)
39+
} else {
40+
console.log(respInfo.statusCode)
41+
reject(respBody)
42+
}
4043
}
41-
}
42-
)
44+
)
45+
})
4346
}
4447

45-
console.log(md5File.sync('/Volumes/code/github.com/jerexyz/postcss-assets-urls/index.js'))
48+
module.exports = qiniuUpload

qiniu.test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)