Changeset 3820862
- Timestamp:
- 11/24/2026 03:53:06 PM (66 minutes ago)
- Location:
- indigetal-media-offload-for-bunny-net
- Files:
-
- 9 edited
- 8 copied
-
tags/1.1.2 (copied) (copied from indigetal-media-offload-for-bunny-net/trunk )
-
tags/1.1.2/includes/Admin/BunnySettings.php (modified) (1 diff)
-
tags/1.1.2/includes/Bunny/BunnyStorageClient.php (modified) (8 diffs)
-
tags/1.1.2/includes/Bunny/BunnyVideoHandler.php (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/includes/Bunny/BunnyVideoHandler.php ) (6 diffs)
-
tags/1.1.2/includes/Integration/BunnyMediaLibrary.php (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/includes/Integration/BunnyMediaLibrary.php )
-
tags/1.1.2/includes/Integration/BunnyMetadataManager.php (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/includes/Integration/BunnyMetadataManager.php )
-
tags/1.1.2/includes/Integration/BunnyVideoMetadataSync.php (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/includes/Integration/BunnyVideoMetadataSync.php )
-
tags/1.1.2/indigetal-media-offload-for-bunny-net.php (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/indigetal-media-offload-for-bunny-net.php ) (2 diffs)
-
tags/1.1.2/languages/indigetal-media-offload-for-bunny-net.pot (modified) (1 diff)
-
tags/1.1.2/readme.txt (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/readme.txt ) (3 diffs)
-
tags/1.1.2/uninstall.php (copied) (copied from indigetal-media-offload-for-bunny-net/trunk/uninstall.php )
-
trunk/includes/Admin/BunnySettings.php (modified) (1 diff)
-
trunk/includes/Bunny/BunnyStorageClient.php (modified) (8 diffs)
-
trunk/includes/Bunny/BunnyVideoHandler.php (modified) (6 diffs)
-
trunk/indigetal-media-offload-for-bunny-net.php (modified) (2 diffs)
-
trunk/languages/indigetal-media-offload-for-bunny-net.pot (modified) (1 diff)
-
trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
indigetal-media-offload-for-bunny-net/tags/1.1.2/includes/Admin/BunnySettings.php
r3780520 r3820862 68 68 * @var string 69 69 */ 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/'; 71 71 72 72 /** -
indigetal-media-offload-for-bunny-net/tags/1.1.2/includes/Bunny/BunnyStorageClient.php
r3575069 r3820862 21 21 const DOWNLOAD_TIMEOUT = 120; 22 22 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; 23 28 24 29 /** … … 145 150 } 146 151 147 $body = file_get_contents($local_path);148 149 if ( false === $body) {152 clearstatcache(true, $local_path); 153 154 if (!is_file($local_path)) { 150 155 return new \WP_Error( 151 156 'indigetal_offload_storage_read_failed', … … 157 162 } 158 163 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 159 176 $headers = $this->getDefaultHeaders(); 160 $checksum = $this->calculateFileChecksum($local_path);161 162 if ('' !== $checksum) {163 $headers['Checksum'] = $checksum;164 }165 166 177 $filetype = wp_check_filetype(basename($local_path)); 167 178 … … 173 184 'method' => 'PUT', 174 185 'headers' => $headers, 175 'body' => $body,176 186 'timeout' => self::UPLOAD_TIMEOUT, 177 187 'redirection' => 0, … … 227 237 228 238 /** 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 /** 229 345 * Upload a local file to Bunny Storage. 230 346 * … … 254 370 BunnyLogger::log("Uploading file to Bunny Storage: {$remote_path}", 'info'); 255 371 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); 257 377 } 258 378 … … 378 498 * @param string $operation Human-readable operation label. 379 499 * @param int $success_code Expected success HTTP status. 500 * @param callable|null $attempt_request Optional per-attempt request callback. 380 501 * @return array|\WP_Error 381 502 */ 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) { 383 504 $attempt = 0; 384 505 $last_error = null; … … 388 509 $this->maybeWaitForRetryAfter(); 389 510 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 } 391 516 392 517 if (is_wp_error($response)) { -
indigetal-media-offload-for-bunny-net/tags/1.1.2/includes/Bunny/BunnyVideoHandler.php
r3802365 r3820862 21 21 */ 22 22 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; 24 27 /** 25 28 * Bunny-compatible subset of WordPress default video MIME types for Media Library uploads. … … 124 127 $uploadEndpoint = "library/{$library_id}/videos/{$videoId}"; 125 128 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) { 128 132 BunnyLogger::log("uploadVideo: Failed to read video file for {$filePath}.", 'error'); 129 133 $readError = new \WP_Error('video_file_read_failed', __('Failed to read the video file before uploading.', 'indigetal-media-offload-for-bunny-net')); … … 132 136 } 133 137 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); 145 140 }); 146 141 … … 209 204 210 205 /** 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. 212 280 * 213 281 * @param string $filePath Absolute local video path. … … 217 285 clearstatcache(true, $filePath); 218 286 219 $fileSize = filesize($filePath); 220 if (false === $fileSize) { 287 if (false === filesize($filePath)) { 221 288 return new \WP_Error( 222 289 'video_file_size_unavailable', … … 225 292 } 226 293 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; 272 295 } 273 296 -
indigetal-media-offload-for-bunny-net/tags/1.1.2/indigetal-media-offload-for-bunny-net.php
r3802365 r3820862 4 4 * Plugin URI: https://wordpress.org/plugins/indigetal-media-offload-for-bunny-net/ 5 5 * Description: Seamlessly offload the WordPress Media Library to Bunny Storage and stream videos from Bunny Stream. 6 * Version: 1.1. 16 * Version: 1.1.2 7 7 * Requires at least: 6.5 8 8 * Requires PHP: 8.0 9 9 * Author: Indigetal WebCraft 10 * Author URI: https://indigetal.com 10 * Author URI: https://indigetal.com/indigetal-offload-media-for-bunny-net/ 11 11 * License: GPL-2.0-or-later 12 12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 21 21 * Semantic version of the Free plugin runtime (keep in sync with the Version header). 22 22 */ 23 define('INDIGETAL_OFFLOAD_VERSION', '1.1. 1');23 define('INDIGETAL_OFFLOAD_VERSION', '1.1.2'); 24 24 25 25 if (!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 33 33 #. Author URI of the plugin 34 34 #: indigetal-media-offload-for-bunny-net.php 35 msgid "https://indigetal.com "35 msgid "https://indigetal.com/indigetal-offload-media-for-bunny-net/" 36 36 msgstr "" 37 37 -
indigetal-media-offload-for-bunny-net/tags/1.1.2/readme.txt
r3802365 r3820862 5 5 Tested up to: 7.1 6 6 Requires PHP: 8.0 7 Stable tag: 1.1. 17 Stable tag: 1.1.2 8 8 License: GPL-2.0-or-later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 133 133 == Changelog == 134 134 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 135 139 = 1.1.1 = 136 140 * 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`. … … 158 162 == Upgrade Notice == 159 163 164 = 1.1.2 = 165 Server-side Stream and Storage uploads read the local file from disk. No settings migration required. 166 160 167 = 1.1.1 = 161 168 Stream MP4 renditions and core Video URL refresh. No settings migration required. -
indigetal-media-offload-for-bunny-net/trunk/includes/Admin/BunnySettings.php
r3780520 r3820862 68 68 * @var string 69 69 */ 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/'; 71 71 72 72 /** -
indigetal-media-offload-for-bunny-net/trunk/includes/Bunny/BunnyStorageClient.php
r3575069 r3820862 21 21 const DOWNLOAD_TIMEOUT = 120; 22 22 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; 23 28 24 29 /** … … 145 150 } 146 151 147 $body = file_get_contents($local_path);148 149 if ( false === $body) {152 clearstatcache(true, $local_path); 153 154 if (!is_file($local_path)) { 150 155 return new \WP_Error( 151 156 'indigetal_offload_storage_read_failed', … … 157 162 } 158 163 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 159 176 $headers = $this->getDefaultHeaders(); 160 $checksum = $this->calculateFileChecksum($local_path);161 162 if ('' !== $checksum) {163 $headers['Checksum'] = $checksum;164 }165 166 177 $filetype = wp_check_filetype(basename($local_path)); 167 178 … … 173 184 'method' => 'PUT', 174 185 'headers' => $headers, 175 'body' => $body,176 186 'timeout' => self::UPLOAD_TIMEOUT, 177 187 'redirection' => 0, … … 227 237 228 238 /** 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 /** 229 345 * Upload a local file to Bunny Storage. 230 346 * … … 254 370 BunnyLogger::log("Uploading file to Bunny Storage: {$remote_path}", 'info'); 255 371 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); 257 377 } 258 378 … … 378 498 * @param string $operation Human-readable operation label. 379 499 * @param int $success_code Expected success HTTP status. 500 * @param callable|null $attempt_request Optional per-attempt request callback. 380 501 * @return array|\WP_Error 381 502 */ 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) { 383 504 $attempt = 0; 384 505 $last_error = null; … … 388 509 $this->maybeWaitForRetryAfter(); 389 510 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 } 391 516 392 517 if (is_wp_error($response)) { -
indigetal-media-offload-for-bunny-net/trunk/includes/Bunny/BunnyVideoHandler.php
r3802365 r3820862 21 21 */ 22 22 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; 24 27 /** 25 28 * Bunny-compatible subset of WordPress default video MIME types for Media Library uploads. … … 124 127 $uploadEndpoint = "library/{$library_id}/videos/{$videoId}"; 125 128 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) { 128 132 BunnyLogger::log("uploadVideo: Failed to read video file for {$filePath}.", 'error'); 129 133 $readError = new \WP_Error('video_file_read_failed', __('Failed to read the video file before uploading.', 'indigetal-media-offload-for-bunny-net')); … … 132 136 } 133 137 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); 145 140 }); 146 141 … … 209 204 210 205 /** 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. 212 280 * 213 281 * @param string $filePath Absolute local video path. … … 217 285 clearstatcache(true, $filePath); 218 286 219 $fileSize = filesize($filePath); 220 if (false === $fileSize) { 287 if (false === filesize($filePath)) { 221 288 return new \WP_Error( 222 289 'video_file_size_unavailable', … … 225 292 } 226 293 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; 272 295 } 273 296 -
indigetal-media-offload-for-bunny-net/trunk/indigetal-media-offload-for-bunny-net.php
r3802365 r3820862 4 4 * Plugin URI: https://wordpress.org/plugins/indigetal-media-offload-for-bunny-net/ 5 5 * Description: Seamlessly offload the WordPress Media Library to Bunny Storage and stream videos from Bunny Stream. 6 * Version: 1.1. 16 * Version: 1.1.2 7 7 * Requires at least: 6.5 8 8 * Requires PHP: 8.0 9 9 * Author: Indigetal WebCraft 10 * Author URI: https://indigetal.com 10 * Author URI: https://indigetal.com/indigetal-offload-media-for-bunny-net/ 11 11 * License: GPL-2.0-or-later 12 12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 21 21 * Semantic version of the Free plugin runtime (keep in sync with the Version header). 22 22 */ 23 define('INDIGETAL_OFFLOAD_VERSION', '1.1. 1');23 define('INDIGETAL_OFFLOAD_VERSION', '1.1.2'); 24 24 25 25 if (!function_exists('indigetal_offload_free_version')) { -
indigetal-media-offload-for-bunny-net/trunk/languages/indigetal-media-offload-for-bunny-net.pot
r3575069 r3820862 33 33 #. Author URI of the plugin 34 34 #: indigetal-media-offload-for-bunny-net.php 35 msgid "https://indigetal.com "35 msgid "https://indigetal.com/indigetal-offload-media-for-bunny-net/" 36 36 msgstr "" 37 37 -
indigetal-media-offload-for-bunny-net/trunk/readme.txt
r3802365 r3820862 5 5 Tested up to: 7.1 6 6 Requires PHP: 8.0 7 Stable tag: 1.1. 17 Stable tag: 1.1.2 8 8 License: GPL-2.0-or-later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 133 133 == Changelog == 134 134 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 135 139 = 1.1.1 = 136 140 * 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`. … … 158 162 == Upgrade Notice == 159 163 164 = 1.1.2 = 165 Server-side Stream and Storage uploads read the local file from disk. No settings migration required. 166 160 167 = 1.1.1 = 161 168 Stream MP4 renditions and core Video URL refresh. No settings migration required.
Note:
See TracChangeset
for help on using the changeset viewer.