This commit is contained in:
Javier Casares 2026-11-03 16:44:19 +00:00
commit a498b5af15

View file

@ -94,7 +94,10 @@ class Media_Uploader {
*
* Uses the AWS CommandPool to upload the original file and all generated
* thumbnail sizes in parallel, streaming each file directly from disk.
* No headObject pre-check is performed — files are assumed to be new.
*
* Before the first upload of an attachment, a remote name collision check
* runs (see maybe_rename_for_remote()) so an existing remote object is
* never silently overwritten by a re-uploaded file name.
*
* Concurrency defaults to 5 and can be overridden via the
* IDRIVEE2_UPLOAD_CONCURRENCY constant in wp-config.php.
@ -139,12 +142,26 @@ class Media_Uploader {
return $meta;
}
// Build list of files: original + each thumbnail.
$upload_dir = wp_upload_dir();
$basedir = $upload_dir['basedir'];
$client = $this->client_factory->create();
/*
* First upload only: wp_unique_filename() deduplicates against the local
* uploads directory, but this plugin deletes local files after uploading,
* so a re-used name looks "new" locally while the remote object exists.
* Rename the files WordPress-style (file-1.jpg, ...) to avoid overwriting.
*/
if ( ! get_post_meta( $attachment_id, '_idrivee2_s3_base_url', true ) ) {
$meta = $this->maybe_rename_for_remote( $client, $meta, $basedir );
}
$meta_file = isset( $meta['file'] ) && is_string( $meta['file'] ) ? $meta['file'] : '';
$base_path = path_join( $basedir, $meta_file );
$meta_sizes = isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ? $meta['sizes'] : array();
// Build list of files: original + each thumbnail.
/* @var array<string, string> $files */
$files = array( 'original' => $base_path );
foreach ( $meta_sizes as $size ) {
@ -154,7 +171,6 @@ class Media_Uploader {
}
// Open a stream for each file and build the AWS command list.
$client = $this->client_factory->create();
$bucket = $this->config->get_bucket();
$commands = array();
$key_map = array(); // int index -> file metadata.
@ -339,6 +355,205 @@ class Media_Uploader {
return $meta;
}
/**
* Rename attachment files when the remote key already exists.
*
* WordPress core deduplicates file names against the local uploads
* directory only (wp_unique_filename()). Because this plugin deletes local
* files after uploading, a re-uploaded name looks "new" locally while the
* remote object still exists, and a plain PutObject would silently
* overwrite it.
*
* When the remote key is taken, the original and all size files are renamed
* locally to the next free "-N" variant (file-1.jpg, file-2.jpg, ...) and
* the metadata is updated to match, mirroring what core would have done
* on a local collision. The updated meta is returned so the caller's
* meta_file/GUID handling and the persisted attachment metadata stay in
* sync with the uploaded keys.
*
* @since 1.5.1
*
* @param \Aws\S3\S3Client $client S3 client.
* @param array<string, mixed> $meta Attachment metadata.
* @param string $basedir Uploads base directory.
* @return array<string, mixed> Updated metadata.
*/
private function maybe_rename_for_remote( \Aws\S3\S3Client $client, array $meta, string $basedir ): array {
$meta_file = isset( $meta['file'] ) && is_string( $meta['file'] ) ? $meta['file'] : '';
if ( '' === $meta_file ) {
return $meta;
}
$new_file = $this->unique_remote_file( $client, $meta_file, $basedir );
if ( $new_file === $meta_file ) {
return $meta;
}
$old_path = path_join( $basedir, $meta_file );
$new_path = path_join( $basedir, $new_file );
// Security: refuse to rename files outside the uploads directory,
// mirroring the realpath() jail used for uploads in do_upload().
$real_basedir = realpath( $basedir );
if ( false === $real_basedir
|| ! $this->is_inside_basedir( $real_basedir, dirname( $old_path ) )
|| ! $this->is_inside_basedir( $real_basedir, dirname( $new_path ) ) ) {
$this->logger->warning( 'Refusing rename outside uploads dir', array( 'path' => $old_path ) );
return $meta;
}
if ( file_exists( $old_path )
// phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- WP_Filesystem move() unavailable without credentials in this context
&& ! rename( $old_path, $new_path ) ) {
$this->logger->warning( 'Rename failed after remote name collision, keeping original name', array( 'path' => $old_path ) );
return $meta;
}
// Base names without extension: "file.jpg" / "file-1.jpg" -> "file" / "file-1".
$old_name = basename( $meta_file );
$new_name = basename( $new_file );
$dot = strrpos( $old_name, '.' );
$old_base = ( false === $dot ) ? $old_name : substr( $old_name, 0, $dot );
$dot = strrpos( $new_name, '.' );
$new_base = ( false === $dot ) ? $new_name : substr( $new_name, 0, $dot );
$sizes = isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ? $meta['sizes'] : array();
foreach ( $sizes as $size_key => $size ) {
if ( ! is_array( $size ) || ! isset( $size['file'] ) || ! is_string( $size['file'] ) ) {
continue;
}
$size_file = $size['file'];
if ( ! str_starts_with( $size_file, $old_base ) ) {
continue; // Custom-named size: leave untouched.
}
$new_size_file = $new_base . substr( $size_file, strlen( $old_base ) );
$old_size_path = path_join( dirname( $old_path ), $size_file );
$new_size_path = path_join( dirname( $new_path ), $new_size_file );
// Security: same uploads-dir jail as for the original file.
if ( ! $this->is_inside_basedir( $real_basedir, dirname( $old_size_path ) )
|| ! $this->is_inside_basedir( $real_basedir, dirname( $new_size_path ) ) ) {
$this->logger->warning( 'Refusing rename outside uploads dir', array( 'path' => $old_size_path ) );
continue;
}
if ( file_exists( $old_size_path )
// phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- WP_Filesystem move() unavailable without credentials in this context
&& ! rename( $old_size_path, $new_size_path ) ) {
$this->logger->warning( 'Rename failed for size file', array( 'path' => $old_size_path ) );
}
$size['file'] = $new_size_file;
$sizes[ $size_key ] = $size;
}
$meta['sizes'] = $sizes;
$meta['file'] = $new_file;
$this->logger->info(
sprintf( 'Remote name collision: %s renamed to %s', $old_name, $new_name ),
array( 'original' => $meta_file )
);
return $meta;
}
/**
* Check whether a resolved directory stays inside the uploads base directory.
*
* @since 1.5.1
*
* @param string $real_basedir Realpath of the uploads base directory.
* @param string $dir Directory to check.
* @return bool True when the directory resolves inside the uploads base directory.
*/
private function is_inside_basedir( string $real_basedir, string $dir ): bool {
$real = realpath( $dir );
return false !== $real
&& 0 === strpos( $real, $real_basedir . DIRECTORY_SEPARATOR );
}
/**
* Find an uploads-relative file name that is free remotely and locally.
*
* Mirrors the WordPress core naming scheme from wp_unique_filename():
* file.jpg -> file-1.jpg -> file-2.jpg ... Candidates that exist locally
* are also skipped so the rename() in maybe_rename_for_remote() cannot
* clobber an existing local file. Gives up after 100 attempts and returns
* the original name.
*
* @since 1.5.1
*
* @param \Aws\S3\S3Client $client S3 client.
* @param string $meta_file Uploads-relative file path (e.g. 2026/10/file.jpg).
* @param string $basedir Uploads base directory.
* @return string A free file path, or the original when everything is taken.
*/
private function unique_remote_file( \Aws\S3\S3Client $client, string $meta_file, string $basedir ): string {
if ( ! $this->remote_key_exists( $client, $meta_file ) ) {
return $meta_file;
}
$name = basename( $meta_file );
$dot = strrpos( $name, '.' );
$ext = ( false === $dot ) ? '' : substr( $name, $dot );
$base = ( '' === $ext ) ? $name : substr( $name, 0, -strlen( $ext ) );
$dir = dirname( $meta_file );
$prefix = ( '.' === $dir ) ? '' : $dir . '/';
for ( $i = 1; $i <= 100; ++$i ) {
$candidate = $prefix . $base . '-' . $i . $ext;
if ( file_exists( path_join( $basedir, $candidate ) ) ) {
continue;
}
if ( ! $this->remote_key_exists( $client, $candidate ) ) {
return $candidate;
}
}
$this->logger->warning( 'No free remote file name found after 100 attempts, keeping original', array( 'file' => $meta_file ) );
return $meta_file;
}
/**
* Check whether an object key already exists in the remote bucket.
*
* Protected so unit-test doubles can stub the remote check without a
* live S3 connection.
*
* @since 1.5.1
*
* @param \Aws\S3\S3Client $client S3 client.
* @param string $key Object key.
* @return bool True when the key exists.
*/
protected function remote_key_exists( \Aws\S3\S3Client $client, string $key ): bool {
try {
$client->headObject(
array(
'Bucket' => $this->config->get_bucket(),
'Key' => $key,
)
);
return true;
} catch ( \Aws\Exception\AwsException $e ) {
if ( 404 === $e->getStatusCode()
|| in_array( $e->getAwsErrorCode(), array( 'NotFound', 'NoSuchKey' ), true ) ) {
return false;
}
// Connection or permission errors: assume the key is free so
// uploads are not blocked (matches the pre-1.5.1 behavior).
$this->logger->warning(
'headObject failed, assuming the key is free',
array(
'key' => $key,
'error' => $e->getAwsErrorMessage() ?? $e->getMessage(),
)
);
return false;
}
}
/**
* Schedule files for deletion via the cron queue.
*