While testing for issue http://drupal.org/node/1933596, I discovered that plup produces paths like:
public:///image.png or private:///image.png - note the triple slashes - in database table file_managed.
But only if the original files go directly to the root of either private or public file system.
Triple slashes do not occur, if files go to subdirectories (field settings).

That seemed to be no problem until Drupal 7.20, but suddenly derivative images cannot be created anymore.
The problem with missing derivative images when triple slashes appear, is already fixed in drupal dev (not released yet).
Shouldn't we try to set the correct path in database?
like:
scheme://filename.ext (two slashes)

An idea would be to wrap file_stream_wrapper_uri_normalize() around token_replace() in line 627 in plup.module (function plup_field_attach_presave())

But there might be better ways to set the correct path.

Comments

sam152’s picture

I had this exact issue. This caused me big grief when using filefield_sources‎. You can guess what happens when you do something like:

    $fid = db_select('file_managed', 'f')
      ->condition('uri', rawurldecode($uri))
      ->fields('f', array('fid'))
      ->execute()
      ->fetchField();

I have added the patch I committed to my site which resolved the issue. I can't see any ill effects, I do however feel line 625 (or the whole function) could be a bit better commented. Does that whole hook seem like a hack? Can anyone with a bit of file API experience look into that.

sam152’s picture

Status: Active » Needs review
indigoxela’s picture

Looks good!

Tested on a fresh Drupal 7.23 install with filefield_sources and imce modules and two content types with images. One with plup widget, the other with filefield_sources.

I can confirm that the patch works as expected - images uploaded with plup can be selected and saved with filefield_sources.
Without patch the formerly uploaded images can be found when browsing in imce's dialog box, but not used in the image field - error message:
"The selected file could not be used because the file does not exist in the database".
(It does exist in the database, but has a triple slash.)

Many thanks for your patch.

indigoxela’s picture

Problem occurs when mixing subdirectory and root image field paths.

I was wondering, why that slash was added in function plup_field_attach_presave - it IS necessary.
The slash is missing, if there is an image field with plupload widget in a subdirectory.
The path created looks like: public://subdirImagename.jpg - no slash between directory name and image name.

So we have to check if file_directory setting is empty (as far as I can see, it is always set - but I might be wrong).
That would result in something like:
$dir = !empty($instance['settings']['file_directory']) ? $instance['settings']['file_directory'] . '/' : '';

sam152’s picture

Aren't we back to square one then, since public isn't empty you still get public:///. How about what you suggested before:

$dir = isset($instance['settings']['file_directory']) ? file_stream_wrapper_uri_normalize($instance['settings']['file_directory'] .'/') : '';

Which would produce outputs like the below?

  • ''
  • 'public://'
  • 'public://subdir/

Haven't tested or anything. For now my immediate issue is solved, might revisit later if I have time.

indigoxela’s picture

Hi PerthSam152,
nope, not the same.

We should use !empty() instead of isset().

Variable $instance['settings']['file_directory'] is always set, but can be empty.

If we test on isset(), it will always be true, so a slash is always added.

If we test on !empty(), the slash is only added to subdirectories, where it is needed, but not if images go directly to root.

indigoxela’s picture

StatusFileSize
new750 bytes

Here is my patch, which uses !empty() instead of isset(). So we add a slash only to image paths in subdirectories, but not to images in root of Drupals filesystem.

Please test it with filefield_sources module.

The patch is against 7.x-1.0-alpha1+5-dev because master seems broken.