Changeset 43462
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
r43437 r43462 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 } -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r43437 r43462 1433 1433 } 1434 1434 1435 /** 1436 * @ticket 43751 1437 * @group multisite 1438 * @group ms-required 1439 */ 1440 public function test_create_item_with_file_exceeds_multisite_max_filesize() { 1441 wp_set_current_user( self::$author_id ); 1442 update_site_option( 'fileupload_maxk', 1 ); 1443 update_site_option( 'upload_space_check_disabled', false ); 1444 1445 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1446 $request->set_file_params( 1447 array( 1448 'file' => array( 1449 'error' => '0', 1450 'file' => file_get_contents( $this->test_file ), 1451 'name' => 'canola.jpg', 1452 'size' => filesize( $this->test_file ), 1453 'tmp_name' => $this->test_file, 1454 ), 1455 ) 1456 ); 1457 $request->set_param( 'title', 'My title is very cool' ); 1458 $request->set_param( 'caption', 'This is a better caption.' ); 1459 $request->set_header( 'Content-MD5', md5_file( $this->test_file ) ); 1460 1461 $response = rest_get_server()->dispatch( $request ); 1462 $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 ); 1463 } 1464 1465 /** 1466 * @ticket 43751 1467 * @group multisite 1468 * @group ms-required 1469 */ 1470 public function test_create_item_with_data_exceeds_multisite_max_filesize() { 1471 wp_set_current_user( self::$author_id ); 1472 update_site_option( 'fileupload_maxk', 1 ); 1473 update_site_option( 'upload_space_check_disabled', false ); 1474 1475 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1476 $request->set_header( 'Content-Type', 'image/jpeg' ); 1477 $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); 1478 $request->set_body( file_get_contents( $this->test_file ) ); 1479 $request->set_param( 'title', 'My title is very cool' ); 1480 $request->set_param( 'caption', 'This is a better caption.' ); 1481 1482 $response = rest_get_server()->dispatch( $request ); 1483 $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 ); 1484 } 1485 1486 /** 1487 * @ticket 43751 1488 * @group multisite 1489 * @group ms-required 1490 */ 1491 public function test_create_item_with_file_exceeds_multisite_site_upload_space() { 1492 wp_set_current_user( self::$author_id ); 1493 add_filter( 'get_space_allowed', '__return_zero' ); 1494 update_site_option( 'upload_space_check_disabled', false ); 1495 1496 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1497 $request->set_file_params( 1498 array( 1499 'file' => array( 1500 'error' => '0', 1501 'file' => file_get_contents( $this->test_file ), 1502 'name' => 'canola.jpg', 1503 'size' => filesize( $this->test_file ), 1504 'tmp_name' => $this->test_file, 1505 ), 1506 ) 1507 ); 1508 $request->set_param( 'title', 'My title is very cool' ); 1509 $request->set_param( 'caption', 'This is a better caption.' ); 1510 $request->set_header( 'Content-MD5', md5_file( $this->test_file ) ); 1511 1512 $response = rest_get_server()->dispatch( $request ); 1513 $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 ); 1514 } 1515 1516 /** 1517 * @ticket 43751 1518 * @group multisite 1519 * @group ms-required 1520 */ 1521 public function test_create_item_with_data_exceeds_multisite_site_upload_space() { 1522 wp_set_current_user( self::$author_id ); 1523 add_filter( 'get_space_allowed', '__return_zero' ); 1524 update_site_option( 'upload_space_check_disabled', false ); 1525 1526 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 1527 $request->set_header( 'Content-Type', 'image/jpeg' ); 1528 $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' ); 1529 $request->set_body( file_get_contents( $this->test_file ) ); 1530 $request->set_param( 'title', 'My title is very cool' ); 1531 $request->set_param( 'caption', 'This is a better caption.' ); 1532 1533 $response = rest_get_server()->dispatch( $request ); 1534 $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 ); 1535 } 1536 1435 1537 }
Note: See TracChangeset
for help on using the changeset viewer.