Skip to content

Commit 4a5f2c6

Browse files
author
cenzhende
committed
使用自有接口
1 parent afe1908 commit 4a5f2c6

File tree

5 files changed

+14
-50
lines changed

5 files changed

+14
-50
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Version](https://img.shields.io/npm/v/juejin-file-uploader.svg?style=flat-square)](https://www.npmjs.com/package/juejin-file-uploader)
55
[![License](https://img.shields.io/npm/l/juejin-file-uploader.svg?style=flat-square)](LICENSE)
66

7-
[掘金](https://juejin.im) 浏览器端文件上传插件,基于 [七牛云存储](https://www.qiniu.com/)
7+
[掘金](https://juejin.im) 浏览器端文件上传插件。
88

99
## 安装
1010

@@ -90,7 +90,3 @@ uploadTask.abort('不想上传了')
9090
## 前置需求
9191

9292
- 支持 [Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)
93-
94-
## 相关文档
95-
96-
[直传文件](https://developer.qiniu.com/kodo/api/1312/upload)

dist/juejin-file-uploader.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "juejin-file-uploader",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "juejin file uploader",
55
"main": "dist/juejin-file-uploader.min.js",
66
"scripts": {

src/juejin-file-uploader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import UploadTask from './upload-task'
22

33
const DEFAULT_CONFIG = {
44
bucket: '',
5-
tokenUrl: 'https://cdn-ms.xitu.io/v1/policy/info',
6-
uploadUrl: 'https://up.qbox.me'
5+
uploadUrl: 'https://cdn-ms.xitu.io/v1/upload'
76
}
87

98
export default class JuejinFileUploader {

src/upload-task.js

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,38 @@
11
export default class UploadTask {
22

3-
constructor (file, { bucket, tokenUrl, uploadUrl }) {
3+
constructor (file, { bucket, uploadUrl }) {
44
this.file = file
55
this.bucket = bucket
6-
this.tokenUrl = tokenUrl
76
this.uploadUrl = uploadUrl
8-
9-
this.infoXhr = new XMLHttpRequest()
107
this.uploadXhr = new XMLHttpRequest()
118
this.abortError = null
129
}
1310

1411
start () {
15-
return getUplaodInfo({
16-
filename: this.file.name,
17-
tokenUrl: this.tokenUrl,
12+
return uploadFile({
13+
file: this.file,
1814
bucket: this.bucket,
19-
xhr: this.infoXhr
15+
uploadUrl: this.uploadUrl,
16+
xhr: this.uploadXhr
2017
})
21-
.then(({ token, key, url }) =>
22-
uploadFile({
23-
key,
24-
token,
25-
file: this.file,
26-
uploadUrl: this.uploadUrl,
27-
xhr: this.uploadXhr
28-
})
29-
.then(() => url)
30-
)
18+
.then(res => res.d.url.https)
3119
.catch(error => Promise.reject(this.abortError || error))
3220
}
3321

3422
abort (reason) {
3523
this.abortError = createAbortError(reason)
36-
this.infoXhr.abort()
3724
this.uploadXhr.abort()
3825
}
3926

4027
}
4128

42-
function getUplaodInfo ({ filename, tokenUrl, bucket, xhr }) {
43-
const url = `${tokenUrl}`
44-
+ `?bucket=${encodeURIComponent(bucket)}`
45-
+ `&filename=${encodeURIComponent(filename)}`
46-
return sendRequest({
47-
method: 'GET',
48-
url,
49-
xhr
50-
})
51-
.then(res => res.d)
52-
.then(info => ({
53-
token: info.token,
54-
key: info.key,
55-
url: info.url.https
56-
}))
57-
}
58-
59-
function uploadFile ({ file, key, token, uploadUrl, xhr }) {
29+
function uploadFile ({ file, bucket, uploadUrl, xhr }) {
6030
const form = new FormData()
61-
form.append('token', token)
31+
const queryPrefix = uploadUrl.indexOf('?') === -1 ? '?' : '&'
6232
form.append('file', file)
63-
form.append('key', key)
6433
return sendRequest({
6534
method: 'POST',
66-
url: uploadUrl,
35+
url: `${uploadUrl}${queryPrefix}bucket=${bucket}`,
6736
data: form,
6837
xhr
6938
})

0 commit comments

Comments
 (0)