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

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

Merged
merged 10 commits into from
Jul 3, 2017

Conversation

phlbnks
Copy link

@phlbnks phlbnks commented Jun 21, 2017

Allow import of media from local filesystem without moving on disk

Fixes #24

@danielbachhuber
Copy link
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
Copy link
Author

phlbnks commented Jun 21, 2017

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

Copy link
Member

@danielbachhuber danielbachhuber left a comment

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.

@@ -174,6 +174,9 @@ function regenerate( $args, $assoc_args = array() ) {
* [--desc=<description>]
* : "Description" field (post content) of attachment post.
*
* [--import_only]
Copy link
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
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
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
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
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!

Copy link
Contributor

@gitlost gitlost Jun 22, 2017

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
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 :/

$post_array['post_status'] = 'inherit';

$success = wp_insert_attachment( $post_array, $file, $assoc_args['post_id'] );
if ( ! is_wp_error( $success ) ) {
Copy link
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
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?

$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
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
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
Copy link
Author

phlbnks commented Jun 27, 2017

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

@gitlost
Copy link
Contributor

gitlost commented Jun 27, 2017

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

@@ -69,6 +69,25 @@ Feature: Manage WordPress attachments
My fabulous caption
"""

Scenario: Import a file as attachment from a local image and leave it in it's current location
Given download:
Copy link
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
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
Member

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

@danielbachhuber
Copy link
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
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants