Loading inc/class-s3-uploads.php +18 −0 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ class S3_Uploads { add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) ); add_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 ); add_filter( 'wp_delete_file', array( $this, 'wp_filter_delete_file' ) ); remove_filter( 'admin_notices', 'wpthumb_errors' ); add_action( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) ); Loading @@ -60,6 +61,7 @@ class S3_Uploads { remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) ); remove_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 ); remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) ); remove_filter( 'wp_delete_file', array( $this, 'wp_filter_delete_file' ) ); } /** Loading Loading @@ -98,6 +100,22 @@ class S3_Uploads { return $dirs; } /** * When WordPress removes files, it's expecting to do so on * absolute file paths, as such it breaks when using uris for * file paths (such as s3://...). We have to filter the file_path * to only return the relative section, to play nice with WordPress * handling. * * @param string $file_path * @return string */ public function wp_filter_delete_file( $file_path ) { $dir = wp_upload_dir(); return str_replace( trailingslashit( $dir['basedir'] ), '', $file_path ); } public function get_s3_url() { if ( $this->bucket_url ) { return $this->bucket_url; Loading tests/data/canola.jpg +174 KiB (198 KiB) Loading image diff... tests/test-s3-uploads-stream-wrapper.php +4 −4 Original line number Diff line number Diff line Loading @@ -93,13 +93,13 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase { $image = getimagesize( $file ); $this->assertEquals( array( 160, 120, 640, 480, 2, 'width="160" height="120"', 'width="640" height="480"', 'bits' => 8, 'channels' => 3, 'mime' => 'image/jpeg' 'mime' => 'image/jpeg', ), $image ); } Loading tests/test-s3-uploads.php +45 −0 Original line number Diff line number Diff line Loading @@ -67,4 +67,49 @@ class Test_S3_Uploads extends WP_UnitTestCase { $this->assertInstanceOf( 'Aws\\S3\\S3Client', $s3 ); } public function test_generate_attachment_metadata() { S3_Uploads::get_instance()->setup(); $upload_dir = wp_upload_dir(); copy( dirname( __FILE__ ) . '/data/canola.jpg', $upload_dir['path'] . '/canola.jpg' ); $test_file = $upload_dir['path'] . '/canola.jpg'; $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', ) ); $meta_data = wp_generate_attachment_metadata( $attachment_id, $test_file ); $this->assertEquals( array( 'file' => 'canola-150x150.jpg', 'width' => 150, 'height' => 150, 'mime-type' => 'image/jpeg', ), $meta_data['sizes']['thumbnail'] ); $wp_upload_dir = wp_upload_dir(); $this->assertTrue( file_exists( $wp_upload_dir['path'] . '/canola-150x150.jpg' ) ); } public function test_image_sizes_are_deleted_on_attachment_delete() { S3_Uploads::get_instance()->setup(); $upload_dir = wp_upload_dir(); copy( dirname( __FILE__ ) . '/data/canola.jpg', $upload_dir['path'] . '/canola.jpg' ); $test_file = $upload_dir['path'] . '/canola.jpg'; $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', ) ); $meta_data = wp_generate_attachment_metadata( $attachment_id, $test_file ); wp_update_attachment_metadata( $attachment_id, $meta_data ); foreach ( $meta_data['sizes'] as $size ) { $this->assertTrue( file_exists( $upload_dir['path'] . '/' . $size['file'] ) ); } wp_delete_attachment( $attachment_id, true ); foreach ( $meta_data['sizes'] as $size ) { $this->assertFalse( file_exists( $upload_dir['path'] . '/' . $size['file'] ), sprintf( 'File %s was not deleted.', $upload_dir['path'] . '/' . $size['file'] ) ); } } } Loading
inc/class-s3-uploads.php +18 −0 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ class S3_Uploads { add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) ); add_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 ); add_filter( 'wp_delete_file', array( $this, 'wp_filter_delete_file' ) ); remove_filter( 'admin_notices', 'wpthumb_errors' ); add_action( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) ); Loading @@ -60,6 +61,7 @@ class S3_Uploads { remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) ); remove_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 ); remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) ); remove_filter( 'wp_delete_file', array( $this, 'wp_filter_delete_file' ) ); } /** Loading Loading @@ -98,6 +100,22 @@ class S3_Uploads { return $dirs; } /** * When WordPress removes files, it's expecting to do so on * absolute file paths, as such it breaks when using uris for * file paths (such as s3://...). We have to filter the file_path * to only return the relative section, to play nice with WordPress * handling. * * @param string $file_path * @return string */ public function wp_filter_delete_file( $file_path ) { $dir = wp_upload_dir(); return str_replace( trailingslashit( $dir['basedir'] ), '', $file_path ); } public function get_s3_url() { if ( $this->bucket_url ) { return $this->bucket_url; Loading
tests/test-s3-uploads-stream-wrapper.php +4 −4 Original line number Diff line number Diff line Loading @@ -93,13 +93,13 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase { $image = getimagesize( $file ); $this->assertEquals( array( 160, 120, 640, 480, 2, 'width="160" height="120"', 'width="640" height="480"', 'bits' => 8, 'channels' => 3, 'mime' => 'image/jpeg' 'mime' => 'image/jpeg', ), $image ); } Loading
tests/test-s3-uploads.php +45 −0 Original line number Diff line number Diff line Loading @@ -67,4 +67,49 @@ class Test_S3_Uploads extends WP_UnitTestCase { $this->assertInstanceOf( 'Aws\\S3\\S3Client', $s3 ); } public function test_generate_attachment_metadata() { S3_Uploads::get_instance()->setup(); $upload_dir = wp_upload_dir(); copy( dirname( __FILE__ ) . '/data/canola.jpg', $upload_dir['path'] . '/canola.jpg' ); $test_file = $upload_dir['path'] . '/canola.jpg'; $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', ) ); $meta_data = wp_generate_attachment_metadata( $attachment_id, $test_file ); $this->assertEquals( array( 'file' => 'canola-150x150.jpg', 'width' => 150, 'height' => 150, 'mime-type' => 'image/jpeg', ), $meta_data['sizes']['thumbnail'] ); $wp_upload_dir = wp_upload_dir(); $this->assertTrue( file_exists( $wp_upload_dir['path'] . '/canola-150x150.jpg' ) ); } public function test_image_sizes_are_deleted_on_attachment_delete() { S3_Uploads::get_instance()->setup(); $upload_dir = wp_upload_dir(); copy( dirname( __FILE__ ) . '/data/canola.jpg', $upload_dir['path'] . '/canola.jpg' ); $test_file = $upload_dir['path'] . '/canola.jpg'; $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( 'post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption', ) ); $meta_data = wp_generate_attachment_metadata( $attachment_id, $test_file ); wp_update_attachment_metadata( $attachment_id, $meta_data ); foreach ( $meta_data['sizes'] as $size ) { $this->assertTrue( file_exists( $upload_dir['path'] . '/' . $size['file'] ) ); } wp_delete_attachment( $attachment_id, true ); foreach ( $meta_data['sizes'] as $size ) { $this->assertFalse( file_exists( $upload_dir['path'] . '/' . $size['file'] ), sprintf( 'File %s was not deleted.', $upload_dir['path'] . '/' . $size['file'] ) ); } } }