Skip to content

Add --skip-copy flag to allow import of media from local filesystem without moving on disk - #21

Merged
danielbachhuber merged 10 commits into
wp-cli:masterfrom
phlbnks:import_only
Jul 3, 2017
Merged

danielbachhuber merged 10 commits into
wp-cli:masterfrom
phlbnks:import_only

Conversation

@phlbnks

@phlbnks phlbnks commented Jun 21, 2017 •

Copy link
Copy Markdown

Allow import of media from local filesystem without moving on disk

Fixes #24

@danielbachhuber

Copy link
Copy Markdown
Member

@emirpprime Each pull request should only contain one conceptual change. Can you make sure this pull request only contains the specific change you're submitting?

Phil Banks added 2 commits June 21, 2017 18:17
@phlbnks

phlbnks commented Jun 21, 2017

Copy link
Copy Markdown
Author

@danielbachhuber Done, I think - I could only see that there was a WPCS commit in there accidentally.

@danielbachhuber danielbachhuber left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Couple of changes to start with. Still thinking this through generally.

Comment thread src/Media_Command.php Outdated
* [--desc=<description>]
* : "Description" field (post content) of attachment post.
*
* [--import_only]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you think about calling this --skip-copy or similar, to better communicate that we're skipping existing behavior?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes that makes sense to me too.

"""
Success: Imported 1 of 1 items.
"""
And the {CACHE_DIR}/large-image.jpg file should exist

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should also assert that there's no file in the new destination

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will do. I should get this updated later today.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@danielbachhuber I'm struggling to work out how to write this.
Can you point me ot any docs or examples that would help?

I guess I need to learn what Behat actually does with the image if it imports it? Does it move to {CACHE_DIR}/wp-content/uploads/year/month?
Then I think I'll need to create a new variable in FeatureContext.php for {UPLOADS_DIR} to use in the test. Is that about right?

Thanks!

@gitlost gitlost Jun 22, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should just need And the wp-content/uploads/large-image.jpg file should not exist - see media-regenerate.feature for examples.

Edit - Should add that Daniel is offline till Wednesday slack#cli.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks @gitlost, can't believe I didn't think to check in there :/

Comment thread src/Media_Command.php Outdated
$post_array['post_status'] = 'inherit';

$success = wp_insert_attachment( $post_array, $file, $assoc_args['post_id'] );
if ( ! is_wp_error( $success ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should switch the conditions and bail early on error.

So, instead of this:

if ( ! is_wp_error( $success ) ) {
    // Handle success.
}
if ( is_wp_error( $success ) ) {
    // Handle failure.
    continue;
}

you should prefer something like this:

if ( is_wp_error( $success ) ) {
    // Handle failure.
    continue;
}
// Handle success.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@schlessera I've updated the error handling part.

Since $success is used in areas these changes don't touch, shall I submit that as a seperate PR once this is resolved? Or is it ok to handle it in here?

Comment thread src/Media_Command.php
$post_array['post_mime_type'] = $wp_filetype['type'];
$post_array['post_status'] = 'inherit';

$success = wp_insert_attachment( $post_array, $file, $assoc_args['post_id'] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As the variable $success is being used several times here, and mostly refers to a "non-success", I would prefer the name to be changed to something like $result instead. Naming it $success seems misleading.

(Yes, I know that name was already in the code... 😄 )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can handle this in a subsequent PR.

@phlbnks

phlbnks commented Jun 27, 2017

Copy link
Copy Markdown
Author

I don't think that check fail was due to my code...

@gitlost

gitlost commented Jun 27, 2017

Copy link
Copy Markdown
Contributor

@emirpprime no it's ImageMagick doing its thing (randomly seg faulting) - trying to make it go away in #26

"""

Scenario: Import a file as attachment from a local image and leave it in it's current location
Given download:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

After Given download, you'll also need to run:

And I run `wp option update uploads_use_yearmonth_folders 0`

Otherwise, your file path assertion below will always be invalid, because WordPress will always used date-based folders.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@danielbachhuber danielbachhuber changed the title --import_only Add --skip-copy flag to allow import of media from local filesystem without moving on disk Jun 28, 2017
@danielbachhuber

Copy link
Copy Markdown
Member

@emirpprime Can you merge master to adopt the testing fix?

@danielbachhuber danielbachhuber added this to the 1.0.4 milestone Jul 3, 2017
@danielbachhuber danielbachhuber added command:media Related to 'media' command command:media-import Related to 'media import' command labels Jul 3, 2017
@danielbachhuber

Copy link
Copy Markdown
Member

Can you merge master to adopt the testing fix?

I did this:

$ git remote add emirpprime https://github.com/emirpprime/media-command.git
$ git fetch emirpprime
remote: Counting objects: 108, done.
remote: Total 108 (delta 64), reused 64 (delta 64), pack-reused 44
Receiving objects: 100% (108/108), 14.30 KiB | 0 bytes/s, done.
Resolving deltas: 100% (66/66), completed with 7 local objects.
From https://github.com/emirpprime/media-command
 * [new branch]        allow_unsafe -> emirpprime/allow_unsafe
 * [new branch]        import_only  -> emirpprime/import_only
 * [new branch]        master       -> emirpprime/master
 * [new branch]        wpcs         -> emirpprime/wpcs
$ gco import_only
Branch import_only set up to track remote branch import_only from emirpprime.
Switched to a new branch 'import_only'
$ gm master
Auto-merging src/Media_Command.php
Auto-merging features/media-import.feature
Merge made by the 'recursive' strategy.
 bin/install-imagick.sh        |  6 +++---
 features/media-import.feature | 16 ++++++++--------
 src/Media_Command.php         | 14 ++++++++++++--
 3 files changed, 23 insertions(+), 13 deletions(-)
$ gp emirpprime
Counting objects: 22, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (22/22), 2.72 KiB | 0 bytes/s, done.
Total 22 (delta 13), reused 0 (delta 0)
remote: Resolving deltas: 100% (13/13), completed with 7 local objects.
To https://github.com/emirpprime/media-command.git
   6c763ecf..054115d1  import_only -> import_only

@danielbachhuber
danielbachhuber dismissed schlessera’s stale review July 3, 2017 14:05

Can handle the issue in a subsequent PR

@danielbachhuber

Copy link
Copy Markdown
Member

Thanks for your work on this, @emirpprime !

@danielbachhuber
danielbachhuber merged commit a43c457 into wp-cli:master Jul 3, 2017
@phlbnks
phlbnks deleted the import_only branch July 10, 2017 08:33
danielbachhuber added a commit that referenced this pull request Nov 18, 2022
Add `--skip-copy` flag to allow import of media from local filesystem without moving on disk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:media Related to 'media' command command:media-import Related to 'media import' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants