Changeset 43489
- Timestamp:
- 07/17/2018 04:11:48 PM (6 years ago)
- Location:
- branches/4.9
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.9
-
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r43445 r43489 560 560 ); 561 561 562 $size_check = self::check_upload_size( $file_data ); 563 if ( is_wp_error( $size_check ) ) { 564 return $size_check; 565 } 566 562 567 $overrides = array( 563 568 'test_form' => false, … … 727 732 } 728 733 734 $size_check = self::check_upload_size( $files['file'] ); 735 if ( is_wp_error( $size_check ) ) { 736 return $size_check; 737 } 738 729 739 /** Include admin functions to get access to wp_handle_upload() */ 730 740 require_once ABSPATH . 'wp-admin/includes/admin.php'; … … 764 774 } 765 775 776 /** 777 * Determine if uploaded file exceeds space quota on multisite. 778 * 779 * Replicates check_upload_size(). 780 * 781 * @since 4.9.8 782 * 783 * @param array $file $_FILES array for a given file. 784 * @return true|WP_Error True if can upload, error for errors. 785 */ 786 protected function check_upload_size( $file ) { 787 if ( ! is_multisite() ) { 788 return true; 789 } 790 791 if ( get_site_option( 'upload_space_check_disabled' ) ) { 792 return true; 793 } 794 795 $space_left = get_upload_space_available(); 796 797 $file_size = filesize( $file['tmp_name'] ); 798 if ( $space_left < $file_size ) { 799 /* translators: %s: required disk space in kilobytes */ 800 return new WP_Error( 'rest_upload_limited_space', sprintf( __( 'Not enough space to upload. %s KB needed.' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) ), array( 'status' => 400 ) ); 801 } 802 803 if ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) { 804 /* translators: %s: maximum allowed file size in kilobytes */ 805 return new WP_Error( 'rest_upload_file_too_big', sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) ), array( 'status' => 400 ) ); 806 } 807 808 if ( upload_is_user_over_quota( false ) ) { 809 return new WP_Error( 'rest_upload_user_quota_exceeded', __( 'You have used your space quota. Please delete files before uploading.' ), array( 'status' => 400 ) ); 810 } 811 return true; 812 } 813 766 814 } -
branches/4.9/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r43445 r43489 1315 1315 } 1316 1316 1317 /** 1318 * @ticket 43751 1319 * @group multisite 1320 * @group ms-required 1321 */ 1322 public function test_create_item_with_file_exceeds_multisite_max_filesize() { 1323 wp_set_current_user( self::$author_id ); 1324 update_site_option( 'fileupload_maxk', 1 ); 1325 update_site_option( 'upload_space_check_disabled', false ); 1326 1327 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1328 $request->set_file_params( 1329 array( 1330 'file' => array( 1331 'error' => '0', 1332 'file' => file_get_contents( $this->test_file ), 1333 'name' => 'canola.jpg', 1334 'size' => filesize( $this->test_file ), 1335 'tmp_name' => $this->test_file, 1336 ), 1337 ) 1338 ); 1339 $request->set_param( 'title', 'My title is very cool' ); 1340 $request->set_param( 'caption', 'This is a better caption.' ); 1341 $request->set_header( 'Content-MD5', md5_file( $this->test_file ) ); 1342 1343 $response = rest_get_server()->dispatch( $request ); 1344 $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 ); 1345 } 1346 1347 /** 1348 * @ticket 43751 1349 * @group multisite 1350 * @group ms-required 1351 */ 1352 public function test_create_item_with_data_exceeds_multisite_max_filesize() { 1353 wp_set_current_user( self::$author_id ); 1354 update_site_option( 'fileupload_maxk', 1 ); 1355 update_site_option( 'upload_space_check_disabled', false ); 1356 1357 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1358 $request->set_header( 'Content-Type', 'image/jpeg' ); 1359 $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); 1360 $request->set_body( file_get_contents( $this->test_file ) ); 1361 $request->set_param( 'title', 'My title is very cool' ); 1362 $request->set_param( 'caption', 'This is a better caption.' ); 1363 1364 $response = rest_get_server()->dispatch( $request ); 1365 $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 ); 1366 } 1367 1368 /** 1369 * @ticket 43751 1370 * @group multisite 1371 * @group ms-required 1372 */ 1373 public function test_create_item_with_file_exceeds_multisite_site_upload_space() { 1374 wp_set_current_user( self::$author_id ); 1375 add_filter( 'get_space_allowed', '__return_zero' ); 1376 update_site_option( 'upload_space_check_disabled', false ); 1377 1378 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1379 $request->set_file_params( 1380 array( 1381 'file' => array( 1382 'error' => '0', 1383 'file' => file_get_contents( $this->test_file ), 1384 'name' => 'canola.jpg', 1385 'size' => filesize( $this->test_file ), 1386 'tmp_name' => $this->test_file, 1387 ), 1388 ) 1389 ); 1390 $request->set_param( 'title', 'My title is very cool' ); 1391 $request->set_param( 'caption', 'This is a better caption.' ); 1392 $request->set_header( 'Content-MD5', md5_file( $this->test_file ) ); 1393 1394 $response = rest_get_server()->dispatch( $request ); 1395 $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 ); 1396 } 1397 1398 /** 1399 * @ticket 43751 1400 * @group multisite 1401 * @group ms-required 1402 */ 1403 public function test_create_item_with_data_exceeds_multisite_site_upload_space() { 1404 wp_set_current_user( self::$author_id ); 1405 add_filter( 'get_space_allowed', '__return_zero' ); 1406 update_site_option( 'upload_space_check_disabled', false ); 1407 1408 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1409 $request->set_header( 'Content-Type', 'image/jpeg' ); 1410 $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); 1411 $request->set_body( file_get_contents( $this->test_file ) ); 1412 $request->set_param( 'title', 'My title is very cool' ); 1413 $request->set_param( 'caption', 'This is a better caption.' ); 1414 1415 $response = rest_get_server()->dispatch( $request ); 1416 $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 ); 1417 } 1418 1317 1419 }
Note: See TracChangeset
for help on using the changeset viewer.