|
| 1 | +export class FileChunk { |
| 2 | + public stepSize:number = 1024 * 1024 * 3; |
| 3 | + public rawFile:any = null; |
| 4 | + public uploadProgress:number = null; |
| 5 | + public uploading:boolean = null; |
| 6 | + public uploadComplete:boolean = null; |
| 7 | + public byteStepSize:number = null; |
| 8 | + public totalSize:number = null; |
| 9 | + public startByte:number = null; |
| 10 | + public endByte:number = null; |
| 11 | + public currentChunk:number = 0; |
| 12 | + public totalChunks:number = null; |
| 13 | + public uniqueIdentifier:string = null; |
| 14 | + public totalSent:number = null; |
| 15 | + public extraData:any = {}; |
| 16 | + constructor(rawFile:any, options:any = {}) { |
| 17 | + |
| 18 | + if (typeof options !== 'undefined'){ |
| 19 | + if (typeof options.byteStepSize !== 'undefined'){ |
| 20 | + this.setByteStepSize(options.byteStepSize); |
| 21 | + } |
| 22 | + } |
| 23 | + this.setRawFile(rawFile) |
| 24 | + |
| 25 | + this.setRawFile(rawFile); |
| 26 | + this.setUploadProgress(0); |
| 27 | + this.setUploading(false); |
| 28 | + this.setUploadComplete(false); |
| 29 | + this.setTotalSize(this.getRawFile().size); |
| 30 | + this.setStartByte(0); |
| 31 | + this.setEndByte(this.getByteStepSize()); |
| 32 | + this.setCurrentChunk(0); |
| 33 | + if (!this.getBrowserSliceMethod()){ |
| 34 | + this.setTotalChunks(1); |
| 35 | + } |
| 36 | + else{ |
| 37 | + this.setTotalChunks(Math.ceil(this.totalSize / this.byteStepSize)); |
| 38 | + } |
| 39 | + this.setUniqueIdenfier(this.generateUniqueIdentifier()); |
| 40 | + this.setTotalSent(0); |
| 41 | + } |
| 42 | + |
| 43 | + public setExtraData(index:any, value:any){ |
| 44 | + this.extraData[index] = value; |
| 45 | + } |
| 46 | + |
| 47 | + public getExtraData(index:any){ |
| 48 | + return this.extraData[index]; |
| 49 | + } |
| 50 | + |
| 51 | + //getters and setters |
| 52 | + public setProgress(v:any){ |
| 53 | + this.uploadProgress = v; |
| 54 | + } |
| 55 | + |
| 56 | + public getProgress(){ |
| 57 | + return this.uploadProgress; |
| 58 | + } |
| 59 | + |
| 60 | + public setUploading(v:boolean){ |
| 61 | + this.uploading = v; |
| 62 | + } |
| 63 | + |
| 64 | + public getUploading(){ |
| 65 | + return this.uploading; |
| 66 | + } |
| 67 | + |
| 68 | + public getUploadComplete(){ |
| 69 | + return this.uploadComplete; |
| 70 | + } |
| 71 | + |
| 72 | + public setUploadComplete(v:boolean){ |
| 73 | + this.uploadComplete = v; |
| 74 | + } |
| 75 | + |
| 76 | + public setUploadProgress(v:number){ |
| 77 | + this.uploadProgress = v; |
| 78 | + } |
| 79 | + |
| 80 | + public getUploadProgress(){ |
| 81 | + return this.uploadProgress; |
| 82 | + } |
| 83 | + |
| 84 | + public getStartByte(){ |
| 85 | + return this.startByte; |
| 86 | + } |
| 87 | + |
| 88 | + public setStartByte(v:number){ |
| 89 | + this.startByte = v; |
| 90 | + } |
| 91 | + |
| 92 | + public getEndByte(){ |
| 93 | + return this.endByte; |
| 94 | + } |
| 95 | + |
| 96 | + public setEndByte(v:number){ |
| 97 | + this.endByte = v; |
| 98 | + } |
| 99 | + |
| 100 | + public getByteStepSize(){ |
| 101 | + return this.byteStepSize; |
| 102 | + } |
| 103 | + |
| 104 | + public setByteStepSize(v:number){ |
| 105 | + this.byteStepSize = v; |
| 106 | + } |
| 107 | + |
| 108 | + public setTotalSize(v:number){ |
| 109 | + this.totalSize = v; |
| 110 | + } |
| 111 | + |
| 112 | + public getTotalSize(){ |
| 113 | + return this.totalSize; |
| 114 | + } |
| 115 | + |
| 116 | + public getRawFile(){ |
| 117 | + return this.rawFile; |
| 118 | + } |
| 119 | + |
| 120 | + public setRawFile(v:File){ |
| 121 | + this.rawFile = v; |
| 122 | + } |
| 123 | + |
| 124 | + public getCurrentChunk(){ |
| 125 | + return this.currentChunk; |
| 126 | + } |
| 127 | + |
| 128 | + public setCurrentChunk(v:number){ |
| 129 | + this.currentChunk = v; |
| 130 | + } |
| 131 | + |
| 132 | + public getTotalChunks(){ |
| 133 | + return this.totalChunks; |
| 134 | + } |
| 135 | + |
| 136 | + public setTotalChunks(v:number){ |
| 137 | + this.totalChunks = v; |
| 138 | + } |
| 139 | + |
| 140 | + public setUniqueIdenfier(v:string){ |
| 141 | + this.uniqueIdentifier = v; |
| 142 | + } |
| 143 | + |
| 144 | + public getUniqueIdenfier(){ |
| 145 | + return this.uniqueIdentifier; |
| 146 | + } |
| 147 | + |
| 148 | + public getRawFileExtension(){ |
| 149 | + const extension = this.getRawFileName().split('.'); |
| 150 | + return extension[extension.length - 1]; |
| 151 | + } |
| 152 | + |
| 153 | + public getRawFileName(){ |
| 154 | + return this.getRawFile().name; |
| 155 | + } |
| 156 | + |
| 157 | + public getContentType(){ |
| 158 | + return this.getRawFile().type; |
| 159 | + } |
| 160 | + |
| 161 | + public getTotalSent(){ |
| 162 | + return this.totalSent; |
| 163 | + } |
| 164 | + |
| 165 | + public setTotalSent(v:number){ |
| 166 | + this.totalSent = v; |
| 167 | + } |
| 168 | + |
| 169 | + public getCurrentRawFileChunk(){ |
| 170 | + if (!this.getBrowserSliceMethod()){ |
| 171 | + return this.getRawFile(); |
| 172 | + } |
| 173 | + else{ |
| 174 | + return this.getRawFile()[this.getBrowserSliceMethod()](this.getStartByte(), this.getEndByte()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public retrocedeChunk(){ |
| 179 | + if (!this.getBrowserSliceMethod()){ |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + this.setEndByte(this.getStartByte()); |
| 184 | + this.setStartByte(this.getStartByte() - this.getByteStepSize()); |
| 185 | + this.setCurrentChunk(this.getCurrentChunk() - 1); |
| 186 | + |
| 187 | + if (this.getTotalSent() != 0){ |
| 188 | + this.setTotalSent(this.getTotalSent() - this.getByteStepSize()); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public prepareNextChunk(){ |
| 193 | + if (!this.getBrowserSliceMethod()){ |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + if (this.getEndByte() > this.getTotalSize()){ //finished |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + this.setStartByte(this.getEndByte()); |
| 202 | + this.setEndByte(this.getEndByte() + this.getByteStepSize()); |
| 203 | + this.setCurrentChunk(this.getCurrentChunk() + 1); |
| 204 | + |
| 205 | + return true; |
| 206 | + } |
| 207 | + |
| 208 | + public getBrowserSliceMethod():string{ |
| 209 | + if (this.rawFile && typeof this.rawFile !== 'undefined'){ |
| 210 | + if (this.rawFile.slice && typeof this.rawFile.slice == 'function'){ |
| 211 | + return 'slice'; |
| 212 | + } |
| 213 | + else if(this.rawFile.mozSlice && typeof this.rawFile.mozSlice == 'function'){ |
| 214 | + return 'mozSlice'; |
| 215 | + } |
| 216 | + else if(this.rawFile.webkitSlice && typeof this.rawFile.webkitSlice == 'function'){ |
| 217 | + return 'webkitSlice'; |
| 218 | + } |
| 219 | + } |
| 220 | + else{ |
| 221 | + return null; |
| 222 | + } |
| 223 | + }//getBrowserSliceMethod() ends here |
| 224 | + |
| 225 | + |
| 226 | + public generateUniqueIdentifier():string{ |
| 227 | + var d = new Date().getTime(); |
| 228 | + |
| 229 | + if (typeof performance !== 'undefined' && typeof performance.now === 'function'){ |
| 230 | + d += performance.now(); //use high-precision timer if available |
| 231 | + } |
| 232 | + |
| 233 | + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
| 234 | + var r = (d + Math.random() * 16) % 16 | 0; |
| 235 | + d = Math.floor(d / 16); |
| 236 | + return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); |
| 237 | + }); |
| 238 | + } |
| 239 | + |
| 240 | +} |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
0 commit comments