Skip to content

Commit

Permalink
Merge pull request #87 from humanmade/cleanup-files
Browse files Browse the repository at this point in the history
Clean up image sizes on delete attachment
  • Loading branch information
joehoyle committed Apr 3, 2016
2 parents f39078a + 07c1680 commit 05e7837
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
18 changes: 18 additions & 0 deletions inc/class-s3-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function setup() {

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' ) );
Expand All @@ -60,6 +61,7 @@ public function tear_down() {
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' ) );
}

/**
Expand Down Expand Up @@ -98,6 +100,22 @@ public function filter_upload_dir( $dirs ) {
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;
Expand Down
Binary file modified tests/data/canola.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions tests/test-s3-uploads-stream-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public function test_getimagesize_via_stream_wrapper() {
$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 );
}

Expand Down
45 changes: 45 additions & 0 deletions tests/test-s3-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,49 @@ public function test_get_s3_client() {

$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'] ) );
}
}
}

0 comments on commit 05e7837

Please sign in to comment.