AssembleUploadChunksJob.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Assemble the segments of a chunked upload.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Upload
  22. */
  23. use Wikimedia\ScopedCallback;
  24. /**
  25. * Assemble the segments of a chunked upload.
  26. *
  27. * @ingroup Upload
  28. */
  29. class AssembleUploadChunksJob extends Job {
  30. public function __construct( Title $title, array $params ) {
  31. parent::__construct( 'AssembleUploadChunks', $title, $params );
  32. $this->removeDuplicates = true;
  33. }
  34. public function run() {
  35. $scope = RequestContext::importScopedSession( $this->params['session'] );
  36. $this->addTeardownCallback( function () use ( &$scope ) {
  37. ScopedCallback::consume( $scope ); // T126450
  38. } );
  39. $context = RequestContext::getMain();
  40. $user = $context->getUser();
  41. try {
  42. if ( !$user->isLoggedIn() ) {
  43. $this->setLastError( "Could not load the author user from session." );
  44. return false;
  45. }
  46. UploadBase::setSessionStatus(
  47. $user,
  48. $this->params['filekey'],
  49. [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ]
  50. );
  51. $upload = new UploadFromChunks( $user );
  52. $upload->continueChunks(
  53. $this->params['filename'],
  54. $this->params['filekey'],
  55. new WebRequestUpload( $context->getRequest(), 'null' )
  56. );
  57. // Combine all of the chunks into a local file and upload that to a new stash file
  58. $status = $upload->concatenateChunks();
  59. if ( !$status->isGood() ) {
  60. UploadBase::setSessionStatus(
  61. $user,
  62. $this->params['filekey'],
  63. [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
  64. );
  65. $this->setLastError( $status->getWikiText( false, false, 'en' ) );
  66. return false;
  67. }
  68. // We can only get warnings like 'duplicate' after concatenating the chunks
  69. $status = Status::newGood();
  70. $status->value = [
  71. 'warnings' => UploadBase::makeWarningsSerializable( $upload->checkWarnings() )
  72. ];
  73. // We have a new filekey for the fully concatenated file
  74. $newFileKey = $upload->getStashFile()->getFileKey();
  75. // Remove the old stash file row and first chunk file
  76. $upload->stash->removeFileNoAuth( $this->params['filekey'] );
  77. // Build the image info array while we have the local reference handy
  78. $apiMain = new ApiMain(); // dummy object (XXX)
  79. $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
  80. // Cleanup any temporary local file
  81. $upload->cleanupTempFile();
  82. // Cache the info so the user doesn't have to wait forever to get the final info
  83. UploadBase::setSessionStatus(
  84. $user,
  85. $this->params['filekey'],
  86. [
  87. 'result' => 'Success',
  88. 'stage' => 'assembling',
  89. 'filekey' => $newFileKey,
  90. 'imageinfo' => $imageInfo,
  91. 'status' => $status
  92. ]
  93. );
  94. } catch ( Exception $e ) {
  95. UploadBase::setSessionStatus(
  96. $user,
  97. $this->params['filekey'],
  98. [
  99. 'result' => 'Failure',
  100. 'stage' => 'assembling',
  101. 'status' => Status::newFatal( 'api-error-stashfailed' )
  102. ]
  103. );
  104. $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
  105. // To be extra robust.
  106. MWExceptionHandler::rollbackMasterChangesAndLog( $e );
  107. return false;
  108. }
  109. return true;
  110. }
  111. public function getDeduplicationInfo() {
  112. $info = parent::getDeduplicationInfo();
  113. if ( is_array( $info['params'] ) ) {
  114. $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
  115. }
  116. return $info;
  117. }
  118. public function allowRetries() {
  119. return false;
  120. }
  121. }