|
@@ -1,51 +1,110 @@
|
1
|
|
-import SparkMD5 from 'spark-md5'
|
|
1
|
+import { getFilemd5sum, getBase64 } from './testutil'
|
2
|
2
|
|
3
|
|
-export function getFilemd5sum(ofile) {
|
4
|
|
- return new Promise((resolve, reject) => {
|
5
|
|
- let file = ofile;
|
6
|
|
- let tmp_md5;
|
7
|
|
- let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
|
8
|
|
- // file = this.files[0],
|
9
|
|
- chunkSize = 8097152, // Read in chunks of 2MB
|
10
|
|
- chunks = Math.ceil(file.size / chunkSize),
|
11
|
|
- currentChunk = 0,
|
12
|
|
- spark = new SparkMD5.ArrayBuffer(),
|
13
|
|
- fileReader = new FileReader();
|
14
|
|
-
|
15
|
|
- fileReader.onload = function (e) {
|
16
|
|
- spark.append(e.target.result); // Append array buffer
|
17
|
|
- currentChunk++;
|
18
|
|
-
|
19
|
|
- if (currentChunk < chunks) {
|
20
|
|
- loadNext();
|
21
|
|
- } else {
|
22
|
|
- tmp_md5 = spark.end();
|
23
|
|
- resolve(tmp_md5)
|
24
|
|
- }
|
25
|
|
- };
|
26
|
|
-
|
27
|
|
- fileReader.onerror = function () {
|
28
|
|
- reject('error');
|
29
|
|
- };
|
30
|
|
-
|
31
|
|
- loadNext = () => {
|
32
|
|
- var start = currentChunk * chunkSize,
|
33
|
|
- end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
|
34
|
|
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
|
35
|
|
- }
|
36
|
|
- loadNext();
|
37
|
|
- })
|
|
3
|
+const IMAGE_MULTIPART = '/multipart/upload/'
|
|
4
|
+const VIDEO_MULTIPART = '/upload/'
|
|
5
|
+
|
|
6
|
+function FileFactory(file) {
|
|
7
|
+ this.offset = 0; //用于断点续传,默认为 0
|
|
8
|
+ this.BYTES_PER_CHUNK = 1024 * 1024
|
|
9
|
+ this.file = file
|
|
10
|
+ this.fileSize = file.size
|
|
11
|
+ this.fileType = file.type
|
|
12
|
+ this.chunkNum = this.BYTES_PER_CHUNK > this.fileSize ? Math.ceil(this.fileSize / this.BYTES_PER_CHUNK):1
|
|
13
|
+ this.chunkSize = this.BYTES_PER_CHUNK > this.fileSize ? this.fileSize:this.BYTES_PER_CHUNK
|
38
|
14
|
}
|
39
|
15
|
|
40
|
|
-export function getBase64(file) {
|
41
|
|
- return new Promise((resolve, reject) => {
|
42
|
|
- var reader = new FileReader();
|
43
|
|
- reader.readAsDataURL(file);
|
44
|
|
- reader.onload = function (e) {
|
45
|
|
- resolve(e.target.result);
|
|
16
|
+export default function UploadSdk(host, origin, token, file) {
|
|
17
|
+
|
|
18
|
+ this.host = host
|
|
19
|
+ this.origin = origin
|
|
20
|
+ this.token = token
|
|
21
|
+ this.file = file
|
|
22
|
+
|
|
23
|
+ this.generateMd5 = function (callback) {
|
|
24
|
+ getFilemd5sum(this.file).then(result => {callback(result)})
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ this.generateBase64 = function (callback) {
|
|
28
|
+ getBase64(this.file).then(result => {callback(result)})
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ this.imageUploadAction = function () {
|
|
32
|
+ return new Promise( (resolve, reject) => {
|
|
33
|
+ this.generateMd5( (md5) => {
|
|
34
|
+ this.md5 = md5
|
|
35
|
+ this.generateBase64( (base64) => {
|
|
36
|
+ this.base64Body = base64
|
|
37
|
+ let fileFactory = new FileFactory(file)
|
|
38
|
+ this.postImage(this.md5,this.base64Body,fileFactory).then( res => {
|
|
39
|
+ resolve(res)
|
|
40
|
+ }).catch( (err) => {
|
|
41
|
+ reject(err)
|
|
42
|
+ })
|
|
43
|
+ })
|
|
44
|
+ })
|
|
45
|
+ })
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ this.videoUploadAction = function () {
|
|
49
|
+ return new Promise( (resolve, reject) => {
|
|
50
|
+ this.postVideo().then( res => {
|
|
51
|
+ resolve(res)
|
|
52
|
+ }).catch( (err) => {
|
|
53
|
+ reject(err)
|
|
54
|
+ })
|
|
55
|
+ })
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ this.postVideo = function() {
|
|
59
|
+ return new Promise( (resolve, reject) => {
|
|
60
|
+ let xhr = new XMLHttpRequest()
|
|
61
|
+ xhr.open("POSt", this.host+VIDEO_MULTIPART+this.origin, true)
|
|
62
|
+ xhr.open("POSt", this.host+IMAGE_MULTIPART+this.origin, true)
|
|
63
|
+ xhr.withCredentials = true;
|
|
64
|
+ xhr.setRequestHeader('Authorization', `Bearer ${this.token}`)
|
|
65
|
+ xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8')
|
|
66
|
+ xhr.setRequestHeader('Accept', 'application/json')
|
|
67
|
+ let fd = new FormData()
|
|
68
|
+ fd.append('file',this.file)
|
|
69
|
+ fd.append('code_rate','ld,sd,hd')
|
|
70
|
+ xhr.send(fd)
|
|
71
|
+ xhr.onreadystatechange = function () {
|
|
72
|
+ if (xhr.readyState === 4) {
|
|
73
|
+ if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
|
|
74
|
+ resolve(xhr.responseText)
|
|
75
|
+ } else {
|
|
76
|
+ reject(xhr.status)
|
|
77
|
+ }
|
46
|
78
|
}
|
47
|
|
- reader.onerror = function() {
|
48
|
|
- reject('error')
|
|
79
|
+ }
|
|
80
|
+ })
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ this.postImage = function(md5,base64,fileFactory) {
|
|
84
|
+ return new Promise((resolve, reject) => {
|
|
85
|
+ let xhr = new XMLHttpRequest()
|
|
86
|
+ xhr.open("POSt", this.host+IMAGE_MULTIPART+this.origin, true)
|
|
87
|
+ xhr.withCredentials = true
|
|
88
|
+ xhr.setRequestHeader('Authorization', `Bearer ${this.token}`)
|
|
89
|
+ xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8')
|
|
90
|
+ xhr.setRequestHeader('Accept', 'application/json')
|
|
91
|
+ xhr.setRequestHeader('LINK_UPLOAD_FILE_SIZE',fileFactory.fileSize)
|
|
92
|
+ xhr.setRequestHeader('LINK_UPLOAD_CHUNK_INDEX',1+'')
|
|
93
|
+ xhr.setRequestHeader('LINK_UPLOAD_CHUNK_NUM',fileFactory.chunkNum)
|
|
94
|
+ xhr.setRequestHeader('LINK_UPLOAD_CHUNK_SIZE',fileFactory.chunkSize)
|
|
95
|
+ xhr.setRequestHeader('LINK_UPLOAD_OFFSET',fileFactory.offset+'')
|
|
96
|
+ xhr.setRequestHeader('LINK_UPLOAD_FILE_MD5',md5)
|
|
97
|
+ xhr.setRequestHeader('LINK_UPLOAD_FILE_TYPE','jepg')
|
|
98
|
+ xhr.send(JSON.stringify({body: base64}))
|
|
99
|
+ xhr.onreadystatechange = function () {
|
|
100
|
+ if (xhr.readyState === 4) {
|
|
101
|
+ if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
|
|
102
|
+ resolve(xhr.responseText)
|
|
103
|
+ } else {
|
|
104
|
+ reject(xhr.status)
|
|
105
|
+ }
|
49
|
106
|
}
|
50
|
|
- });
|
|
107
|
+ }
|
|
108
|
+ })
|
|
109
|
+ }
|
51
|
110
|
}
|