When I use private files download the thumb and the image when viewing a node gets the wrong url.

The thumb URL when uploading an image is expected to be:
http://www.example.com/?q=system/files/imagefield_thumbs/filename.png&12...
I'm getting this:
http://www.example.com/?q=system/files/C%253A/ABSOLUTE_PATH_TO_PRIVATE_F...

When viewing the node I'm expecting the image to get this URL
http://www.example.com/?q=system/files/filename.png&1286050779
I'm getting this:
http://www.example.com/?q=system/files/C%253A/ABSOLUTE_PATH_TO_PRIVATE_F...

Config:
Default settings for Image widget

OS Windows
Drupal 6.19
CCK 6.28
Imagefield 3.7
File permission OK
Private file download

Luckely I have 7 lines of code that fixes this.
In imagefield.module:

function theme_imagefield_image(...) {
...

Add these 4 lines

if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
    $short_path = preg_replace('/^' . preg_quote(file_directory_path(), '/') . '/', '', $file['filepath']);
    $file['filepath'] = $short_path;
}

// Encode the parts of the path.
$parts = explode('/', $file['filepath']);
...

In imagefield_file.inc:

function imagefield_file_admin_thumb_path(...) {
...

Add these 3 lines

if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
    $filepath = 'imagefield_thumbs' . $short_path;
}

return $filepath;
}

That should hopefully do the trick.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Title: imagefield is not using private file download correctly » ImageField thumbnail has wrong path when using private files and Windows

Okay after some poking around I'm pretty sure I found out why this is occurring. The file_create_url() function should automatically strip off the absolute path of private file paths. See http://api.drupal.org/file_create_url.

However what's happening here is that ImageField encodes each part of the path (including the "C:/" prefix) before it calls file_create_url(). Since the file directory prefix is encoded, it doesn't match the return of file_directory_path() and it doesn't get stripped out.

So in other words, we shouldn't be encoding the file directory path, only the parts after the prefix. I'll see if I can put together a patch which accomplishes this fix.

quicksketch’s picture

Status: Active » Fixed
FileSize
1.25 KB

Okay my final result was something pretty similar to what you had suggested: strip off the prefix before calling file_create_url(). I just did it a bit more explicitly and in a different variable rather than modifying $file['filepath'] directly. Please reopen if this patch does not seem to fix the problem.

quicksketch’s picture

Title: ImageField thumbnail has wrong path when using private files and Windows » Images have wrong path when using private files and absolute Windows file directory

Clarifying title slightly.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.