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

Metadata Pathing bug when using ud_get_stateless_media()->add_media( false, id ); #343

Closed
jjtroberts opened this issue Jan 23, 2019 · 6 comments
Assignees
Labels
Milestone

Comments

@jjtroberts
Copy link

I'm programmatically doing the following via Ansible as part of a site migration playbook but some of the images are being uploaded to the wrong GCS bucket folder.

- name: Get post ids
  shell: |
    wp post list --post_type=attachment --fields=ID --format=json
  args:
    chdir: "{{ target_path }}"
  register: post_ids

- name: output post ids
  debug:
    msg: "{{ post_ids.stdout }}"

- name: load json into a variable
  set_fact:
    tmpJsonData: "{{ post_ids.stdout|from_json }}"

- name: process each post id
  shell: |
    wp eval 'ud_get_stateless_media()->add_media( false, {{ item.ID }} );' --allow-root --skip-themes
  args:
    chdir: "{{ target_path }}"
  with_items: "{{ tmpJsonData }}"

Some of the assets were uploaded correctly, while a large portion were uploaded to the GCS bucket and placed in a folder named "./" which is inaccessible except by using gsutil commands. Somehow the pathing in the metadata is off: "./demo-dev-assets/uploads/----->./<------"

Here's the serialized data below from one of the assets.

a:10:{s:2:"id";s:57:"demo-dev-assets/uploads/the-carlisle.png/1548280161189584";s:4:"name";s:24:"uploads/the-carlisle.png";s:8:"fileLink";s:71:"https://storage.googleapis.com/demo-dev-assets/uploads/the-carlisle.png";s:12:"storageClass";s:14:"MULTI_REGIONAL";s:9:"mediaLink";s:131:"https://www.googleapis.com/download/storage/v1/b/demo-dev-assets/o/uploads%2Fthe-carlisle.png?generation=1548280161189584&alt=media";s:8:"selfLink";s:84:"https://www.googleapis.com/storage/v1/b/demo-dev-assets/o/uploads%2Fthe-carlisle.png";s:6:"bucket";s:15:"demo-dev-assets";s:6:"object";a:28:{s:6:"bucket";s:15:"demo-dev-assets";s:12:"cacheControl";s:38:"public, max-age=36000, must-revalidate";s:14:"componentCount";N;s:18:"contentDisposition";N;s:15:"contentEncoding";N;s:15:"contentLanguage";N;s:11:"contentType";s:9:"image/png";s:6:"crc32c";s:8:"/iJY5g==";s:4:"etag";s:16:"CND9lIjxhOACEAE=";s:14:"eventBasedHold";N;s:10:"generation";s:16:"1548280161189584";s:2:"id";s:57:"demo-dev-assets/uploads/the-carlisle.png/1548280161189584";s:4:"kind";s:14:"storage#object";s:10:"kmsKeyName";N;s:7:"md5Hash";s:24:"blP6bmBTqfwEWyu6vMoRcQ==";s:9:"mediaLink";s:131:"https://www.googleapis.com/download/storage/v1/b/demo-dev-assets/o/uploads%2Fthe-carlisle.png?generation=1548280161189584&alt=media";s:8:"metadata";a:5:{s:5:"width";s:3:"700";s:6:"height";s:3:"138";s:9:"object-id";s:2:"73";s:9:"source-id";s:32:"a62dfee973e98e48006b2a823f537d8c";s:9:"file-hash";s:32:"060e2d06ec0bbd69305d2a4e913bff5b";}s:14:"metageneration";s:1:"1";s:4:"name";s:24:"uploads/the-carlisle.png";s:23:"retentionExpirationTime";N;s:8:"selfLink";s:84:"https://www.googleapis.com/storage/v1/b/demo-dev-assets/o/uploads%2Fthe-carlisle.png";s:4:"size";s:5:"29216";s:12:"storageClass";s:14:"MULTI_REGIONAL";s:13:"temporaryHold";N;s:11:"timeCreated";s:24:"2019-01-23T21:49:21.189Z";s:11:"timeDeleted";N;s:23:"timeStorageClassUpdated";s:24:"2019-01-23T21:49:21.189Z";s:7:"updated";s:24:"2019-01-23T21:49:21.189Z";}s:5:"sizes";a:2:{s:9:"thumbnail";a:5:{s:2:"id";s:69:"./demo-dev-assets/uploads/./the-carlisle-150x138.png/1548280161621004";s:4:"name";s:34:"uploads/./the-carlisle-150x138.png";s:8:"fileLink";s:81:"https://storage.googleapis.com/demo-dev-assets/uploads/./the-carlisle-150x138.png";s:9:"mediaLink";s:143:"https://www.googleapis.com/download/storage/v1/b/demo-dev-assets/o/uploads%2F.%2Fthe-carlisle-150x138.png?generation=1548280161621004&alt=media";s:8:"selfLink";s:96:"https://www.googleapis.com/storage/v1/b/demo-dev-assets/o/uploads%2F.%2Fthe-carlisle-150x138.png";}s:6:"medium";a:5:{s:2:"id";s:68:"./demo-dev-assets/uploads/./the-carlisle-300x59.png/1548280162021433";s:4:"name";s:33:"uploads/./the-carlisle-300x59.png";s:8:"fileLink";s:80:"https://storage.googleapis.com/demo-dev-assets/uploads/./the-carlisle-300x59.png";s:9:"mediaLink";s:142:"https://www.googleapis.com/download/storage/v1/b/demo-dev-assets/o/uploads%2F.%2Fthe-carlisle-300x59.png?generation=1548280162021433&alt=media";s:8:"selfLink";s:95:"https://www.googleapis.com/storage/v1/b/demo-dev-assets/o/uploads%2F.%2Fthe-carlisle-300x59.png";}}s:12:"cacheControl";s:38:"public, max-age=36000, must-revalidate";}
@jjtroberts
Copy link
Author

