store = $store; } /** * Checks one chunk against the job's declared upload: right place, right length, right SHA-256. * * @throws HttpException */ public function validate( JobState $job, int $index, int $total, string $hash, string $body ): void { $expected_total = (int) $job->get( 'total_chunks', 0 ); $chunk_size = (int) $job->get( 'chunk_size', MIGRATEX_CHUNK_SIZE ); $size = (int) $job->get( 'size', 0 ); if ( $total !== $expected_total || $index >= $expected_total ) { throw new HttpException( 'om_chunk_range', esc_html__( 'Chunk index does not match the declared upload.', 'migratex' ), 400 ); } $expected_length = $index < $expected_total - 1 ? $chunk_size : $size - ( $expected_total - 1 ) * $chunk_size; if ( strlen( $body ) !== $expected_length ) { throw new HttpException( 'om_chunk_length', esc_html__( 'Chunk has an unexpected size.', 'migratex' ), 400 ); } if ( ! hash_equals( $hash, hash( 'sha256', $body ) ) ) { // 422 so the client knows to simply send this chunk again. throw new HttpException( 'om_chunk_corrupt', esc_html__( 'Chunk failed its integrity check during transfer.', 'migratex' ), 422 ); } } /** * @throws HttpException */ public function receive( JobState $job, int $index, int $total, string $hash, string $body ): JobState { $this->validate( $job, $index, $total, $hash, $body ); $lock = $this->store->acquire( $job, 5000 ); if ( null === $lock ) { throw new HttpException( 'om_busy', esc_html__( 'The upload is busy. Retrying.', 'migratex' ), 503 ); } try { $fresh = $this->store->load( $job->id() ); if ( null === $fresh || $fresh->isTerminal() ) { throw new HttpException( 'om_job_closed', esc_html__( 'This import is no longer active.', 'migratex' ), 409 ); } if ( 'upload' !== $fresh->stage() ) { // The final chunk may have been stored while its response was lost, so the // client retries it. Nothing is written; just report where the job is. if ( $fresh->get( 'upload_complete' ) ) { return $fresh; } throw new HttpException( 'om_upload_closed', esc_html__( 'The upload has already finished.', 'migratex' ), 409 ); } $this->write( $fresh, $index, $body, true ); $this->store->save( $fresh ); return $fresh; } finally { $this->store->release( $lock ); } } /** * For a caller that already holds the job's lock and saves the job itself (the pull from another site * runs inside a tick). Same checks, same write, same bookkeeping as an upload from the browser; the job * is left in its stage so the caller decides when to move on. * * @throws HttpException */ public function accept( JobState $job, int $index, int $total, string $hash, string $body ): void { $this->validate( $job, $index, $total, $hash, $body ); $this->write( $job, $index, $body, false ); } /** * Puts the chunk at its own offset and records it. * * @throws HttpException */ private function write( JobState $job, int $index, string $body, bool $advance ): void { $expected_total = (int) $job->get( 'total_chunks', 0 ); $chunk_size = (int) $job->get( 'chunk_size', MIGRATEX_CHUNK_SIZE ); $path = Paths::job( $job->id() ) . '/upload.part'; $handle = fopen( $path, 'c+b' ); if ( false === $handle ) { throw new HttpException( 'om_write', esc_html__( 'Cannot write the uploaded data.', 'migratex' ), 500 ); } try { fseek( $handle, $index * $chunk_size ); SafeFile::writeAll( $handle, $body ); fflush( $handle ); } catch ( \RuntimeException $e ) { throw new HttpException( 'om_disk_full', esc_html__( 'Not enough disk space to store the upload.', 'migratex' ), 507 ); } finally { fclose( $handle ); } $map = str_pad( (string) $job->cursor( 'map', '' ), $expected_total, '0' ); $map[ $index ] = '1'; $received = substr_count( $map, '1' ); $job->setCursor( 'map', $map ); $job->setStageProgress( $received / $expected_total ); $job->setDetail( array( 'chunks' => $received, 'total_chunks' => $expected_total, 'bytes' => min( (int) $job->get( 'size', 0 ), $received * $chunk_size ), 'total_bytes' => (int) $job->get( 'size', 0 ), 'remote' => (bool) $job->get( 'remote_url' ), ) ); if ( $received === $expected_total ) { $job->set( 'upload_complete', true ); $job->set( 'chunks_received', $received ); if ( $advance ) { $job->advanceTo( 'assemble' ); $job->setLabel( __( 'Verifying archive…', 'migratex' ) ); } } } }