assertNoResetRunning(); $size = $request->int( 'size' ); $filename = sanitize_file_name( $request->str( 'filename' ) ); if ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ) !== MIGRATEX_ARCHIVE_EXT ) { throw new HttpException( 'om_bad_extension', sprintf( /* translators: %s: expected file extension */ esc_html__( 'Only .%s archives created by this plugin can be imported.', 'migratex' ), esc_html( MIGRATEX_ARCHIVE_EXT ) ), 415 ); } if ( $size < 1 ) { throw new HttpException( 'om_bad_size', esc_html__( 'Archive size is invalid.', 'migratex' ), 400 ); } // Upload copy + extracted staging copy + headroom for the database dump. $free = Environment::freeDiskSpace( Paths::jobs() ); // null when the host hides it: the check is skipped. if ( null !== $free && $free < $size * 2.5 ) { throw new HttpException( 'om_disk_full', esc_html__( 'Not enough free disk space to restore this archive.', 'migratex' ), 507 ); } $job = $this->store->findActive( 'import', get_current_user_id() ); if ( null !== $job ) { throw new HttpException( 'om_import_active', esc_html__( 'An import is already in progress. Cancel it before starting another.', 'migratex' ), 409 ); } // The piece size is fixed now and stored with the job, so the server checks every piece against // the same size the browser was told to cut the file into. $chunk_size = Environment::chunkBytes(); $rules = $this->customRules( $request ); // refuses the request, with the reason, if a rule cannot be used. $job = $this->store->create( 'import', get_current_user_id(), array( 'filename' => $filename, 'size' => $size, 'total_chunks' => (int) ceil( $size / $chunk_size ), 'chunk_size' => $chunk_size, 'custom_replacements' => $rules, ) ); return Response::job( $job ); } /** * Starts a restore from an archive already in the backups folder, so nothing has to be * uploaded again. The browser only names the file; the path comes from Library, which * accepts nothing outside the folder. The job then follows exactly the same stages, * and the same atomic database swap, as an uploaded import. */ public function fromBackup( Request $request ): Response { $this->assertNoResetRunning(); $name = $request->str( 'file' ); $path = Library::path( $name ); if ( null === $path ) { throw new HttpException( 'om_backup_not_found', esc_html__( 'That backup no longer exists.', 'migratex' ), 404 ); } if ( null !== $this->store->findActive( 'import', get_current_user_id() ) ) { throw new HttpException( 'om_import_active', esc_html__( 'An import is already in progress. Cancel it before starting another.', 'migratex' ), 409 ); } $size = (int) filesize( $path ); // Extraction needs roughly the archive's size again, plus the database. $free = Environment::freeDiskSpace( Paths::jobs() ); // null when the host hides it: the check is skipped. if ( null !== $free && $free < $size * 1.5 ) { throw new HttpException( 'om_disk_full', esc_html__( 'Not enough free disk space to restore this backup.', 'migratex' ), 507 ); } $job = $this->store->create( 'import', get_current_user_id(), array( 'filename' => $name, 'size' => $size, 'archive_path' => $path, 'source' => 'backup', 'custom_replacements' => $this->customRules( $request ), ) ); // There is no upload step: begin at verification. $job->advanceTo( 'assemble' ); $job->setLabel( __( 'Verifying archive…', 'migratex' ) ); $this->store->save( $job ); return Response::job( $job ); } /** * Receives one slice. Metadata travels in headers because the body is the raw bytes: * * X-OM-Chunk-Index zero-based position * X-OM-Chunk-Total total chunk count (must match what init computed) * X-OM-Chunk-Hash SHA-256 of this chunk's bytes * * Re-sending an already stored index is a no-op, which makes client retries safe. */ public function chunk( Request $request ): Response { $job = $this->ownedJob( $request, 'import' ); $index = $request->header( 'X-OM-Chunk-Index' ); $total = $request->header( 'X-OM-Chunk-Total' ); $hash = strtolower( $request->header( 'X-OM-Chunk-Hash' ) ); if ( ! ctype_digit( $index ) || ! ctype_digit( $total ) || 1 !== preg_match( '/^[a-f0-9]{64}$/', $hash ) ) { throw new HttpException( 'om_bad_chunk_headers', esc_html__( 'Missing or malformed chunk headers.', 'migratex' ), 400 ); } $body = $request->body(); if ( '' === $body ) { throw new HttpException( 'om_empty_chunk', esc_html__( 'Chunk body is empty.', 'migratex' ), 400 ); } if ( strlen( $body ) > (int) $job->get( 'chunk_size', MIGRATEX_CHUNK_SIZE ) ) { throw new HttpException( 'om_chunk_too_large', esc_html__( 'Chunk exceeds the maximum size.', 'migratex' ), 413 ); } $receiver = new ChunkReceiver( $this->store ); return Response::job( $receiver->receive( $job, (int) $index, (int) $total, $hash, $body ) ); } public function tick( Request $request ): Response { $job = $this->ownedJob( $request, 'import' ); return Response::job( $this->runner->tick( $job ) ); } }