-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
170 lines (163 loc) · 5.31 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let CryptoJS = require("crypto-js");
import axios from './axios-base'
import imageUtils from './imagesUtils'
import helpers from './helpers'
class FileChunk{
static slice(file, blob, offset, chunkSize){
let reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onerror = () => {
reader.abort();
reject({
message:'Error parsing',
statusCode:error.response.status
});
};
reader.onload = () => {
resolve(reader.result);
};
reader.readAsArrayBuffer(blob.call(file, offset,chunkSize))
});
}
}
export default class UploadTusMedia{
renameFile(filename){
let pattern=/\.[0-9a-z]+$/i;
let uuid = this.createUUID();
let extension = filename.match(pattern);
return uuid+extension;
}
createUUID(){
var dt = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}
async uploadFiles(files){
if(files.length){
let media = [];
for(let file of files){
let blobSlice = Blob.prototype.slice;
let chunkSize = 10485760;
let fileSlices = [];
let fileSize = file.size;
let fileRename = this.renameFile(file.name);
let offset = 0;
//get full content of file return promise
let fileFullChecksum = await FileChunk.slice(file,blobSlice,offset,file.size)
.then((response) => {
let arrayBuffer = CryptoJS.lib.WordArray.create(response);
return CryptoJS.MD5(arrayBuffer).toString(CryptoJS.enc.Base64);//CryptoJS.enc.Base64
})
.catch((error) => {
return error;
})
let idx = 1;
let retry = 0;
while(offset < fileSize){
//slice part of file
let fileSlice = await FileChunk.slice(file,blobSlice,offset,(offset + chunkSize))
.then((response) => {
let arrayBuffer = CryptoJS.lib.WordArray.create(response);
let fileSliceChecksum = CryptoJS.MD5(arrayBuffer).toString(CryptoJS.enc.Base64);//CryptoJS.enc.Base64
return {buffer:response,checksum:fileSliceChecksum}
})
.catch((error) => {
return error;
})
// send slice file to server
let newOffset = await this.send({
content: fileSlice.buffer,
sliceChecksum: fileSlice.checksum,
fileSize:fileSize,
fileFullChecksum: fileFullChecksum,
offset: offset,
filename: fileRename,
retry:retry
})
//retry number file send
retry = retry + 1
//update offset
offset = idx * newOffset
if(offset === fileSize){
//genarate thumbnails
let fileWithSrc = null;
if(helpers.checkTypeMedia(file.type) == 'image'){
fileWithSrc = await imageUtils.readFile(file)
.then((response) => {
return response;
})
.catch((error) => {
return error;
})
media.push({filename:fileRename, file:fileWithSrc});
}
else{
media.push({filename:fileRename, file:file});
}
}
}
}
return media;
}
}
//upload file to server
send(params){
return new Promise((resolve, reject) => {
axios.patch('tus/'+ params.filename,
params.content,
{headers:{
'Accept':'application/json',
// Standard TUS headers
"Content-Type" : "application/offset+octet-stream",
"Tus-Resumable" : "1.0.0",
"Upload-Offset" : params.offset,
"Upload-Checksum" : 'md5 '+ params.sliceChecksum,
// ThunderTUS headers
"CrossCheck" : "true",
"Express" : "true",
"Upload-Length" : params.fileSize,
"Upload-CrossChecksum" : 'md5 '+ params.fileFullChecksum
},
}
)
.then((response) => {
//204 => upload da parte okay
//460 => Checksum Mismatch - 3 tentivas cancelar o envio
//410 => checksum final do ficheiro inteiro nao bate certo - cancelar o envio
//400 => md5 server not suport - cancelar envio
//404 => ficheiro not exist on server - cancelar envio
console.log(response.status )
if(response.status === 204 ){
resolve(response.headers['upload-offset'])
}
else{
reject({
message:response.data,
statusCode:response.status
})
}
})
.catch((error) => {
if(error.response.status === 460){
if(params.retry === 3){
reject({
message:error.response.data,
statusCode:error.response.status
})
}
resolve(error.response.headers['upload-offset'])
}
else{
reject({
message:error.response.data,
statusCode:error.response.status
})
}
});
});
}
}