1
- import { assign , defineProperty , copyOwnAndInheritedProps , uniqueId , round , toKbps , toMbps } from "../core/utils"
1
+ import { assign , defineProperty , copyOwnAndInheritedProps , uniqueId , round , toKbps , toMbps } from "../core/utils" ;
2
2
3
3
export interface IUploadStats {
4
- bytesSent : number
5
- progress : number
6
- total : number
7
- speed : ISpeedStats
8
- start : number
9
- end : number
10
- time : number
4
+ bytesSent : number ;
5
+ progress : number ;
6
+ total : number ;
7
+ speed : ISpeedStats ;
8
+ start : number ;
9
+ end : number ;
10
+ time : number ;
11
11
}
12
12
13
13
export interface ISpeedStats {
14
- kbps : number
15
- mbps : number
14
+ kbps : number ;
15
+ mbps : number ;
16
16
}
17
17
18
18
class VTransmitFile {
19
- private _nativeFile : File = null
20
- id : string = VTransmitFile . idFactory ( )
21
- accepted : boolean = undefined // Passed all validation.
22
- lastModified : number = undefined
23
- lastModifiedDate : Date = undefined
24
- name : string = undefined
25
- processing : boolean = undefined
26
- size : number = undefined
27
- status : string = undefined
28
- type : string = undefined
19
+ private _nativeFile : File = null ;
20
+ id : string = VTransmitFile . idFactory ( ) ;
21
+ accepted : boolean = undefined ; // Passed all validation.
22
+ lastModified : number = undefined ;
23
+ lastModifiedDate : Date = undefined ;
24
+ name : string = undefined ;
25
+ processing : boolean = undefined ;
26
+ size : number = undefined ;
27
+ status : string = undefined ;
28
+ type : string = undefined ;
29
29
upload : IUploadStats = {
30
30
bytesSent : 0 ,
31
31
progress : 0 ,
@@ -37,82 +37,110 @@ class VTransmitFile {
37
37
start : undefined ,
38
38
end : undefined ,
39
39
time : undefined ,
40
- }
41
- webkitRelativePath : USVString = undefined
42
- width : number = undefined
43
- height : number = undefined
44
- xhr : XMLHttpRequest = undefined
45
- private _dataUrl : string
46
- errorMessage : string = undefined
47
- thumbnailLoaded : boolean = false
40
+ } ;
41
+ webkitRelativePath : USVString = undefined ;
42
+ width : number = undefined ;
43
+ height : number = undefined ;
44
+ xhr : XMLHttpRequest = undefined ;
45
+ private _dataUrl : string ;
46
+ errorMessage : string = undefined ;
47
+ thumbnailLoaded : boolean = false ;
48
+ isChunked : boolean = false ;
49
+ chunkLength : number = 1 ;
50
+ chunkIndex : number = 0 ;
48
51
49
52
constructor ( ...data : object [ ] ) {
50
- assign ( this , ...data )
53
+ assign ( this , ...data ) ;
51
54
}
52
55
53
56
set ( ...data : object [ ] ) : VTransmitFile {
54
- assign ( this , ...data )
55
- return this
57
+ assign ( this , ...data ) ;
58
+ return this ;
56
59
}
57
60
58
61
copyNativeFile ( file : File ) : VTransmitFile {
59
62
// save reference for upload
60
- this . nativeFile = file
63
+ this . nativeFile = file ;
61
64
// Copy props to normal object for Vue reactivity.
62
65
// Vue cannot define reactive properties on native file's readonly props.
63
- return this . set ( copyOwnAndInheritedProps ( file ) )
66
+ return this . set ( copyOwnAndInheritedProps ( file ) ) ;
64
67
}
65
68
66
69
copyOwnAndInheritedProps ( ...data : object [ ] ) : VTransmitFile {
67
- return this . set ( ...data . map ( copyOwnAndInheritedProps ) )
70
+ return this . set ( ...data . map ( copyOwnAndInheritedProps ) ) ;
68
71
}
69
72
70
73
handleProgress ( e : ProgressEvent ) : void {
71
- this . startProgress ( )
72
- const total = e . total || this . upload . total
73
- this . upload . progress = Math . min ( 100 , 100 * e . loaded / total )
74
- this . upload . bytesSent = e . loaded
75
- this . upload . total = total
76
- this . upload . time = ( Date . now ( ) - this . upload . start ) / 1000
74
+ this . startProgress ( ) ;
75
+ const total = e . total || this . upload . total ;
76
+ this . upload . progress = Math . min ( 100 , ( 100 * e . loaded ) / total ) ;
77
+ this . upload . bytesSent = e . loaded ;
78
+ this . upload . total = total ;
79
+ this . upload . time = ( Date . now ( ) - this . upload . start ) / 1000 ;
77
80
// Recalc the upload speed in bytes/sec
78
- this . upload . speed . kbps = round ( toKbps ( this . upload . bytesSent , this . upload . time ) )
79
- this . upload . speed . mbps = round ( toMbps ( this . upload . bytesSent , this . upload . time ) )
81
+ this . upload . speed . kbps = round ( toKbps ( this . upload . bytesSent , this . upload . time ) ) ;
82
+ this . upload . speed . mbps = round ( toMbps ( this . upload . bytesSent , this . upload . time ) ) ;
80
83
if ( this . upload . progress === 100 ) {
81
- this . endProgress ( )
84
+ this . endProgress ( ) ;
82
85
}
83
86
}
84
87
85
88
startProgress ( ) : VTransmitFile {
86
89
// Avoid starting twice
87
90
if ( typeof this . upload . start !== "number" ) {
88
- this . upload . start = Date . now ( )
91
+ this . upload . start = Date . now ( ) ;
89
92
}
90
- return this
93
+ return this ;
91
94
}
92
95
93
96
endProgress ( ) : VTransmitFile {
94
97
// Avoid ending twice
95
98
if ( typeof this . upload . end !== "number" ) {
96
- this . upload . end = Date . now ( )
97
- this . upload . time = ( Date . now ( ) - this . upload . start ) / 1000
99
+ this . upload . end = Date . now ( ) ;
100
+ this . upload . time = ( Date . now ( ) - this . upload . start ) / 1000 ;
101
+ }
102
+ return this ;
103
+ }
104
+
105
+ chunkify ( maxBytes : number ) {
106
+ if ( this . size <= maxBytes ) {
107
+ return [ VTransmitFile . fromNativeFile ( this . nativeFile ) ] ;
98
108
}
99
- return this
109
+
110
+ let chunks : VTransmitFile [ ] = [ ] ;
111
+ let chunkLength = Math . ceil ( this . size / maxBytes ) ;
112
+
113
+ for ( let chunkIndex = 0 ; chunkIndex < chunkLength ; chunkIndex ++ ) {
114
+ let offset = chunkIndex * maxBytes ;
115
+
116
+ chunks . push (
117
+ VTransmitFile . fromNativeFile (
118
+ new File ( [ this . nativeFile . slice ( offset , offset + maxBytes ) ] , this . name , {
119
+ type : this . type ,
120
+ lastModified : this . lastModified ,
121
+ } ) ,
122
+ { chunkIndex, chunkLength, isChunked : true }
123
+ )
124
+ ) ;
125
+ }
126
+
127
+ return chunks ;
100
128
}
101
129
102
130
get nativeFile ( ) {
103
- return this . _nativeFile
131
+ return this . _nativeFile ;
104
132
}
105
133
106
134
set nativeFile ( file : File ) {
107
135
if ( ! ( file instanceof File ) ) {
108
- throw new TypeError ( `[${ VTransmitFile . name } ] Expected an instance of File (native).` )
136
+ throw new TypeError ( `[${ VTransmitFile . name } ] Expected an instance of File (native).` ) ;
109
137
}
110
- this . _nativeFile = file
111
- this . upload . total = file . size
138
+ this . _nativeFile = file ;
139
+ this . upload . total = file . size ;
112
140
}
113
141
114
142
get dataUrl ( ) {
115
- return this . thumbnailLoaded ? this . _dataUrl : ( this . _dataUrl || "" )
143
+ return this . thumbnailLoaded ? this . _dataUrl : this . _dataUrl || "" ;
116
144
}
117
145
118
146
set dataUrl ( value ) {
@@ -121,19 +149,19 @@ class VTransmitFile {
121
149
enumerable : false ,
122
150
configurable : true ,
123
151
writable : true ,
124
- } )
125
- this . thumbnailLoaded = true
152
+ } ) ;
153
+ this . thumbnailLoaded = true ;
126
154
}
127
155
128
156
static fromNativeFile ( file : File , ...data ) {
129
- const instance = new VTransmitFile ( ...data )
130
- instance . copyNativeFile ( file )
131
- return instance
157
+ const instance = new VTransmitFile ( ...data ) ;
158
+ instance . copyNativeFile ( file ) ;
159
+ return instance ;
132
160
}
133
161
134
162
static idFactory ( ) {
135
- return uniqueId ( "v-transmit-file-" )
163
+ return uniqueId ( "v-transmit-file-" ) ;
136
164
}
137
165
}
138
166
139
- export default VTransmitFile
167
+ export default VTransmitFile ;
0 commit comments