jjtroberts commented Jan 23, 2019

I'm wondering if I shouldn't be passing in "false" for the $metadata parameter as it gets used to generate the fileLink here: https://github.com/wpCloud/wp-stateless/blob/88a38db18ae69d7b5d2ec3a57df4a3ea468d2029/lib/classes/class-utility.php#L236

No, that's not it. Perhaps $file is getting unexpected output because I'm running roots bedrock?
https://github.com/wpCloud/wp-stateless/blob/88a38db18ae69d7b5d2ec3a57df4a3ea468d2029/lib/classes/class-utility.php#L188

@jjtroberts
Copy link
Author

$upload_dir[ 'basedir' ] gives this:

array(6) {
  ["path"]=>
  string(69) "/chroot/home/demo/blah/docroot/web/app/uploads"
  ["url"]=>
  string(47) "https://obfuscation.com/app/uploads"
  ["subdir"]=>
  string(0) ""
  ["basedir"]=>
  string(69) "/chroot/home/demo/blah/docroot/web/app/uploads"
  ["baseurl"]=>
  string(47) "https://obfuscation.com/app/uploads"
  ["error"]=>
  bool(false)
}

basedir is a symlink: uploads -> /home/demo/obfuscation.com/uploads

But when I execute this script in the environment, all seems as it should be:
wp eval-file sm.php

<?php

  $file = '';
  $upload_dir = wp_upload_dir();

  var_dump($upload_dir);

  $attachment_id = 73;
  $force=false;

  $fullsizepath = wp_normalize_path( get_attached_file( $attachment_id ) );

  var_dump($fullsizepath);

  $file = str_replace( wp_normalize_path(trailingslashit( $upload_dir[ 'basedir' ] )), '', $fullsizepath );

  var_dump($file);

array(6) {
["path"]=>
string(69) "/chroot/home/demo/blah/docroot/web/app/uploads"
["url"]=>
string(47) "https://obfuscation.com/app/uploads"
["subdir"]=>
string(0) ""
["basedir"]=>
string(69) "/chroot/home/demo/blah/docroot/web/app/uploads"
["baseurl"]=>
string(47) "https://obfuscation.com/app/uploads"
["error"]=>
bool(false)
}
string(86) "/chroot/home/demo/blah/docroot/web/app/uploads/the-carlisle.png"
string(16) "the-carlisle.png"

@jjtroberts
Copy link
Author

I rolled back wp-stateless to 2.1.5 and have been unable to replicate this behavior.

I re-updated to 2.2.1 and was able to reproduce the creation of a "./" directory in GCS when using the UI to sync. I tried again from clean install with transients and cache reset, same results.

image

@jjtroberts
Copy link
Author

I was going to start updating plugin versions incrementally, but upon updating from 2.1.5 to 2.1.6 this behavior showed up again.

@jjtroberts
Copy link
Author

jjtroberts commented Jan 24, 2019

2.1.5 on the left, 2.1.6 on the right: https://github.com/wpCloud/wp-stateless/blob/2.1.5/lib/classes/class-utility.php#L241
image

Running tests on these:

string(86) "/chroot/home/demo/obfuscation.com/docroot/web/app/uploads/the-carlisle.png"
string(16) "the-carlisle.png"

2.1.5: string(0) ""
2.1.6: string(1) "."

php > echo dirname("the-carlisle.png");
.

The end result is that all image sizes get uploaded to the "./" directory in GCS while the default image does not. Is this expected behavior?

@jjtroberts
Copy link
Author

jjtroberts commented Jan 24, 2019

I've tried this on a few other dev sites, some legacy, others running roots/bedrock. The problem only occurs on Bedrock-based sites and may have something to do with how it moves the uploads directory around. https://github.com/roots/bedrock

May not be a bedrock thing as it could simply be that the Wordpress option to stop storing files in year/month directories results in assets in uploads. And then dirname() returns a . when there are no slashes:

Returns the path of a parent directory. If there are no slashes in path, a dot ('.') is returned, indicating the current directory. Otherwise, the returned string is path with any trailing /component removed.
ref: http://php.net/manual/en/function.dirname.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants