ChrisFuck vor 5 Jahren
Ursprung
Commit
abbdd767f6
3 geänderte Dateien mit 194 neuen und 161 gelöschten Zeilen
  1. 90
    0
      index.js
  2. 0
    116
      index.ts
  3. 104
    45
      utils.js

+ 90
- 0
index.js Datei anzeigen

@@ -0,0 +1,90 @@
1
+import { getFilemd5sum, getBase64 } from './testutil'
2
+
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
14
+}
15
+
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
+  this.fileFactory = new FileFactory(file)
23
+
24
+  this.generateMd5 = function (callback) {
25
+    getFilemd5sum(this.file).then(result => {callback(result)})
26
+  }
27
+
28
+  this.generateBase64 = function (callback) {
29
+    getBase64(this.file).then(result => {callback(result)})
30
+  }
31
+  //上传
32
+  this.action = function() {
33
+    this.generateMd5( (md5) => {
34
+      this.md5 = md5
35
+      this.generateBase64( (base64) => {
36
+        this.base64Body = base64
37
+        this.post(this.md5,this.base64Body,this.fileFactory)
38
+      })
39
+    })
40
+  }
41
+  //成功返回函数
42
+  this.onSuccess = function () {
43
+    return 'success'
44
+  }
45
+  //续传
46
+  this.onContinue = function() {
47
+    return 90
48
+  }
49
+  //错误
50
+  this.onError = function() {
51
+    return 200
52
+  }
53
+
54
+  this.post = function(md5,base64,fileFactory) {
55
+    return new Promise((resolve, reject) => {
56
+      let xhr = new XMLHttpRequest()
57
+      xhr.open("POSt", this.host+IMAGE_MULTIPART+this.origin, true)
58
+        xhr.withCredentials = true;
59
+        xhr.setRequestHeader('Authorization', `Bearer ${this.token}`);
60
+        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
61
+        xhr.setRequestHeader('Accept', 'application/json');
62
+        xhr.setRequestHeader('LINK_UPLOAD_FILE_SIZE',fileFactory.fileSize)
63
+        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_INDEX',1+'')
64
+        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_NUM',fileFactory.chunkNum)
65
+        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_SIZE',fileFactory.chunkSize)
66
+        xhr.setRequestHeader('LINK_UPLOAD_OFFSET',fileFactory.offset+'')
67
+        xhr.setRequestHeader('LINK_UPLOAD_FILE_MD5',md5)
68
+        xhr.setRequestHeader('LINK_UPLOAD_FILE_TYPE','jepg')
69
+        xhr.send(JSON.stringify({body: base64}))
70
+        xhr.onreadystatechange = function () {
71
+          if (xhr.readyState === 4) {
72
+            if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
73
+              resolve(xhr.responseText)
74
+            } else {
75
+              reject(xhr.status)
76
+            }
77
+          }
78
+        }
79
+    })
80
+  }
81
+}
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+

+ 0
- 116
index.ts Datei anzeigen

@@ -1,116 +0,0 @@
1
-import { getFilemd5sum, getBase64 } from './utils.js'
2
-
3
-interface upload_sdk {
4
-
5
-  //初始化文件 md5 
6
-  generateMd5: (file: File) => any
7
-  //初始化图片 base64
8
-  generateBase64: (file: File) =>any
9
-  //上传
10
-  action: () => Promise<any>
11
-  //成功返回函数
12
-  onSuccess: () => string
13
-  //续传
14
-  onContinue: () => number
15
-  //错误
16
-  onError: () => number
17
-  //http 请求
18
-  post: () => Promise<any>
19
-
20
-}
21
-
22
-class UploadSdk implements upload_sdk {
23
-
24
-  private host: string
25
-  private origin: string
26
-  private token: string
27
-  private file: File
28
-  private base64: any
29
-  private BYTES_PER_CHUNK: number = 1024 * 1024
30
-  private IMAGE_MULTIPART = '/multipart/upload/'
31
-  private VIDEO_MULTIPART = '/upload/'
32
-  private chunkNum: any
33
-  private chunkSize: any
34
-  private fileType: any
35
-  private fileSize: any
36
-  private base64Body: any
37
-  private md5: any
38
-  private offset = 0; //用于断点续传,默认为 0
39
-
40
-  constructor(host: string, origin: string, token: string, file: File){
41
-    this.host = host
42
-    this.origin = origin
43
-    this.token = token
44
-    this.file = file
45
-    this.fileSize = file.size
46
-    this.fileType = file.type
47
-    this.chunkNum =  this.BYTES_PER_CHUNK > this.fileSize ? Math.ceil(this.fileSize / this.BYTES_PER_CHUNK):1
48
-    this.chunkSize = this.BYTES_PER_CHUNK > this.fileSize ? this.fileSize:this.BYTES_PER_CHUNK
49
-  }
50
-
51
-  generateMd5 = async () => {
52
-    let md5 = await getFilemd5sum(this.file)
53
-    return md5
54
-  }
55
-
56
-  generateBase64 = async () => {
57
-    let base64 = await getBase64(this.file)
58
-    return base64
59
-  }
60
-  //上传
61
-  action = () => {
62
-    this.md5 = this.generateMd5()
63
-    this.base64Body = this.generateBase64()
64
-    return this.post()
65
-  }
66
-  //成功返回函数
67
-  onSuccess =  () => {
68
-    return 'success'
69
-  }
70
-  //续传
71
-  onContinue = () => {
72
-    return 90
73
-  }
74
-  //错误
75
-  onError = () => {
76
-    return 200
77
-  }
78
-
79
-  post = () => {
80
-    return new Promise((resolve, reject) => {
81
-      let xhr = new XMLHttpRequest()
82
-      xhr.open("POSt", 'http://api.links123.net/mps/v1/'+this.IMAGE_MULTIPART+'forum', true)
83
-        xhr.withCredentials = true;
84
-        xhr.setRequestHeader('Authorization', `Bearer ${this.token}`);
85
-        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
86
-        xhr.setRequestHeader('Accept', 'application/json');
87
-        xhr.setRequestHeader('LINK_UPLOAD_FILE_SIZE',this.fileSize)
88
-        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_INDEX',1+'')
89
-        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_NUM',this.chunkNum)
90
-        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_SIZE',this.chunkSize)
91
-        xhr.setRequestHeader('LINK_UPLOAD_OFFSET',this.offset+'')
92
-        xhr.setRequestHeader('LINK_UPLOAD_FILE_MD5',this.md5)
93
-        xhr.setRequestHeader('LINK_UPLOAD_FILE_TYPE','jepg')
94
-        xhr.send(JSON.stringify({body: this.base64Body}))
95
-        xhr.onreadystatechange = function () {
96
-          if (xhr.readyState === 4) {
97
-            if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
98
-              resolve(xhr.responseText)
99
-            } else {
100
-              reject(xhr.status)
101
-            }
102
-          }
103
-        }
104
-    })
105
-  }
106
-
107
-}
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-

+ 104
- 45
utils.js Datei anzeigen

@@ -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
 }