Skip to content

Commit fc3540d

Browse files
swissspidyclaude
andcommitted
Keep the uploads path when resolving symlinked --skip-copy imports
realpath() resolved symlinks, so a relative path through a symlinked uploads directory, or to a symlinked file in it, was stored as the target path and broke the attachment URL. Resolve only the directory part and put the configured uploads basedir back in front when it is inside the real uploads directory. Also check get_attached_file() in the test, since the URL check alone already passes on main, and add a symlinked-uploads scenario. Based on the suggested change in softglazee's review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 2724e3d commit fc3540d

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎features/media-import.feature‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@ Feature: Manage WordPress attachments
157157
When I run `wp eval 'echo wp_get_attachment_url( {ATTACHMENT_ID} );'`
158158
Then STDOUT should match #/wp-content/uploads/codeispoetry\.png$#
159159

160+
When I run `wp eval 'var_export( file_exists( get_attached_file( {ATTACHMENT_ID} ) ) );'`
161+
Then STDOUT should be:
162+
"""
163+
true
164+
"""
165+
166+
Scenario: Import a file with --skip-copy using a relative path through a symlinked uploads directory
167+
Given download:
168+
| path | url |
169+
| {CACHE_DIR}/codeispoetry.png | http://wp-cli.github.io/behat-data/codeispoetry.png |
170+
And I run `wp option update uploads_use_yearmonth_folders 0`
171+
And I run `mkdir shared-uploads`
172+
And I run `cp {CACHE_DIR}/codeispoetry.png shared-uploads/codeispoetry.png`
173+
And I run `rm -rf wp-content/uploads`
174+
And I run `ln -s ../shared-uploads wp-content/uploads`
175+
176+
When I run `wp media import wp-content/uploads/codeispoetry.png --skip-copy --porcelain`
177+
Then save STDOUT as {ATTACHMENT_ID}
178+
179+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
180+
Then STDOUT should be:
181+
"""
182+
codeispoetry.png
183+
"""
184+
185+
When I run `wp eval 'echo wp_get_attachment_url( {ATTACHMENT_ID} );'`
186+
Then STDOUT should match #/wp-content/uploads/codeispoetry\.png$#
187+
160188
Scenario: Import a file and use its filename as the title
161189
Given download:
162190
| path | url |

‎src/Media_Command.php‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,7 @@ public function import( $args, $assoc_args = array() ) {
608608
}
609609
// With --skip-copy the path is stored as-is, so a relative one would not resolve against the uploads directory.
610610
if ( Utils\get_flag_value( $assoc_args, 'skip-copy' ) && ! Path::is_absolute( $file ) ) {
611-
$absolute_file = realpath( $file );
612-
if ( false !== $absolute_file ) {
613-
$file = $absolute_file;
614-
}
611+
$file = $this->get_absolute_path( $file );
615612
}
616613
$src_basename = Path::basename( $file );
617614
if ( Utils\get_flag_value( $assoc_args, 'skip-duplicates' ) ) {
@@ -2335,6 +2332,36 @@ private function get_real_attachment_url( $attachment_id ) {
23352332
return wp_get_attachment_url( $attachment_id );
23362333
}
23372334

2335+
/**
2336+
* Makes a relative path to a local file absolute, for imports with --skip-copy.
2337+
*
2338+
* Only the directory part goes through realpath(). When it is inside the real uploads
2339+
* directory, the configured uploads basedir is put back in front, so a symlinked uploads
2340+
* directory (or a symlinked file in it) gives the same result as an absolute path.
2341+
*
2342+
* @param string $path Relative path to an existing file.
2343+
* @return string
2344+
*/
2345+
private function get_absolute_path( $path ) {
2346+
$real_dir = realpath( dirname( $path ) );
2347+
if ( false === $real_dir ) {
2348+
return $path;
2349+
}
2350+
$real_dir = wp_normalize_path( $real_dir );
2351+
$absolute = trailingslashit( $real_dir ) . Path::basename( $path );
2352+
2353+
$basedir = wp_get_upload_dir()['basedir'];
2354+
$real_basedir = realpath( $basedir );
2355+
if ( false !== $real_basedir ) {
2356+
$real_basedir = wp_normalize_path( $real_basedir );
2357+
if ( 0 === strpos( trailingslashit( $real_dir ), trailingslashit( $real_basedir ) ) ) {
2358+
$absolute = $basedir . substr( $absolute, strlen( $real_basedir ) );
2359+
}
2360+
}
2361+
2362+
return $absolute;
2363+
}
2364+
23382365
/**
23392366
* Create image slug based on user input slug.
23402367
* Add basename extension to slug.

0 commit comments

Comments
 (0)