Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slow call to scandir() when uploading files #422

Merged
merged 4 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions inc/class-stream-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,32 @@ public function dir_opendir( $path, $options ) {

$this->openedBucketPrefix = $params['Key'];

// WordPress attempts to scan whole directories via wp_unique_filename(), which can be very slow
// when there are thousands of files in a single uploads sub directory. This is due to behaviour
// introduced in https://core.trac.wordpress.org/changeset/46822/. Essentially when a file is uploaded,
// it's not enough to make sure no filename already exists (and append a `-1` to the end), because
// image sizes of that image could also conflict with already existing files too. Because image sizes
// (in the form of -800x600.jpg) can be arbitrary integers, it's not possible to iterate the filesystem
// for all possible matching / colliding file names. WordPress core uses a preg-match on all files that
// might conflict with the given filename.
//
// Fortunately, we can make use of S3 arbitrary prefixes to optimize this query. The WordPress regex
// done via _wp_check_existing_file_names() is essentially `^$filename-...`, so we can modify the prefix
// to include the filename, therefore only return a subset of files from S3 that are more likely to match
// the preg_match() call.
//
// Essentially, wp_unique_filename( my-file.jpg ) doing a `scandir( s3://bucket/2019/04/ )` will actually result in an s3
// listObject query for `s3://bucket/2019/04/my-file` which means even if there are millions of files in `2019/04/` we only
// return a much smaller subset.
//
// Anyone reading this far, brace yourselves for a mighty horrible hack.
$backtrace = debug_backtrace( 0, 3 );
if ( isset( $backtrace[1]['function'] ) && $backtrace[1]['function'] === 'scandir' && isset( $backtrace[2]['function'] ) && $backtrace[2]['function'] === 'wp_unique_filename' ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to bail if it hit the filter callback above, or is that already handled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be triggered, as the backtrace doesn't reflect our own scandir call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha.

$filename = $backtrace[2]['args'][1];
$name = pathinfo( $filename, PATHINFO_FILENAME );
$op['Prefix'] .= $name;
}

// Filter our "/" keys added by the console as directories, and ensure
// that if a filter function is provided that it passes the filter.
$this->objectIterator = \Aws\flatmap(
Expand Down
12 changes: 12 additions & 0 deletions tests/test-s3-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,16 @@ function test_get_s3_bucket() {
$this->assertEquals( 'hmn-uploads', $uploads->get_s3_bucket() );
}

function test_wp_unique_filename() {
S3_Uploads\Plugin::get_instance()->setup();
$upload_dir = wp_upload_dir();

file_put_contents( $upload_dir['path'] . '/my-file-scaled.jpg', '' );
$filename = wp_unique_filename( $upload_dir['path'], 'my-file.jpg' );
$this->assertEquals( 'my-file-1.jpg', $filename );

$filename = wp_unique_filename( $upload_dir['path'], 'my-new-file.jpg' );
$this->assertEquals( 'my-new-file.jpg', $filename );
}

}