Plugin Directory

Changeset 3820862


Ignore:
Timestamp:
11/24/2026 03:53:06 PM (66 minutes ago)
Author:
indigetal
Message:

Update to version 1.1.2

Server-side Stream and Storage uploads send the local file from disk.

Location:
indigetal-media-offload-for-bunny-net
Files:
9 edited
8 copied

Legend:

Unmodified
Added
Removed
  • indigetal-media-offload-for-bunny-net/tags/1.1.2/includes/Admin/BunnySettings.php

    r3780520 r3820862  
    6868     * @var string
    6969     */
    70     private const PRO_COMPANION_INFO_URL = 'https://indigetal.com/';
     70    private const PRO_COMPANION_INFO_URL = 'https://indigetal.com/indigetal-offload-media-for-bunny-net/';
    7171
    7272    /**
  • indigetal-media-offload-for-bunny-net/tags/1.1.2/includes/Bunny/BunnyStorageClient.php

    r3575069 r3820862  
    2121    const DOWNLOAD_TIMEOUT = 120;
    2222    const MAX_ATTEMPTS = 3;
     23
     24    /**
     25     * Byte size where the direct Storage PUT adds Expect: 100-continue.
     26     */
     27    private const DIRECT_UPLOAD_EXPECT_THRESHOLD = 1048576;
    2328
    2429    /**
    … …  
    145150        }
    146151
    147         $body = file_get_contents($local_path);
    148 
    149         if (false === $body) {
     152        clearstatcache(true, $local_path);
     153
     154        if (!is_file($local_path)) {
    150155            return new \WP_Error(
    151156                'indigetal_offload_storage_read_failed',
    … …  
    157162        }
    158163
     164        $file_size = filesize($local_path);
     165
     166        if (false === $file_size) {
     167            return new \WP_Error(
     168                'indigetal_offload_storage_read_failed',
     169                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     170                [
     171                    'local_path' => $local_path,
     172                ]
     173            );
     174        }
     175
    159176        $headers = $this->getDefaultHeaders();
    160         $checksum = $this->calculateFileChecksum($local_path);
    161 
    162         if ('' !== $checksum) {
    163             $headers['Checksum'] = $checksum;
    164         }
    165 
    166177        $filetype = wp_check_filetype(basename($local_path));
    167178
    … …  
    173184            'method'      => 'PUT',
    174185            'headers'     => $headers,
    175             'body'        => $body,
    176186            'timeout'     => self::UPLOAD_TIMEOUT,
    177187            'redirection' => 0,
    … …  
    227237
    228238    /**
     239     * PUT a local file to Bunny Storage from disk.
     240     *
     241     * @param string               $request_url Absolute request URL.
     242     * @param string               $local_path  Absolute local file path.
     243     * @param array<string,string> $headers     Stable request headers.
     244     * @return array|\WP_Error
     245     */
     246    private function requestDirectStoragePut($request_url, $local_path, array $headers) {
     247        $request_headers = $headers;
     248        clearstatcache(true, $local_path);
     249
     250        if (!is_file($local_path)) {
     251            return new \WP_Error(
     252                'indigetal_offload_storage_read_failed',
     253                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     254                [
     255                    'local_path' => $local_path,
     256                ]
     257            );
     258        }
     259
     260        $file_size = filesize($local_path);
     261
     262        if (false === $file_size) {
     263            return new \WP_Error(
     264                'indigetal_offload_storage_read_failed',
     265                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     266                [
     267                    'local_path' => $local_path,
     268                ]
     269            );
     270        }
     271
     272        $checksum = $this->calculateFileChecksum($local_path);
     273
     274        if ('' !== $checksum) {
     275            $request_headers['Checksum'] = $checksum;
     276        }
     277
     278        $file_handle = fopen($local_path, 'rb');
     279
     280        if (false === $file_handle) {
     281            return new \WP_Error(
     282                'indigetal_offload_storage_read_failed',
     283                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     284                [
     285                    'local_path' => $local_path,
     286                ]
     287            );
     288        }
     289
     290        $request_headers['Content-Length'] = (string) $file_size;
     291
     292        if ($file_size >= self::DIRECT_UPLOAD_EXPECT_THRESHOLD) {
     293            $request_headers['Expect'] = '100-continue';
     294        }
     295
     296        $sent_file = false;
     297        $callback = function ($handle, $request_args, $url) use ($request_url, $file_handle, $file_size, &$sent_file) {
     298            if ($url !== $request_url) {
     299                return;
     300            }
     301
     302            curl_setopt($handle, \CURLOPT_POSTFIELDS, null);
     303            curl_setopt($handle, \CURLOPT_UPLOAD, true);
     304            curl_setopt($handle, \CURLOPT_INFILE, $file_handle);
     305            $infile_size_option = \CURLOPT_INFILESIZE;
     306            if (defined('CURLOPT_INFILESIZE_LARGE')) {
     307                $infile_size_option = \CURLOPT_INFILESIZE_LARGE;
     308            }
     309            curl_setopt($handle, $infile_size_option, $file_size);
     310            $sent_file = true;
     311        };
     312
     313        add_action('http_api_curl', $callback, PHP_INT_MAX, 3);
     314
     315        try {
     316            $response = wp_remote_request($request_url, [
     317                'method'      => 'PUT',
     318                'headers'     => $request_headers,
     319                'timeout'     => self::UPLOAD_TIMEOUT,
     320                'redirection' => 0,
     321            ]);
     322        } finally {
     323            remove_action('http_api_curl', $callback, PHP_INT_MAX);
     324            fclose($file_handle);
     325        }
     326
     327        if (false === $sent_file && is_wp_error($response)) {
     328            return $response;
     329        }
     330
     331        if (false === $sent_file) {
     332            return new \WP_Error(
     333                'indigetal_offload_storage_upload_not_sent',
     334                __('The storage upload received a response, but the local file was not sent.', 'indigetal-media-offload-for-bunny-net'),
     335                [
     336                    'local_path' => $local_path,
     337                ]
     338            );
     339        }
     340
     341        return $response;
     342    }
     343
     344    /**
    229345     * Upload a local file to Bunny Storage.
    230346     *
    … …  
    254370        BunnyLogger::log("Uploading file to Bunny Storage: {$remote_path}", 'info');
    255371
    256         return $this->requestWithRetry($request_url, $request_args, $remote_path, 'upload', 201);
     372        $attempt_request = function () use ($request_url, $local_path, $request_args) {
     373            return $this->requestDirectStoragePut($request_url, $local_path, $request_args['headers']);
     374        };
     375
     376        return $this->requestWithRetry($request_url, $request_args, $remote_path, 'upload', 201, $attempt_request);
    257377    }
    258378
    … …  
    378498     * @param string $operation     Human-readable operation label.
    379499     * @param int    $success_code  Expected success HTTP status.
     500     * @param callable|null $attempt_request Optional per-attempt request callback.
    380501     * @return array|\WP_Error
    381502     */
    382     private function requestWithRetry($request_url, array $request_args, $remote_path, $operation, $success_code) {
     503    private function requestWithRetry($request_url, array $request_args, $remote_path, $operation, $success_code, $attempt_request = null) {
    383504        $attempt = 0;
    384505        $last_error = null;
    … …  
    388509            $this->maybeWaitForRetryAfter();
    389510
    390             $response = wp_remote_request($request_url, $request_args);
     511            if (is_callable($attempt_request)) {
     512                $response = $attempt_request();
     513            } else {
     514                $response = wp_remote_request($request_url, $request_args);
     515            }
    391516
    392517            if (is_wp_error($response)) {
  • indigetal-media-offload-for-bunny-net/tags/1.1.2/includes/Bunny/BunnyVideoHandler.php

    r3802365 r3820862  
    2121     */
    2222    private const MP4_FALLBACK_MAX_HEIGHT = 720;
    23     private const DIRECT_UPLOAD_MEMORY_SAFETY_BUFFER = 16777216; // 16 MiB.
     23    /**
     24     * Byte size where the direct Stream PUT adds Expect: 100-continue.
     25     */
     26    private const DIRECT_UPLOAD_EXPECT_THRESHOLD = 1048576;
    2427    /**
    2528     * Bunny-compatible subset of WordPress default video MIME types for Media Library uploads.
    … …  
    124127        $uploadEndpoint = "library/{$library_id}/videos/{$videoId}";
    125128
    126         $videoData = file_get_contents($filePath);
    127         if ($videoData === false || strlen($videoData) === 0) {
     129        clearstatcache(true, $filePath);
     130        $fileSize = filesize($filePath);
     131        if (false === $fileSize || 0 === $fileSize) {
    128132            BunnyLogger::log("uploadVideo: Failed to read video file for {$filePath}.", 'error');
    129133            $readError = new \WP_Error('video_file_read_failed', __('Failed to read the video file before uploading.', 'indigetal-media-offload-for-bunny-net'));
    … …  
    132136        }
    133137
    134         $uploadResponse = $this->apiClient->executeWithRetry(function() use ($uploadEndpoint, $videoData) {
    135             return wp_remote_request($this->video_base_url . $uploadEndpoint, [
    136                 'method'    => 'PUT',
    137                 'headers'   => [
    138                     'AccessKey'    => $this->apiClient->getAccessKey(),
    139                     'Accept'       => 'application/json',
    140                     'Content-Type' => 'application/octet-stream'
    141                 ],
    142                 'body'      => $videoData,
    143                 'timeout'   => 300,
    144             ]);
     138        $uploadResponse = $this->apiClient->executeWithRetry(function() use ($uploadEndpoint, $filePath, $fileSize) {
     139            return $this->requestDirectVideoPut($this->video_base_url . $uploadEndpoint, $filePath, (int) $fileSize);
    145140        });
    146141
    … …  
    209204
    210205    /**
    211      * Ensure the direct PUT upload path has enough PHP memory to read the file.
     206     * PUT a local video file to Bunny Stream from disk.
     207     *
     208     * @param string $requestUrl Absolute request URL.
     209     * @param string $filePath   Absolute local video path.
     210     * @param int    $fileSize   File size in bytes.
     211     * @return array|\WP_Error
     212     */
     213    private function requestDirectVideoPut($requestUrl, $filePath, $fileSize) {
     214        $fileHandle = fopen($filePath, 'rb');
     215        if (false === $fileHandle) {
     216            return new \WP_Error(
     217                'video_file_read_failed',
     218                __('Failed to read the video file before uploading.', 'indigetal-media-offload-for-bunny-net')
     219            );
     220        }
     221
     222        $sentFile = false;
     223        $callback = function ($handle, $requestArgs, $url) use ($requestUrl, $fileHandle, $fileSize, &$sentFile) {
     224            if ($url !== $requestUrl) {
     225                return;
     226            }
     227
     228            curl_setopt($handle, \CURLOPT_POSTFIELDS, null);
     229            curl_setopt($handle, \CURLOPT_UPLOAD, true);
     230            curl_setopt($handle, \CURLOPT_INFILE, $fileHandle);
     231            $infileSizeOption = \CURLOPT_INFILESIZE;
     232            if (defined('CURLOPT_INFILESIZE_LARGE')) {
     233                $infileSizeOption = \CURLOPT_INFILESIZE_LARGE;
     234            }
     235            curl_setopt($handle, $infileSizeOption, $fileSize);
     236            $sentFile = true;
     237        };
     238
     239        $headers = [
     240            'AccessKey'      => $this->apiClient->getAccessKey(),
     241            'Accept'         => 'application/json',
     242            'Content-Type'   => 'application/octet-stream',
     243            'Content-Length' => (string) $fileSize,
     244        ];
     245
     246        if ($fileSize >= self::DIRECT_UPLOAD_EXPECT_THRESHOLD) {
     247            $headers['Expect'] = '100-continue';
     248        }
     249
     250        add_action('http_api_curl', $callback, PHP_INT_MAX, 3);
     251
     252        try {
     253            $response = wp_remote_request($requestUrl, [
     254                'method'      => 'PUT',
     255                'headers'     => $headers,
     256                'timeout'     => 300,
     257                'redirection' => 0,
     258            ]);
     259        } finally {
     260            remove_action('http_api_curl', $callback, PHP_INT_MAX);
     261            fclose($fileHandle);
     262        }
     263
     264        if (false === $sentFile && is_wp_error($response)) {
     265            return $response;
     266        }
     267
     268        if (false === $sentFile) {
     269            return new \WP_Error(
     270                'video_upload_failed',
     271                __('The video upload did not send the local file.', 'indigetal-media-offload-for-bunny-net')
     272            );
     273        }
     274
     275        return $response;
     276    }
     277
     278    /**
     279     * Confirm the local video has a readable file size before the direct PUT.
    212280     *
    213281     * @param string $filePath Absolute local video path.
    … …  
    217285        clearstatcache(true, $filePath);
    218286
    219         $fileSize = filesize($filePath);
    220         if (false === $fileSize) {
     287        if (false === filesize($filePath)) {
    221288            return new \WP_Error(
    222289                'video_file_size_unavailable',
    … …  
    225292        }
    226293
    227         $memoryLimit = $this->getMemoryLimitBytes();
    228         if ($memoryLimit <= 0) {
    229             return true;
    230         }
    231 
    232         $currentUsage = memory_get_usage(true);
    233         $remainingMemory = max(0, $memoryLimit - $currentUsage);
    234         $requiredMemory = (int) $fileSize + self::DIRECT_UPLOAD_MEMORY_SAFETY_BUFFER;
    235 
    236         if ($remainingMemory >= $requiredMemory) {
    237             return true;
    238         }
    239 
    240         return new \WP_Error(
    241             'video_direct_upload_memory_limit_exceeded',
    242             sprintf(
    243                 /* translators: 1: video file size, 2: remaining PHP memory, 3: required PHP memory */
    244                 __('This video is too large for the server-side Stream upload path. File size: %1$s. Available PHP memory: %2$s. Required memory: %3$s. Reduce the file size, raise the PHP memory limit, or upload from an environment with a higher memory ceiling.', 'indigetal-media-offload-for-bunny-net'),
    245                 size_format((int) $fileSize),
    246                 size_format((int) $remainingMemory),
    247                 size_format((int) $requiredMemory)
    248             ),
    249             [
    250                 'file_size'        => (int) $fileSize,
    251                 'memory_limit'     => (int) $memoryLimit,
    252                 'memory_usage'     => (int) $currentUsage,
    253                 'remaining_memory' => (int) $remainingMemory,
    254                 'required_memory'  => (int) $requiredMemory,
    255             ]
    256         );
    257     }
    258 
    259     /**
    260      * Return the configured PHP memory limit in bytes.
    261      *
    262      * @return int
    263      */
    264     private function getMemoryLimitBytes() {
    265         $memoryLimit = ini_get('memory_limit');
    266 
    267         if (false === $memoryLimit || '' === $memoryLimit) {
    268             return 0;
    269         }
    270 
    271         return (int) wp_convert_hr_to_bytes($memoryLimit);
     294        return true;
    272295    }
    273296
  • indigetal-media-offload-for-bunny-net/tags/1.1.2/indigetal-media-offload-for-bunny-net.php

    r3802365 r3820862  
    44 * Plugin URI: https://wordpress.org/plugins/indigetal-media-offload-for-bunny-net/
    55 * Description: Seamlessly offload the WordPress Media Library to Bunny Storage and stream videos from Bunny Stream.
    6  * Version: 1.1.1
     6 * Version: 1.1.2
    77 * Requires at least: 6.5
    88 * Requires PHP: 8.0
    99 * Author: Indigetal WebCraft
    10  * Author URI: https://indigetal.com
     10 * Author URI: https://indigetal.com/indigetal-offload-media-for-bunny-net/
    1111 * License: GPL-2.0-or-later
    1212 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    2121 * Semantic version of the Free plugin runtime (keep in sync with the Version header).
    2222 */
    23 define('INDIGETAL_OFFLOAD_VERSION', '1.1.1');
     23define('INDIGETAL_OFFLOAD_VERSION', '1.1.2');
    2424
    2525if (!function_exists('indigetal_offload_free_version')) {
  • indigetal-media-offload-for-bunny-net/tags/1.1.2/languages/indigetal-media-offload-for-bunny-net.pot

    r3575069 r3820862  
    3333#. Author URI of the plugin
    3434#: indigetal-media-offload-for-bunny-net.php
    35 msgid "https://indigetal.com"
     35msgid "https://indigetal.com/indigetal-offload-media-for-bunny-net/"
    3636msgstr ""
    3737
  • indigetal-media-offload-for-bunny-net/tags/1.1.2/readme.txt

    r3802365 r3820862  
    55Tested up to: 7.1
    66Requires PHP: 8.0
    7 Stable tag: 1.1.1
     7Stable tag: 1.1.2
    88License: GPL-2.0-or-later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    133133== Changelog ==
    134134
     135= 1.1.2 =
     136* Server-side Stream uploads send the local video from disk instead of loading the whole file into PHP memory. An unreadable file size still fails.
     137* Server-side Storage uploads send the local file from disk on each attempt instead of loading the whole file into PHP memory. An empty file still uploads. A missing file, a directory, or an unreadable file size still fails before any request.
     138
    135139= 1.1.1 =
    136140* Stream MP4 URLs use the highest stored rendition at or below 720p. The list is `_indigetal_offload_video_available_resolutions`, stored after encoding finishes. Until then the file stays `play_720p.mp4`.
    … …  
    158162== Upgrade Notice ==
    159163
     164= 1.1.2 =
     165Server-side Stream and Storage uploads read the local file from disk. No settings migration required.
     166
    160167= 1.1.1 =
    161168Stream MP4 renditions and core Video URL refresh. No settings migration required.
  • indigetal-media-offload-for-bunny-net/trunk/includes/Admin/BunnySettings.php

    r3780520 r3820862  
    6868     * @var string
    6969     */
    70     private const PRO_COMPANION_INFO_URL = 'https://indigetal.com/';
     70    private const PRO_COMPANION_INFO_URL = 'https://indigetal.com/indigetal-offload-media-for-bunny-net/';
    7171
    7272    /**
  • indigetal-media-offload-for-bunny-net/trunk/includes/Bunny/BunnyStorageClient.php

    r3575069 r3820862  
    2121    const DOWNLOAD_TIMEOUT = 120;
    2222    const MAX_ATTEMPTS = 3;
     23
     24    /**
     25     * Byte size where the direct Storage PUT adds Expect: 100-continue.
     26     */
     27    private const DIRECT_UPLOAD_EXPECT_THRESHOLD = 1048576;
    2328
    2429    /**
    … …  
    145150        }
    146151
    147         $body = file_get_contents($local_path);
    148 
    149         if (false === $body) {
     152        clearstatcache(true, $local_path);
     153
     154        if (!is_file($local_path)) {
    150155            return new \WP_Error(
    151156                'indigetal_offload_storage_read_failed',
    … …  
    157162        }
    158163
     164        $file_size = filesize($local_path);
     165
     166        if (false === $file_size) {
     167            return new \WP_Error(
     168                'indigetal_offload_storage_read_failed',
     169                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     170                [
     171                    'local_path' => $local_path,
     172                ]
     173            );
     174        }
     175
    159176        $headers = $this->getDefaultHeaders();
    160         $checksum = $this->calculateFileChecksum($local_path);
    161 
    162         if ('' !== $checksum) {
    163             $headers['Checksum'] = $checksum;
    164         }
    165 
    166177        $filetype = wp_check_filetype(basename($local_path));
    167178
    … …  
    173184            'method'      => 'PUT',
    174185            'headers'     => $headers,
    175             'body'        => $body,
    176186            'timeout'     => self::UPLOAD_TIMEOUT,
    177187            'redirection' => 0,
    … …  
    227237
    228238    /**
     239     * PUT a local file to Bunny Storage from disk.
     240     *
     241     * @param string               $request_url Absolute request URL.
     242     * @param string               $local_path  Absolute local file path.
     243     * @param array<string,string> $headers     Stable request headers.
     244     * @return array|\WP_Error
     245     */
     246    private function requestDirectStoragePut($request_url, $local_path, array $headers) {
     247        $request_headers = $headers;
     248        clearstatcache(true, $local_path);
     249
     250        if (!is_file($local_path)) {
     251            return new \WP_Error(
     252                'indigetal_offload_storage_read_failed',
     253                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     254                [
     255                    'local_path' => $local_path,
     256                ]
     257            );
     258        }
     259
     260        $file_size = filesize($local_path);
     261
     262        if (false === $file_size) {
     263            return new \WP_Error(
     264                'indigetal_offload_storage_read_failed',
     265                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     266                [
     267                    'local_path' => $local_path,
     268                ]
     269            );
     270        }
     271
     272        $checksum = $this->calculateFileChecksum($local_path);
     273
     274        if ('' !== $checksum) {
     275            $request_headers['Checksum'] = $checksum;
     276        }
     277
     278        $file_handle = fopen($local_path, 'rb');
     279
     280        if (false === $file_handle) {
     281            return new \WP_Error(
     282                'indigetal_offload_storage_read_failed',
     283                __('Failed to read the local file for Bunny Storage upload.', 'indigetal-media-offload-for-bunny-net'),
     284                [
     285                    'local_path' => $local_path,
     286                ]
     287            );
     288        }
     289
     290        $request_headers['Content-Length'] = (string) $file_size;
     291
     292        if ($file_size >= self::DIRECT_UPLOAD_EXPECT_THRESHOLD) {
     293            $request_headers['Expect'] = '100-continue';
     294        }
     295
     296        $sent_file = false;
     297        $callback = function ($handle, $request_args, $url) use ($request_url, $file_handle, $file_size, &$sent_file) {
     298            if ($url !== $request_url) {
     299                return;
     300            }
     301
     302            curl_setopt($handle, \CURLOPT_POSTFIELDS, null);
     303            curl_setopt($handle, \CURLOPT_UPLOAD, true);
     304            curl_setopt($handle, \CURLOPT_INFILE, $file_handle);
     305            $infile_size_option = \CURLOPT_INFILESIZE;
     306            if (defined('CURLOPT_INFILESIZE_LARGE')) {
     307                $infile_size_option = \CURLOPT_INFILESIZE_LARGE;
     308            }
     309            curl_setopt($handle, $infile_size_option, $file_size);
     310            $sent_file = true;
     311        };
     312
     313        add_action('http_api_curl', $callback, PHP_INT_MAX, 3);
     314
     315        try {
     316            $response = wp_remote_request($request_url, [
     317                'method'      => 'PUT',
     318                'headers'     => $request_headers,
     319                'timeout'     => self::UPLOAD_TIMEOUT,
     320                'redirection' => 0,
     321            ]);
     322        } finally {
     323            remove_action('http_api_curl', $callback, PHP_INT_MAX);
     324            fclose($file_handle);
     325        }
     326
     327        if (false === $sent_file && is_wp_error($response)) {
     328            return $response;
     329        }
     330
     331        if (false === $sent_file) {
     332            return new \WP_Error(
     333                'indigetal_offload_storage_upload_not_sent',
     334                __('The storage upload received a response, but the local file was not sent.', 'indigetal-media-offload-for-bunny-net'),
     335                [
     336                    'local_path' => $local_path,
     337                ]
     338            );
     339        }
     340
     341        return $response;
     342    }
     343
     344    /**
    229345     * Upload a local file to Bunny Storage.
    230346     *
    … …  
    254370        BunnyLogger::log("Uploading file to Bunny Storage: {$remote_path}", 'info');
    255371
    256         return $this->requestWithRetry($request_url, $request_args, $remote_path, 'upload', 201);
     372        $attempt_request = function () use ($request_url, $local_path, $request_args) {
     373            return $this->requestDirectStoragePut($request_url, $local_path, $request_args['headers']);
     374        };
     375
     376        return $this->requestWithRetry($request_url, $request_args, $remote_path, 'upload', 201, $attempt_request);
    257377    }
    258378
    … …  
    378498     * @param string $operation     Human-readable operation label.
    379499     * @param int    $success_code  Expected success HTTP status.
     500     * @param callable|null $attempt_request Optional per-attempt request callback.
    380501     * @return array|\WP_Error
    381502     */
    382     private function requestWithRetry($request_url, array $request_args, $remote_path, $operation, $success_code) {
     503    private function requestWithRetry($request_url, array $request_args, $remote_path, $operation, $success_code, $attempt_request = null) {
    383504        $attempt = 0;
    384505        $last_error = null;
    … …  
    388509            $this->maybeWaitForRetryAfter();
    389510
    390             $response = wp_remote_request($request_url, $request_args);
     511            if (is_callable($attempt_request)) {
     512                $response = $attempt_request();
     513            } else {
     514                $response = wp_remote_request($request_url, $request_args);
     515            }
    391516
    392517            if (is_wp_error($response)) {
  • indigetal-media-offload-for-bunny-net/trunk/includes/Bunny/BunnyVideoHandler.php

    r3802365 r3820862  
    2121     */
    2222    private const MP4_FALLBACK_MAX_HEIGHT = 720;
    23     private const DIRECT_UPLOAD_MEMORY_SAFETY_BUFFER = 16777216; // 16 MiB.
     23    /**
     24     * Byte size where the direct Stream PUT adds Expect: 100-continue.
     25     */
     26    private const DIRECT_UPLOAD_EXPECT_THRESHOLD = 1048576;
    2427    /**
    2528     * Bunny-compatible subset of WordPress default video MIME types for Media Library uploads.
    … …  
    124127        $uploadEndpoint = "library/{$library_id}/videos/{$videoId}";
    125128
    126         $videoData = file_get_contents($filePath);
    127         if ($videoData === false || strlen($videoData) === 0) {
     129        clearstatcache(true, $filePath);
     130        $fileSize = filesize($filePath);
     131        if (false === $fileSize || 0 === $fileSize) {
    128132            BunnyLogger::log("uploadVideo: Failed to read video file for {$filePath}.", 'error');
    129133            $readError = new \WP_Error('video_file_read_failed', __('Failed to read the video file before uploading.', 'indigetal-media-offload-for-bunny-net'));
    … …  
    132136        }
    133137
    134         $uploadResponse = $this->apiClient->executeWithRetry(function() use ($uploadEndpoint, $videoData) {
    135             return wp_remote_request($this->video_base_url . $uploadEndpoint, [
    136                 'method'    => 'PUT',
    137                 'headers'   => [
    138                     'AccessKey'    => $this->apiClient->getAccessKey(),
    139                     'Accept'       => 'application/json',
    140                     'Content-Type' => 'application/octet-stream'
    141                 ],
    142                 'body'      => $videoData,
    143                 'timeout'   => 300,
    144             ]);
     138        $uploadResponse = $this->apiClient->executeWithRetry(function() use ($uploadEndpoint, $filePath, $fileSize) {
     139            return $this->requestDirectVideoPut($this->video_base_url . $uploadEndpoint, $filePath, (int) $fileSize);
    145140        });
    146141
    … …  
    209204
    210205    /**
    211      * Ensure the direct PUT upload path has enough PHP memory to read the file.
     206     * PUT a local video file to Bunny Stream from disk.
     207     *
     208     * @param string $requestUrl Absolute request URL.
     209     * @param string $filePath   Absolute local video path.
     210     * @param int    $fileSize   File size in bytes.
     211     * @return array|\WP_Error
     212     */
     213    private function requestDirectVideoPut($requestUrl, $filePath, $fileSize) {
     214        $fileHandle = fopen($filePath, 'rb');
     215        if (false === $fileHandle) {
     216            return new \WP_Error(
     217                'video_file_read_failed',
     218                __('Failed to read the video file before uploading.', 'indigetal-media-offload-for-bunny-net')
     219            );
     220        }
     221
     222        $sentFile = false;
     223        $callback = function ($handle, $requestArgs, $url) use ($requestUrl, $fileHandle, $fileSize, &$sentFile) {
     224            if ($url !== $requestUrl) {
     225                return;
     226            }
     227
     228            curl_setopt($handle, \CURLOPT_POSTFIELDS, null);
     229            curl_setopt($handle, \CURLOPT_UPLOAD, true);
     230            curl_setopt($handle, \CURLOPT_INFILE, $fileHandle);
     231            $infileSizeOption = \CURLOPT_INFILESIZE;
     232            if (defined('CURLOPT_INFILESIZE_LARGE')) {
     233                $infileSizeOption = \CURLOPT_INFILESIZE_LARGE;
     234            }
     235            curl_setopt($handle, $infileSizeOption, $fileSize);
     236            $sentFile = true;
     237        };
     238
     239        $headers = [
     240            'AccessKey'      => $this->apiClient->getAccessKey(),
     241            'Accept'         => 'application/json',
     242            'Content-Type'   => 'application/octet-stream',
     243            'Content-Length' => (string) $fileSize,
     244        ];
     245
     246        if ($fileSize >= self::DIRECT_UPLOAD_EXPECT_THRESHOLD) {
     247            $headers['Expect'] = '100-continue';
     248        }
     249
     250        add_action('http_api_curl', $callback, PHP_INT_MAX, 3);
     251
     252        try {
     253            $response = wp_remote_request($requestUrl, [
     254                'method'      => 'PUT',
     255                'headers'     => $headers,
     256                'timeout'     => 300,
     257                'redirection' => 0,
     258            ]);
     259        } finally {
     260            remove_action('http_api_curl', $callback, PHP_INT_MAX);
     261            fclose($fileHandle);
     262        }
     263
     264        if (false === $sentFile && is_wp_error($response)) {
     265            return $response;
     266        }
     267
     268        if (false === $sentFile) {
     269            return new \WP_Error(
     270                'video_upload_failed',
     271                __('The video upload did not send the local file.', 'indigetal-media-offload-for-bunny-net')
     272            );
     273        }
     274
     275        return $response;
     276    }
     277
     278    /**
     279     * Confirm the local video has a readable file size before the direct PUT.
    212280     *
    213281     * @param string $filePath Absolute local video path.
    … …  
    217285        clearstatcache(true, $filePath);
    218286
    219         $fileSize = filesize($filePath);
    220         if (false === $fileSize) {
     287        if (false === filesize($filePath)) {
    221288            return new \WP_Error(
    222289                'video_file_size_unavailable',
    … …  
    225292        }
    226293
    227         $memoryLimit = $this->getMemoryLimitBytes();
    228         if ($memoryLimit <= 0) {
    229             return true;
    230         }
    231 
    232         $currentUsage = memory_get_usage(true);
    233         $remainingMemory = max(0, $memoryLimit - $currentUsage);
    234         $requiredMemory = (int) $fileSize + self::DIRECT_UPLOAD_MEMORY_SAFETY_BUFFER;
    235 
    236         if ($remainingMemory >= $requiredMemory) {
    237             return true;
    238         }
    239 
    240         return new \WP_Error(
    241             'video_direct_upload_memory_limit_exceeded',
    242             sprintf(
    243                 /* translators: 1: video file size, 2: remaining PHP memory, 3: required PHP memory */
    244                 __('This video is too large for the server-side Stream upload path. File size: %1$s. Available PHP memory: %2$s. Required memory: %3$s. Reduce the file size, raise the PHP memory limit, or upload from an environment with a higher memory ceiling.', 'indigetal-media-offload-for-bunny-net'),
    245                 size_format((int) $fileSize),
    246                 size_format((int) $remainingMemory),
    247                 size_format((int) $requiredMemory)
    248             ),
    249             [
    250                 'file_size'        => (int) $fileSize,
    251                 'memory_limit'     => (int) $memoryLimit,
    252                 'memory_usage'     => (int) $currentUsage,
    253                 'remaining_memory' => (int) $remainingMemory,
    254                 'required_memory'  => (int) $requiredMemory,
    255             ]
    256         );
    257     }
    258 
    259     /**
    260      * Return the configured PHP memory limit in bytes.
    261      *
    262      * @return int
    263      */
    264     private function getMemoryLimitBytes() {
    265         $memoryLimit = ini_get('memory_limit');
    266 
    267         if (false === $memoryLimit || '' === $memoryLimit) {
    268             return 0;
    269         }
    270 
    271         return (int) wp_convert_hr_to_bytes($memoryLimit);
     294        return true;
    272295    }
    273296
  • indigetal-media-offload-for-bunny-net/trunk/indigetal-media-offload-for-bunny-net.php

    r3802365 r3820862  
    44 * Plugin URI: https://wordpress.org/plugins/indigetal-media-offload-for-bunny-net/
    55 * Description: Seamlessly offload the WordPress Media Library to Bunny Storage and stream videos from Bunny Stream.
    6  * Version: 1.1.1
     6 * Version: 1.1.2
    77 * Requires at least: 6.5
    88 * Requires PHP: 8.0
    99 * Author: Indigetal WebCraft
    10  * Author URI: https://indigetal.com
     10 * Author URI: https://indigetal.com/indigetal-offload-media-for-bunny-net/
    1111 * License: GPL-2.0-or-later
    1212 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    2121 * Semantic version of the Free plugin runtime (keep in sync with the Version header).
    2222 */
    23 define('INDIGETAL_OFFLOAD_VERSION', '1.1.1');
     23define('INDIGETAL_OFFLOAD_VERSION', '1.1.2');
    2424
    2525if (!function_exists('indigetal_offload_free_version')) {
  • indigetal-media-offload-for-bunny-net/trunk/languages/indigetal-media-offload-for-bunny-net.pot

    r3575069 r3820862  
    3333#. Author URI of the plugin
    3434#: indigetal-media-offload-for-bunny-net.php
    35 msgid "https://indigetal.com"
     35msgid "https://indigetal.com/indigetal-offload-media-for-bunny-net/"
    3636msgstr ""
    3737
  • indigetal-media-offload-for-bunny-net/trunk/readme.txt

    r3802365 r3820862  
    55Tested up to: 7.1
    66Requires PHP: 8.0
    7 Stable tag: 1.1.1
     7Stable tag: 1.1.2
    88License: GPL-2.0-or-later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    133133== Changelog ==
    134134
     135= 1.1.2 =
     136* Server-side Stream uploads send the local video from disk instead of loading the whole file into PHP memory. An unreadable file size still fails.
     137* Server-side Storage uploads send the local file from disk on each attempt instead of loading the whole file into PHP memory. An empty file still uploads. A missing file, a directory, or an unreadable file size still fails before any request.
     138
    135139= 1.1.1 =
    136140* Stream MP4 URLs use the highest stored rendition at or below 720p. The list is `_indigetal_offload_video_available_resolutions`, stored after encoding finishes. Until then the file stays `play_720p.mp4`.
    … …  
    158162== Upgrade Notice ==
    159163
     164= 1.1.2 =
     165Server-side Stream and Storage uploads read the local file from disk. No settings migration required.
     166
    160167= 1.1.1 =
    161168Stream MP4 renditions and core Video URL refresh. No settings migration required.
Note: See TracChangeset for help on using the changeset viewer.