ChrisFuck vor 5 Jahren
Ursprung
Commit
d8bee88f1e
3 geänderte Dateien mit 114 neuen und 86 gelöschten Zeilen
  1. 0
    51
      index.js
  2. 72
    0
      index.ts
  3. 42
    35
      utils.js

+ 0
- 51
index.js Datei anzeigen

@@ -1,51 +0,0 @@
1
-import {get_filemd5sum, getBase64} from './utils.js'
2
-
3
-function FileUpLoadSdk () {
4
-
5
-  const BYTES_PER_CHUNK = 1024 * 1024; // 每个文件切片大小定为1MB
6
-  let offset = 0; //用于断点续传,默认为 0
7
-
8
-  const IMAGE_TYPE_ERROR_CODE = 10
9
-
10
-  const IMAGE_MULTIPART = '/multipart/upload/'
11
-  const VIDOE_UPLOAD = '/upload/'
12
-
13
-  postImage = (host, origin) => {
14
-
15
-    if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(origin)) {
16
-      return IMAGE_TYPE_ERROR_CODE
17
-    }
18
-
19
-    var index1= origin.lastIndexOf(".");
20
-    var index2= origin.length;
21
-    let fileType= origin.substring(index1+1,index2)
22
-    let fileSize = origin.size
23
-    let md5 = get_filemd5sum(origin)
24
-    let chunkNum = Math.ceil(this.fileSize / this.BYTES_PER_CHUNK); // 分片个数
25
-    let base64Body = getBase64(origin)
26
-    let xhr = new XMLHttpRequest()
27
-    xhr.setRequestHeader('LINK_UPLOAD_FILE_SIZE',fileSize)
28
-    xhr.setRequestHeader('LINK_UPLOAD_CHUNK_INDEX',1)
29
-    xhr.setRequestHeader('LINK_UPLOAD_CHUNK_NUM',chunkNum)
30
-    xhr.setRequestHeader('LINK_UPLOAD_CHUNK_SIZE',BYTES_PER_CHUNK)
31
-    xhr.setRequestHeader('LINK_UPLOAD_OFFSET',offset)
32
-    xhr.setRequestHeader('LINK_UPLOAD_FILE_MD5',md5)
33
-    xhr.setRequestHeader('LINK_UPLOAD_FILE_TYPE',fileType)
34
-    xhr.open('post', host+IMAGE_MULTIPART+origin, true)
35
-    xhr.send(base64Body)
36
-
37
-    xhr.onreadyStateChange = function () {
38
-      if (xhr.readystate === 4) {
39
-        if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
40
-          console.log('type: success, result: ', xhr.responseText)
41
-        } else {
42
-          console.log('type: error, errCode:', xhr.status)
43
-        }
44
-      }
45
-    }
46
-  }
47
-  
48
-}
49
-
50
-
51
-

+ 72
- 0
index.ts Datei anzeigen

@@ -0,0 +1,72 @@
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
+
18
+}
19
+
20
+class UploadSdk implements upload_sdk {
21
+
22
+  private host: string
23
+  private origin: string
24
+  private token: string
25
+  private file: File
26
+  private md5: any
27
+  private base64: any
28
+  
29
+
30
+  constructor(host: string, origin: string, token: string, file: File){
31
+    this.host = host
32
+    this.origin = origin
33
+    this.token = token
34
+    this.file = file
35
+  }
36
+
37
+  generateMd5 = async (file: File) => {
38
+    let md5 = await getFilemd5sum(file)
39
+    return md5
40
+  }
41
+
42
+  generateBase64 = async (file: File) => {
43
+    let base64 = await getBase64(file)
44
+    return base64
45
+  }
46
+  //上传
47
+  action = async () => {
48
+    return new Promise((resolve, reject) => {
49
+      resolve()
50
+    })
51
+  }
52
+  //成功返回函数
53
+  onSuccess =  () => {
54
+    return 'success'
55
+  }
56
+  //续传
57
+  onContinue = () => {
58
+    return 90
59
+  }
60
+  //错误
61
+  onError = () => {
62
+    return 200
63
+  }
64
+
65
+}
66
+
67
+
68
+
69
+
70
+
71
+
72
+

+ 42
- 35
utils.js Datei anzeigen

@@ -1,44 +1,51 @@
1
-import { SparkMD5 } from 'spark-md5'
1
+import SparkMD5 from 'spark-md5'
2 2
 
3
-export function get_filemd5sum(ofile) {
4
-  var file = ofile;
5
-  var tmp_md5;
6
-  var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
7
-      // file = this.files[0],
8
-      chunkSize = 8097152, // Read in chunks of 2MB
9
-      chunks = Math.ceil(file.size / chunkSize),
10
-      currentChunk = 0,
11
-      spark = new SparkMD5.ArrayBuffer(),
12
-      fileReader = new FileReader();
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();
13 14
 
14
-  fileReader.onload = function(e) {
15
-      spark.append(e.target.result); // Append array buffer
16
-      currentChunk++;
15
+        fileReader.onload = function (e) {
16
+            spark.append(e.target.result); // Append array buffer
17
+            currentChunk++;
17 18
 
18
-      if (currentChunk < chunks) {
19
-          loadNext();
20
-      } else {
21
-          tmp_md5 = spark.end();
22
-          return tmp_md5
23
-      }
24
-  };
19
+            if (currentChunk < chunks) {
20
+                loadNext();
21
+            } else {
22
+                tmp_md5 = spark.end();
23
+                resolve(tmp_md5)
24
+            }
25
+        };
25 26
 
26
-  fileReader.onerror = function() {
27
-      console.warn('oops, something went wrong.');
28
-  };
27
+        fileReader.onerror = function () {
28
+            reject('error');
29
+        };
29 30
 
30
-  function loadNext() {
31
-      var start = currentChunk * chunkSize,
32
-          end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
33
-      fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
34
-  }
35
-  loadNext();
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
+    })
36 38
 }
37 39
 
38 40
 export function getBase64(file) {
39
-    var reader = new FileReader();
40
-    reader.readAsDataURL(file);
41
-    reader.onload = function (e) { 
42
-        return this.result.substring(this.result.indexOf(',')+1);
43
-    }
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);
46
+        }
47
+        reader.onerror = function() {
48
+            reject('error')
49
+        }
50
+    });
44 51
 }