| 1 |
/** |
| 2 |
* |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Application |
| 7 |
var wpStatelessApp = angular.module('wpStatelessApp', []) |
| 8 |
|
| 9 |
// Controller |
| 10 |
.controller('wpStatelessTools', ['$scope', '$http', function ($scope, $http) { |
| 11 |
|
| 12 |
var WP_DEBUG = wp_stateless_configs.WP_DEBUG || false; |
| 13 |
$scope.action = 'regenerate_images'; |
| 14 |
$scope.method = 'start'; |
| 15 |
$scope.bulk_size = 1; |
| 16 |
|
| 17 |
/** |
| 18 |
* Counters |
| 19 |
* @type {number} |
| 20 |
*/ |
| 21 |
$scope.objectsCounter = 0; |
| 22 |
$scope.objectsTotal = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* Error storage |
| 26 |
* @type {boolean} |
| 27 |
*/ |
| 28 |
$scope.error = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* Flags |
| 32 |
* @type {boolean} |
| 33 |
*/ |
| 34 |
$scope.isRunning = false; |
| 35 |
$scope.isLoading = false; |
| 36 |
$scope.continue = true; |
| 37 |
|
| 38 |
/** |
| 39 |
* |
| 40 |
* @type {{images: boolean, other: boolean}} |
| 41 |
*/ |
| 42 |
$scope.progresses = { |
| 43 |
images: false, |
| 44 |
other: false |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* |
| 49 |
* @type {{images: boolean, other: boolean}} |
| 50 |
*/ |
| 51 |
$scope.fails = { |
| 52 |
images: false, |
| 53 |
other: false |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* IDs storage |
| 58 |
* @type {Array} |
| 59 |
*/ |
| 60 |
$scope.objectIDs = []; |
| 61 |
|
| 62 |
/** |
| 63 |
* |
| 64 |
* @type {Array} |
| 65 |
*/ |
| 66 |
$scope.chunkIDs = []; |
| 67 |
|
| 68 |
/** |
| 69 |
* Log |
| 70 |
* @type {Array} |
| 71 |
*/ |
| 72 |
$scope.log = []; |
| 73 |
|
| 74 |
/** |
| 75 |
* Status message |
| 76 |
* @type {String} |
| 77 |
*/ |
| 78 |
$scope.status = ''; |
| 79 |
|
| 80 |
/** |
| 81 |
* Status message |
| 82 |
* @type {String} |
| 83 |
*/ |
| 84 |
$scope.extraStatus = ''; |
| 85 |
|
| 86 |
/** |
| 87 |
* Init |
| 88 |
*/ |
| 89 |
$scope.init = function() { |
| 90 |
jQuery("#regenthumbs-bar").progressbar(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get error message |
| 95 |
*/ |
| 96 |
$scope.getError = function(response, message) { |
| 97 |
if(response.data && typeof response.data.data !== 'undefined'){ |
| 98 |
return response.data.data; |
| 99 |
} |
| 100 |
|
| 101 |
if(response.data && typeof response.data == 'string'){ |
| 102 |
$scope.extraStatus = response.data; |
| 103 |
return message; |
| 104 |
} |
| 105 |
|
| 106 |
if(!response.data && response.statusText){ |
| 107 |
return response.statusText + " (Response code: " + response.status + ")"; |
| 108 |
} |
| 109 |
|
| 110 |
if(!response.data && response.status == -1){ |
| 111 |
return "Unable to connect to the server"; |
| 112 |
} |
| 113 |
|
| 114 |
return message; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* |
| 119 |
* @param callback |
| 120 |
*/ |
| 121 |
$scope.getCurrentProgresses = function( callback ) { |
| 122 |
$scope.isLoading = true; |
| 123 |
|
| 124 |
$http({ |
| 125 |
method: 'GET', |
| 126 |
url: ajaxurl, |
| 127 |
params: { |
| 128 |
action: 'stateless_get_current_progresses' |
| 129 |
} |
| 130 |
}).then(function(response){ |
| 131 |
var data = response.data || {}; |
| 132 |
|
| 133 |
if ( data.success ) { |
| 134 |
if ( typeof data.data !== 'undefined' ) { |
| 135 |
$scope.progresses.images = data.data.images; |
| 136 |
$scope.progresses.other = data.data.other; |
| 137 |
if ( 'function' === typeof callback ) { |
| 138 |
callback(); |
| 139 |
} |
| 140 |
} else { |
| 141 |
console.error( 'Could not retrieve progress' ); |
| 142 |
} |
| 143 |
} else { |
| 144 |
console.error( 'Could not retrieve progress' ); |
| 145 |
} |
| 146 |
|
| 147 |
$scope.isLoading = false; |
| 148 |
}, function(response) { |
| 149 |
console.error( 'Could not retrieve progress' ); |
| 150 |
|
| 151 |
$scope.isLoading = false; |
| 152 |
}); |
| 153 |
}; |
| 154 |
|
| 155 |
/** |
| 156 |
* |
| 157 |
*/ |
| 158 |
$scope.getCurrentProgresses(); |
| 159 |
|
| 160 |
/** |
| 161 |
* |
| 162 |
* @param callback |
| 163 |
*/ |
| 164 |
function getAllFails( callback ) { |
| 165 |
$scope.isLoading = true; |
| 166 |
|
| 167 |
$http({ |
| 168 |
method: 'GET', |
| 169 |
url: ajaxurl, |
| 170 |
params: { |
| 171 |
action: 'stateless_get_all_fails' |
| 172 |
} |
| 173 |
}).then(function(response){ |
| 174 |
var data = response.data || {}; |
| 175 |
|
| 176 |
if ( data.success ) { |
| 177 |
if ( typeof data.data !== 'undefined' ) { |
| 178 |
|
| 179 |
$scope.fails.images = data.data.images; |
| 180 |
$scope.fails.other = data.data.other; |
| 181 |
|
| 182 |
if ( 'function' === typeof callback ) { |
| 183 |
callback(); |
| 184 |
} |
| 185 |
} else { |
| 186 |
console.error( 'Could not get fails' ); |
| 187 |
} |
| 188 |
} else { |
| 189 |
console.error( 'Could not get fails' ); |
| 190 |
} |
| 191 |
|
| 192 |
$scope.isLoading = false; |
| 193 |
}, function(response) { |
| 194 |
console.error( 'Could not get fails' ); |
| 195 |
|
| 196 |
$scope.isLoading = false; |
| 197 |
}); |
| 198 |
}; |
| 199 |
|
| 200 |
getAllFails(); |
| 201 |
|
| 202 |
/** |
| 203 |
* Form submit handler |
| 204 |
* @param e |
| 205 |
* @returns {boolean} |
| 206 |
*/ |
| 207 |
$scope.processStart = function(e) { |
| 208 |
|
| 209 |
$scope.error = false; |
| 210 |
$scope.status = ''; |
| 211 |
$scope.extraStatus = ''; |
| 212 |
$scope.objectsCounter = 0; |
| 213 |
$scope.objectsTotal = 0; |
| 214 |
$scope.objectIDs = []; |
| 215 |
$scope.chunkIDs = []; |
| 216 |
|
| 217 |
if ( $scope.method === 'fix' ) { |
| 218 |
|
| 219 |
if ( $scope.action ) { |
| 220 |
switch( $scope.action ) { |
| 221 |
case 'regenerate_images': |
| 222 |
$scope.objectIDs = $scope.fails.images; |
| 223 |
$scope.regenerateImages(); |
| 224 |
break; |
| 225 |
case 'sync_non_images': |
| 226 |
$scope.objectIDs = $scope.fails.other; |
| 227 |
$scope.syncFiles(); |
| 228 |
break; |
| 229 |
default: break; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
var cont = 0; |
| 237 |
if ( 'continue' === $scope.method ) { |
| 238 |
cont = 1; |
| 239 |
} |
| 240 |
|
| 241 |
if ( $scope.action ) { |
| 242 |
switch( $scope.action ) { |
| 243 |
case 'regenerate_images': |
| 244 |
$scope.getImagesMedia( $scope.regenerateImages, cont ); |
| 245 |
break; |
| 246 |
case 'sync_non_images': |
| 247 |
$scope.getOtherMedia( $scope.syncFiles, cont ); |
| 248 |
break; |
| 249 |
case 'sync_non_library_files': |
| 250 |
$scope.getNonLibraryFiles( $scope.syncNonLibraryFiles, cont ); |
| 251 |
break; |
| 252 |
default: break; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return false; |
| 257 |
}; |
| 258 |
|
| 259 |
/** |
| 260 |
* Stop process |
| 261 |
*/ |
| 262 |
$scope.processStop = function() { |
| 263 |
$scope.status = 'Stopping...'; |
| 264 |
$scope.continue = false; |
| 265 |
}; |
| 266 |
|
| 267 |
function array_bulk( arr, bulk_size ) { |
| 268 |
var groups = []; |
| 269 |
var i; |
| 270 |
|
| 271 |
for ( i = 0; i < bulk_size; i++ ) { |
| 272 |
groups[ i ] = []; |
| 273 |
} |
| 274 |
|
| 275 |
for ( i = 0; i < arr.length; i ++ ) { |
| 276 |
groups[ i % bulk_size ].push( arr[ i ] ); |
| 277 |
} |
| 278 |
|
| 279 |
return groups; |
| 280 |
} |
| 281 |
|
| 282 |
$scope.finishProcess = function( chunk_id ) { |
| 283 |
var mode = 'images'; |
| 284 |
if ( 'sync_non_images' === $scope.action ) { |
| 285 |
mode = 'other'; |
| 286 |
} |
| 287 |
|
| 288 |
if ( $scope.objectsCounter >= $scope.objectsTotal ) { |
| 289 |
// process finished |
| 290 |
|
| 291 |
$http({ |
| 292 |
method: 'GET', |
| 293 |
url: ajaxurl, |
| 294 |
params: { |
| 295 |
action: 'stateless_reset_progress', |
| 296 |
mode: mode |
| 297 |
} |
| 298 |
}).then(function(response){ |
| 299 |
$scope.progresses[ mode ] = false; |
| 300 |
|
| 301 |
$scope.status = 'Finished'; |
| 302 |
$scope.isRunning = false; |
| 303 |
}, function(response) { |
| 304 |
console.error( 'Could not reset progress' ); |
| 305 |
}); |
| 306 |
} else if ( 'undefined' !== typeof chunk_id ) { |
| 307 |
// process cancelled, but this is only a chunk finishing request |
| 308 |
|
| 309 |
$scope.chunkIDs[ chunk_id ] = false; |
| 310 |
var all_done = true; |
| 311 |
for ( var i in $scope.chunkIDs ) { |
| 312 |
if ( false !== $scope.chunkIDs[ i ] ) { |
| 313 |
all_done = false; |
| 314 |
break; |
| 315 |
} |
| 316 |
} |
| 317 |
if ( all_done ) { |
| 318 |
$scope.getCurrentProgresses( function() { |
| 319 |
$scope.status = 'Cancelled'; |
| 320 |
$scope.isRunning = false; |
| 321 |
}); |
| 322 |
} |
| 323 |
} else { |
| 324 |
// process cancelled |
| 325 |
|
| 326 |
$scope.getCurrentProgresses( function() { |
| 327 |
$scope.status = 'Cancelled'; |
| 328 |
$scope.isRunning = false; |
| 329 |
}); |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
/** |
| 334 |
* Load images IDs |
| 335 |
* @param callback |
| 336 |
*/ |
| 337 |
$scope.getImagesMedia = function( callback, cont ) { |
| 338 |
|
| 339 |
$scope.continue = true; |
| 340 |
$scope.isLoading = true; |
| 341 |
$scope.status = 'Loading Images Media Objects...'; |
| 342 |
|
| 343 |
$http({ |
| 344 |
method: 'GET', |
| 345 |
url: ajaxurl, |
| 346 |
params: { |
| 347 |
action: 'get_images_media_ids', |
| 348 |
continue: cont |
| 349 |
} |
| 350 |
}).then(function(response){ |
| 351 |
var data = response.data || {}; |
| 352 |
|
| 353 |
if ( data.success ) { |
| 354 |
if ( typeof callback === 'function' ) { |
| 355 |
if ( typeof data.data !== 'undefined' ) { |
| 356 |
$scope.objectIDs = data.data; |
| 357 |
callback(); |
| 358 |
} else { |
| 359 |
$scope.status = 'IDs are malformed'; |
| 360 |
$scope.error = true; |
| 361 |
} |
| 362 |
} |
| 363 |
} else { |
| 364 |
$scope.status = $scope.getError(response, 'Unable to get Images Media ID'); |
| 365 |
$scope.error = true; |
| 366 |
} |
| 367 |
|
| 368 |
$scope.isLoading = false; |
| 369 |
|
| 370 |
if(WP_DEBUG){ |
| 371 |
console.log("WP-Stateless get Images Media ID:", response); |
| 372 |
} |
| 373 |
|
| 374 |
}, function(response) { |
| 375 |
$scope.error = true; |
| 376 |
$scope.status = $scope.getError(response, 'Get Images Media ID: Request failed'); |
| 377 |
$scope.isLoading = false; |
| 378 |
|
| 379 |
if(WP_DEBUG){ |
| 380 |
console.log("WP-Stateless get Images Media ID: Request failed", response, response.headers()); |
| 381 |
} |
| 382 |
}); |
| 383 |
|
| 384 |
}; |
| 385 |
|
| 386 |
/** |
| 387 |
* Load non-images media files |
| 388 |
* @param callback |
| 389 |
*/ |
| 390 |
$scope.getOtherMedia = function( callback, cont ) { |
| 391 |
|
| 392 |
$scope.continue = true; |
| 393 |
$scope.isLoading = true; |
| 394 |
$scope.status = 'Loading non-image Media Objects...'; |
| 395 |
|
| 396 |
$http({ |
| 397 |
method: 'GET', |
| 398 |
url: ajaxurl, |
| 399 |
params: { |
| 400 |
action: 'get_other_media_ids', |
| 401 |
continue: cont |
| 402 |
} |
| 403 |
}).then(function(response){ |
| 404 |
var data = response.data || {}; |
| 405 |
|
| 406 |
if ( data.success ) { |
| 407 |
if ( typeof callback === 'function' ) { |
| 408 |
if ( typeof data.data !== 'undefined' ) { |
| 409 |
$scope.objectIDs = data.data; |
| 410 |
callback(); |
| 411 |
} else { |
| 412 |
$scope.status = $scope.getError(response, 'IDs are malformed'); |
| 413 |
$scope.error = true; |
| 414 |
} |
| 415 |
} |
| 416 |
} else { |
| 417 |
$scope.status = $scope.getError(response, 'Unable to get non Images Media ID'); |
| 418 |
$scope.error = true; |
| 419 |
} |
| 420 |
|
| 421 |
$scope.isLoading = false; |
| 422 |
|
| 423 |
if(WP_DEBUG){ |
| 424 |
console.log("WP-Stateless get non Images Media ID:", response, response.headers()); |
| 425 |
} |
| 426 |
}, function(response) { |
| 427 |
$scope.error = true; |
| 428 |
$scope.status = $scope.getError(response, 'Get non Images Media ID: Request failed'); |
| 429 |
$scope.isLoading = false; |
| 430 |
if(WP_DEBUG){ |
| 431 |
console.log("WP-Stateless get non Images Media ID: Request failed", response, response.headers()); |
| 432 |
} |
| 433 |
}); |
| 434 |
|
| 435 |
}; |
| 436 |
|
| 437 |
/** |
| 438 |
* Load non-images media files |
| 439 |
* @param callback |
| 440 |
*/ |
| 441 |
$scope.getNonLibraryFiles = function( callback, cont ) { |
| 442 |
|
| 443 |
$scope.continue = true; |
| 444 |
$scope.isLoading = true; |
| 445 |
$scope.status = 'Loading non library Objects...'; |
| 446 |
|
| 447 |
$http({ |
| 448 |
method: 'GET', |
| 449 |
url: ajaxurl, |
| 450 |
params: { |
| 451 |
action: 'get_non_library_files_id', |
| 452 |
continue: cont |
| 453 |
} |
| 454 |
}).then(function(response){ |
| 455 |
var data = response.data || {}; |
| 456 |
|
| 457 |
if ( data.success ) { |
| 458 |
if ( typeof callback === 'function' ) { |
| 459 |
if ( typeof data.data !== 'undefined' ) { |
| 460 |
$scope.objectIDs = data.data; |
| 461 |
callback(); |
| 462 |
} else { |
| 463 |
$scope.status = $scope.getError(response, "IDs are malformed"); |
| 464 |
$scope.error = true; |
| 465 |
} |
| 466 |
} |
| 467 |
} else { |
| 468 |
$scope.error = true; |
| 469 |
$scope.status = $scope.getError(response, 'Non libraries files are not found'); |
| 470 |
} |
| 471 |
|
| 472 |
$scope.isLoading = false; |
| 473 |
|
| 474 |
if(WP_DEBUG){ |
| 475 |
console.log("WP-Stateless get non library files:", response, response.headers()); |
| 476 |
} |
| 477 |
}, function(response) { |
| 478 |
$scope.error = true; |
| 479 |
$scope.status = $scope.getError(response, 'Get non library files: Request failed'); |
| 480 |
$scope.isLoading = false; |
| 481 |
|
| 482 |
if(WP_DEBUG){ |
| 483 |
console.log("WP-Stateless get non library files: Request failed", response, response.headers()); |
| 484 |
} |
| 485 |
}); |
| 486 |
|
| 487 |
}; |
| 488 |
|
| 489 |
/** |
| 490 |
* Run sync for files |
| 491 |
*/ |
| 492 |
$scope.syncNonLibraryFiles = function() { |
| 493 |
$scope.isRunning = true; |
| 494 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 495 |
$scope.objectsCounter = 0; |
| 496 |
$scope.status = 'Processing files (' + $scope.objectsTotal + ' total)...'; |
| 497 |
|
| 498 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 499 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 500 |
|
| 501 |
if ( $scope.objectIDs.length ) { |
| 502 |
//$scope.syncSingleNonLibraryFile( $scope.objectIDs.shift() ); |
| 503 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 504 |
for ( var i in $scope.chunkIDs ) { |
| 505 |
$scope.syncSingleNonLibraryFile( $scope.chunkIDs[ i ].shift(), i ); |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Run sync for files |
| 512 |
*/ |
| 513 |
$scope.syncFiles = function() { |
| 514 |
$scope.isRunning = true; |
| 515 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 516 |
$scope.objectsCounter = 0; |
| 517 |
$scope.status = 'Processing files (' + $scope.objectsTotal + ' total)...'; |
| 518 |
|
| 519 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 520 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 521 |
|
| 522 |
if ( $scope.objectIDs.length ) { |
| 523 |
//$scope.syncSingleFile( $scope.objectIDs.shift() ); |
| 524 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 525 |
for ( var i in $scope.chunkIDs ) { |
| 526 |
$scope.syncSingleFile( $scope.chunkIDs[ i ].shift(), i ); |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Run images regeneration |
| 533 |
* @param ids |
| 534 |
*/ |
| 535 |
$scope.regenerateImages = function() { |
| 536 |
$scope.isRunning = true; |
| 537 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 538 |
$scope.objectsCounter = 0; |
| 539 |
$scope.status = 'Processing images (' + $scope.objectsTotal + ' total)...'; |
| 540 |
|
| 541 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 542 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 543 |
|
| 544 |
if ( $scope.objectIDs.length ) { |
| 545 |
//$scope.regenerateSingle( $scope.objectIDs.shift() ); |
| 546 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 547 |
for ( var i in $scope.chunkIDs ) { |
| 548 |
$scope.regenerateSingle( $scope.chunkIDs[ i ].shift(), i ); |
| 549 |
} |
| 550 |
} |
| 551 |
}; |
| 552 |
|
| 553 |
/** |
| 554 |
* Process Single Image |
| 555 |
* @param id |
| 556 |
*/ |
| 557 |
$scope.regenerateSingle = function( id, chunk_id ) { |
| 558 |
|
| 559 |
$http({ |
| 560 |
method: 'GET', |
| 561 |
url: ajaxurl, |
| 562 |
params: { |
| 563 |
action: "stateless_process_image", |
| 564 |
id: id |
| 565 |
} |
| 566 |
}).then( |
| 567 |
function(response) { |
| 568 |
var data = response.data || {}; |
| 569 |
$scope.log.push({message:data.data || "Regenerate single image: Failed"}); |
| 570 |
|
| 571 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 572 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 573 |
|
| 574 |
if(typeof response.data.success == 'undefined' || response.data.success == false){ |
| 575 |
$scope.error = true; |
| 576 |
$scope.status = $scope.getError(response, "Regenerate single image: Failed"); |
| 577 |
// $scope.isRunning = false; |
| 578 |
} |
| 579 |
else if ( 'undefined' !== typeof chunk_id ) { |
| 580 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 581 |
$scope.regenerateSingle( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 582 |
} else { |
| 583 |
$scope.finishProcess( chunk_id ); |
| 584 |
} |
| 585 |
} else { |
| 586 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 587 |
$scope.regenerateSingle( $scope.objectIDs.shift() ); |
| 588 |
} else { |
| 589 |
$scope.finishProcess(); |
| 590 |
} |
| 591 |
} |
| 592 |
if(WP_DEBUG){ |
| 593 |
console.log("WP-Stateless regenerate single image:", response, response.headers()); |
| 594 |
} |
| 595 |
}, |
| 596 |
function(response) { |
| 597 |
$scope.error = true; |
| 598 |
$scope.status = $scope.getError(response, "Regenerate single image: Request failed"); |
| 599 |
$scope.isRunning = false; |
| 600 |
if(WP_DEBUG){ |
| 601 |
console.log("WP-Stateless regenerate single image: Request failed", response, response.headers()); |
| 602 |
} |
| 603 |
} |
| 604 |
); |
| 605 |
|
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Process single file |
| 610 |
* @param id |
| 611 |
*/ |
| 612 |
$scope.syncSingleFile = function( id, chunk_id ) { |
| 613 |
|
| 614 |
$http({ |
| 615 |
method: 'GET', |
| 616 |
url: ajaxurl, |
| 617 |
params: { |
| 618 |
action: "stateless_process_file", |
| 619 |
id: id |
| 620 |
} |
| 621 |
}).then( |
| 622 |
function(response) { |
| 623 |
var data = response.data || {}; |
| 624 |
$scope.log.push({message:data.data || "Sync single file: Failed"}); |
| 625 |
|
| 626 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 627 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 628 |
|
| 629 |
if(typeof response.data.success == 'undefined' || response.data.success == false){ |
| 630 |
$scope.error = true; |
| 631 |
$scope.status = $scope.getError(response, "Sync single file: Failed"); |
| 632 |
// $scope.isRunning = false; |
| 633 |
} |
| 634 |
else if ( 'undefined' !== typeof chunk_id ) { |
| 635 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 636 |
$scope.syncSingleFile( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 637 |
} else { |
| 638 |
$scope.finishProcess( chunk_id ); |
| 639 |
} |
| 640 |
} else { |
| 641 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 642 |
$scope.syncSingleFile( $scope.objectIDs.shift() ); |
| 643 |
} else { |
| 644 |
$scope.finishProcess(); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
if(WP_DEBUG){ |
| 649 |
console.log("WP-Stateless sync single file:", response, response.headers()); |
| 650 |
} |
| 651 |
}, |
| 652 |
function(response) { |
| 653 |
$scope.error = true; |
| 654 |
$scope.status = $scope.getError(response, "Sync single file: Request failed"); |
| 655 |
$scope.isRunning = false; |
| 656 |
|
| 657 |
if(WP_DEBUG){ |
| 658 |
console.log("WP-Stateless sync single file: Request failed", response, response.headers()); |
| 659 |
} |
| 660 |
} |
| 661 |
); |
| 662 |
|
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* Process single file |
| 667 |
* @param id |
| 668 |
*/ |
| 669 |
$scope.syncSingleNonLibraryFile = function( id, chunk_id ) { |
| 670 |
|
| 671 |
$http({ |
| 672 |
method: 'GET', |
| 673 |
url: ajaxurl, |
| 674 |
params: { |
| 675 |
action: "stateless_process_non_library_file", |
| 676 |
file_path: id |
| 677 |
} |
| 678 |
}).then( |
| 679 |
function(response) { |
| 680 |
var data = response.data || {}; |
| 681 |
$scope.log.push({message:data.data || "Sync non library file: Failed"}); |
| 682 |
|
| 683 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 684 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 685 |
|
| 686 |
if(typeof response.data.success == 'undefined' || response.data.success == false){ |
| 687 |
$scope.error = true; |
| 688 |
$scope.status = $scope.getError(response, "Sync non library file: Failed"); |
| 689 |
// $scope.isRunning = false; |
| 690 |
} |
| 691 |
else if ( 'undefined' !== typeof chunk_id ) { |
| 692 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 693 |
$scope.syncSingleNonLibraryFile( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 694 |
} else { |
| 695 |
$scope.finishProcess( chunk_id ); |
| 696 |
} |
| 697 |
} else { |
| 698 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 699 |
$scope.syncSingleNonLibraryFile( $scope.objectIDs.shift() ); |
| 700 |
} else { |
| 701 |
$scope.finishProcess(); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
if(WP_DEBUG){ |
| 706 |
console.log("WP-Stateless sync non library file:", response, response.headers()); |
| 707 |
} |
| 708 |
}, |
| 709 |
function(response) { |
| 710 |
$scope.error = true; |
| 711 |
$scope.status = $scope.getError(response, "Sync non library file: Request failed"); |
| 712 |
$scope.isRunning = false; |
| 713 |
|
| 714 |
if(WP_DEBUG){ |
| 715 |
console.log("WP-Stateless sync non library file: Request failed", response, response.headers()); |
| 716 |
} |
| 717 |
} |
| 718 |
); |
| 719 |
|
| 720 |
} |
| 721 |
|
| 722 |
}]) |
| 723 |
.controller('wpStatelessSettings', function($scope, $filter) { |
| 724 |
$scope.backup = {}; |
| 725 |
$scope.sm = wp_stateless_settings || {}; |
| 726 |
|
| 727 |
$scope.$watch('sm.mode', function(value) { |
| 728 |
if(value == 'stateless'){ |
| 729 |
$scope.backup.hashify_file_name = $scope.sm.hashify_file_name; |
| 730 |
$scope.sm.hashify_file_name = 'true'; |
| 731 |
$scope.apply(); |
| 732 |
} |
| 733 |
else{ |
| 734 |
if($scope.backup.hashify_file_name){ |
| 735 |
$scope.sm.hashify_file_name = $scope.backup.hashify_file_name; |
| 736 |
$scope.apply(); |
| 737 |
} |
| 738 |
} |
| 739 |
}); |
| 740 |
|
| 741 |
$scope.sm.showNotice = function(option){ |
| 742 |
if($scope.sm.readonly && $scope.sm.readonly[option]){ |
| 743 |
var slug = $scope.sm.readonly[option]; |
| 744 |
return $scope.sm.strings[slug]; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
$scope.sm.generatePreviewUrl = function() { |
| 749 |
var host = 'https://storage.googleapis.com/'; |
| 750 |
var is_ssl = $scope.sm.custom_domain.indexOf('https://'); |
| 751 |
var custom_domain = $scope.sm.custom_domain; |
| 752 |
custom_domain = custom_domain.replace('https://', ''); |
| 753 |
custom_domain = custom_domain.replace('http://', ''); |
| 754 |
if ( $scope.sm.bucket && custom_domain == $scope.sm.bucket) { |
| 755 |
host = is_ssl === 0 ? 'https://' : 'http://'; // bucketname will be host |
| 756 |
} |
| 757 |
host += $scope.sm.bucket ? $scope.sm.bucket : '{bucket-name}'; |
| 758 |
var rootdir = $scope.sm.root_dir ? $scope.sm.root_dir + '/' : ''; |
| 759 |
var subdir = $scope.sm.organize_media == '1' ? $filter('date')(Date.now(), 'yyyy/MM') + '/' : ''; |
| 760 |
var hash = $scope.sm.hashify_file_name == 'true' ? Date.now().toString(36) + '-' : ''; |
| 761 |
$scope.sm.preview_url = host + "/" + rootdir + subdir + hash + "your-image-name.jpeg"; |
| 762 |
} |
| 763 |
|
| 764 |
$scope.sm.generatePreviewUrl(); |
| 765 |
|
| 766 |
}) |
| 767 |
.controller('wpStatelessCompatibility', function($scope, $filter) { |
| 768 |
$scope.modules = wp_stateless_compatibility || {}; |
| 769 |
}); |
| 770 |
|
| 771 |
wpStatelessApp.filter("trust", ['$sce', function($sce) { |
| 772 |
return function(htmlCode){ |
| 773 |
return $sce.trustAsHtml(htmlCode); |
| 774 |
} |
| 775 |
}]); |
| 776 |
|