Commit 05e7837f authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Merge pull request #87 from humanmade/cleanup-files

Clean up image sizes on delete attachment
parents f39078a5 07c1680c
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -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' ) );
@@ -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' ) );
	}

	/**
@@ -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;
+174 KiB (198 KiB)
Loading image diff...
+4 −4
Original line number Diff line number Diff line
@@ -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 );
	}

+45 −0
Original line number Diff line number Diff line
@@ -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'] ) );
		}
	}
}