| 1 |
( function () { |
| 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 |
function Upload( apiconfig ) { |
| 49 |
this.api = ( apiconfig instanceof mw.Api ) ? apiconfig : new mw.Api( apiconfig ); |
| 50 |
|
| 51 |
this.watchlist = false; |
| 52 |
this.text = ''; |
| 53 |
this.comment = ''; |
| 54 |
this.filename = null; |
| 55 |
this.file = null; |
| 56 |
this.setState( Upload.State.NEW ); |
| 57 |
|
| 58 |
this.imageinfo = undefined; |
| 59 |
} |
| 60 |
|
| 61 |
const UP = Upload.prototype; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
UP.getApi = function () { |
| 71 |
return $.Deferred().resolve( this.api ).promise(); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
UP.setText = function ( text ) { |
| 82 |
this.text = text; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
UP.setFilename = function ( filename ) { |
| 93 |
this.filename = filename; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
UP.setFilekey = function ( filekey ) { |
| 104 |
const upload = this; |
| 105 |
|
| 106 |
this.setState( Upload.State.STASHED ); |
| 107 |
this.stashPromise = $.Deferred().resolve( ( data ) => upload.api.uploadFromStash( filekey, data ) ); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
UP.setFilenameFromFile = function () { |
| 117 |
const file = this.getFile(); |
| 118 |
if ( !file ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
if ( file.nodeType && file.nodeType === Node.ELEMENT_NODE ) { |
| 122 |
|
| 123 |
this.setFilename( this.getBasename( file.value ) ); |
| 124 |
} else if ( file.name ) { |
| 125 |
|
| 126 |
this.setFilename( this.getBasename( file.name ) ); |
| 127 |
} else { |
| 128 |
|
| 129 |
this.setFilename( '?' ); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
UP.setFile = function ( file ) { |
| 141 |
this.file = file; |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
UP.setWatchlist = function ( watchlist ) { |
| 152 |
this.watchlist = watchlist; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
UP.setComment = function ( comment ) { |
| 163 |
this.comment = comment; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
UP.getText = function () { |
| 174 |
return this.text; |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
UP.getFilename = function () { |
| 185 |
return this.filename; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
UP.getFile = function () { |
| 196 |
return this.file; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
UP.getWatchlist = function () { |
| 207 |
return this.watchlist; |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
UP.getComment = function () { |
| 218 |
return this.comment; |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
UP.getBasename = function ( path ) { |
| 230 |
if ( path === undefined || path === null ) { |
| 231 |
return ''; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
return path.slice( |
| 237 |
Math.max( |
| 238 |
path.lastIndexOf( '/' ), |
| 239 |
path.lastIndexOf( '\\' ) |
| 240 |
) + 1 |
| 241 |
); |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
UP.setState = function ( state, stateDetails ) { |
| 253 |
this.state = state; |
| 254 |
this.stateDetails = stateDetails; |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
UP.getState = function () { |
| 265 |
return this.state; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
UP.getStateDetails = function () { |
| 276 |
return this.stateDetails; |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
UP.getImageInfo = function () { |
| 289 |
return this.imageinfo; |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
UP.upload = function () { |
| 300 |
const upload = this; |
| 301 |
|
| 302 |
if ( !this.getFile() ) { |
| 303 |
return $.Deferred().reject( 'No file to upload. Call setFile to add one.' ); |
| 304 |
} |
| 305 |
|
| 306 |
if ( !this.getFilename() ) { |
| 307 |
return $.Deferred().reject( 'No filename set. Call setFilename to add one.' ); |
| 308 |
} |
| 309 |
|
| 310 |
this.setState( Upload.State.UPLOADING ); |
| 311 |
|
| 312 |
return this.api.chunkedUpload( this.getFile(), { |
| 313 |
watchlist: ( this.getWatchlist() ) ? 1 : undefined, |
| 314 |
comment: this.getComment(), |
| 315 |
filename: this.getFilename(), |
| 316 |
text: this.getText() |
| 317 |
} ).then( ( result ) => { |
| 318 |
upload.setState( Upload.State.UPLOADED ); |
| 319 |
upload.imageinfo = result.upload.imageinfo; |
| 320 |
return result; |
| 321 |
}, ( errorCode, result ) => { |
| 322 |
if ( result && result.upload && result.upload.warnings ) { |
| 323 |
upload.setState( Upload.State.WARNING, result ); |
| 324 |
} else { |
| 325 |
upload.setState( Upload.State.ERROR, result ); |
| 326 |
} |
| 327 |
return $.Deferred().reject( errorCode, result ); |
| 328 |
} ); |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
UP.uploadToStash = function () { |
| 339 |
const upload = this; |
| 340 |
|
| 341 |
if ( !this.getFile() ) { |
| 342 |
return $.Deferred().reject( 'No file to upload. Call setFile to add one.' ); |
| 343 |
} |
| 344 |
|
| 345 |
if ( !this.getFilename() ) { |
| 346 |
this.setFilenameFromFile(); |
| 347 |
} |
| 348 |
|
| 349 |
this.setState( Upload.State.UPLOADING ); |
| 350 |
|
| 351 |
this.stashPromise = this.api.chunkedUploadToStash( this.getFile(), { |
| 352 |
ignorewarnings: true, |
| 353 |
filename: this.getFilename() |
| 354 |
} ).then( ( finishStash ) => { |
| 355 |
upload.setState( Upload.State.STASHED ); |
| 356 |
return finishStash; |
| 357 |
}, ( errorCode, result ) => { |
| 358 |
if ( result && result.upload && result.upload.warnings ) { |
| 359 |
upload.setState( Upload.State.WARNING, result ); |
| 360 |
} else { |
| 361 |
upload.setState( Upload.State.ERROR, result ); |
| 362 |
} |
| 363 |
return $.Deferred().reject( errorCode, result ); |
| 364 |
} ); |
| 365 |
|
| 366 |
return this.stashPromise; |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
UP.finishStashUpload = function () { |
| 377 |
const upload = this; |
| 378 |
|
| 379 |
if ( !this.stashPromise ) { |
| 380 |
return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' ); |
| 381 |
} |
| 382 |
|
| 383 |
return this.stashPromise.then( ( finishStash ) => { |
| 384 |
upload.setState( Upload.State.UPLOADING ); |
| 385 |
|
| 386 |
return finishStash( { |
| 387 |
ignorewarnings: false, |
| 388 |
watchlist: ( upload.getWatchlist() ) ? 1 : undefined, |
| 389 |
comment: upload.getComment(), |
| 390 |
filename: upload.getFilename(), |
| 391 |
text: upload.getText() |
| 392 |
} ).then( ( result ) => { |
| 393 |
upload.setState( Upload.State.UPLOADED ); |
| 394 |
upload.imageinfo = result.upload.imageinfo; |
| 395 |
return result; |
| 396 |
}, ( errorCode, result ) => { |
| 397 |
if ( result && result.upload && result.upload.warnings ) { |
| 398 |
upload.setState( Upload.State.WARNING, result ); |
| 399 |
} else { |
| 400 |
upload.setState( Upload.State.ERROR, result ); |
| 401 |
} |
| 402 |
return $.Deferred().reject( errorCode, result ); |
| 403 |
} ); |
| 404 |
} ); |
| 405 |
}; |
| 406 |
|
| 407 |
mw.Upload = Upload; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
mw.Upload.State = { |
| 415 |
|
| 416 |
NEW: 0, |
| 417 |
|
| 418 |
|
| 419 |
WARNING: 1, |
| 420 |
|
| 421 |
|
| 422 |
ERROR: 2, |
| 423 |
|
| 424 |
|
| 425 |
UPLOADING: 3, |
| 426 |
|
| 427 |
|
| 428 |
STASHED: 4, |
| 429 |
|
| 430 |
|
| 431 |
UPLOADED: 5 |
| 432 |
}; |
| 433 |
}() ); |