hash every file, build the manifest, ask Cloudflare which hashes are missing * upload -> upload missing files in batches (can span several background requests) * finalize -> register hashes and create the deployment */ if ( ! defined( 'ABSPATH' ) ) { exit; } class CFSP_Deploy_Cloudflare { const MAX_FILE_SIZE = 26214400; // 25 MiB: Cloudflare Pages limit per asset. const BATCH_BYTES = 12582912; // ~12 MiB of raw data per upload request. const BATCH_FILES = 1000; /** @return array|WP_Error [ 'files' => int, 'missing' => int ] */ public static function prepare( $job ) { $cache = CFSP_Deployer::read_state( 'hash-cache' ); $overrides = CFSP_Deployer::read_state( 'content-types' ); $manifest = array(); $files = array(); $new_cache = array(); foreach ( CFSP_Deployer::files() as $rel => $file ) { $size = $file->getSize(); if ( $size > self::MAX_FILE_SIZE ) { CFSP_Logger::error( "Skipped {$rel}: larger than 25 MiB (Cloudflare Pages limit)." ); continue; } $ext = strtolower( pathinfo( $rel, PATHINFO_EXTENSION ) ); $type = $overrides[ $rel ] ?? ( CFSP_Exporter::MIME[ $ext ] ?? 'application/octet-stream' ); $mtime = $file->getMTime(); if ( isset( $cache[ $rel ] ) && $cache[ $rel ][0] === $size && $cache[ $rel ][1] === $mtime && $cache[ $rel ][3] === $type ) { $hash = $cache[ $rel ][2]; } else { $hash = md5( base64_encode( file_get_contents( $file->getPathname() ) ) . $ext . $type ); } $new_cache[ $rel ] = array( $size, $mtime, $hash, $type ); $manifest[ '/' . $rel ] = $hash; $files[ $hash ] = array( $rel, $type, $size ); } if ( ! $manifest ) { return new WP_Error( 'cfsp_empty', 'Nothing to deploy: the export directory is empty.' ); } CFSP_Deployer::write_state( 'hash-cache', $new_cache ); CFSP_Deployer::write_state( 'manifest', $manifest ); $cf = new CFSP_Cloudflare(); $missing = $cf->check_missing( array_keys( $files ) ); if ( is_wp_error( $missing ) ) { return $missing; } $queue = array(); foreach ( $missing as $hash ) { if ( isset( $files[ $hash ] ) ) { $queue[] = array( $hash, $files[ $hash ][0], $files[ $hash ][1], $files[ $hash ][2] ); } } CFSP_Deployer::write_state( 'upload-queue', $queue ); return array( 'files' => count( $manifest ), 'missing' => count( $queue ), ); } /** @return int|WP_Error Remaining files in the queue. */ public static function upload( $deadline ) { $queue = CFSP_Deployer::read_state( 'upload-queue' ); $root = trailingslashit( CFSP_Settings::export_dir() ); $cf = new CFSP_Cloudflare(); while ( $queue && time() < $deadline ) { $batch = array(); $bytes = 0; while ( $queue && count( $batch ) < self::BATCH_FILES ) { list( $hash, $rel, $type, $size ) = $queue[0]; if ( $batch && $bytes + $size > self::BATCH_BYTES ) { break; } array_shift( $queue ); if ( ! is_file( $root . $rel ) ) { continue; } $batch[] = array( 'hash' => $hash, 'base64' => base64_encode( file_get_contents( $root . $rel ) ), 'type' => $type, ); $bytes += $size; } if ( ! $batch ) { continue; } $result = $cf->upload( $batch ); if ( is_wp_error( $result ) ) { return $result; } CFSP_Logger::log( sprintf( 'Uploaded %d file(s) (%s).', count( $batch ), size_format( $bytes ) ) ); CFSP_Deployer::write_state( 'upload-queue', $queue ); unset( $batch ); } CFSP_Deployer::write_state( 'upload-queue', $queue ); return count( $queue ); } /** @return array|WP_Error [ 'url' => ..., 'id' => ... ] */ public static function finalize() { $manifest = CFSP_Deployer::read_state( 'manifest' ); if ( ! $manifest ) { return new WP_Error( 'cfsp_restart', 'Manifest missing.' ); } $cf = new CFSP_Cloudflare(); $upsert = $cf->upsert_hashes( array_values( array_unique( $manifest ) ) ); if ( is_wp_error( $upsert ) ) { return $upsert; } $result = $cf->create_deployment( $manifest, CFSP_Deployer::headers_file(), CFSP_Deployer::redirects_file() ); if ( is_wp_error( $result ) ) { return $result; } return array( 'url' => $result['url'] ?? '', 'id' => $result['id'] ?? '', ); } }