$filepath within the function _asa_get_attachment_text() is handled incorrectly and will not work with private file structures. I've utilized filefield's field_file_load() function to prove the concept. We can simply set filepath to $file['filepath'] when passed in this context. I will try to write up a more friendly patch for this today or tomorrow and see if we can get it working properly.

Eclipse

CommentFileSizeAuthor
#5 private_files.patch1 KBeclipsegc

Comments

pwolanin’s picture

Maybe we shoudl use file_create_path()?

http://api.drupal.org/api/function/file_create_path/6

somebodysysop’s picture

Here is the issue, I believe:

I use private_upload.module which places private files in a "private" subdirectory of "files" directory. This module requires a "public" filesystem to work it's magic. From the nodes to which they are attached, these files are accessed using this link: "/system/files/private/".

However, when apachesolr_attachments lists these files, the links only have:

"/files/private/"

Therefore, all of these files return "access denied" when you try to link to them from apachesolr_attachments.

@EclipseGc: Did you ever complete that patch? Is it working? If so, could you provide it here?

somebodysysop’s picture

Any suggestions for this issue? Again, files that are not in the default "files" directory are indexed, but the link that apachesolr_attachments displays are incorrect.

pwolanin’s picture

EclipseGC has promised a patch...

eclipsegc’s picture

Assigned: Unassigned » eclipsegc
Status: Active » Needs review
StatusFileSize
new1 KB

Patch as promised:

pwolanin’s picture

do we need the leading '.'?

Seems like we might also be able to fix this using $filepath = realpath($file->filepath); ?

pwolanin’s picture

I think this was fixed by http://drupal.org/node/490078 - please confirm

pwolanin’s picture

I think this is fixed - can anyone confirm?

somebodysysop’s picture

It did not resolve the issue as far as private file links are concerned. Even with the patch, the url "files/private" is still returned, and users cannot access the file even if they have permission to do so.

I used hook_apachesolr_process_results() to ultimately fix the problem:

function mymodule_apachesolr_process_results(&$results) {

  foreach ($results as &$item) {
    if (isset($item['node']->ss_filemime)) {
      // Modification for private files
      $file_directory_path = variable_get('file_directory_path', 'files');
      if (preg_match('/private/', $item['link'])) {
        $workfilepath = str_replace($file_directory_path, 'system/files', $item['link']);
	    $item['link'] = $workfilepath;
      }
    }
  }
}

I do not know what other issue the patch was designed to fix.

pwolanin’s picture

so the existing code is:

      $item['link'] = file_create_url($item['node']->path);

perhaps your problem is with the logic in this function? http://api.drupal.org/api/function/file_create_url/6

Are you using a mix of public and private files?

somebodysysop’s picture

Are you using a mix of public and private files?

It is a mix. But, upon greater reflection, I think the existing code is fine. The url created is, under normal circumstances, correct.

The private_upload.module appears to do some fancy footwork in nodeapi in order to correctly display public and private files attached to the same node:

/**
 * Create a URL for the file that changes if the file is public or private.
 * TODO - Push to get a file_create_url hook into Drupal7.
 *
 * @param file object $file
 * @return str: the correct URL
 */
function _private_upload_create_url($file) {
  if (_private_upload_is_file_private($file->filepath)) {
    $download_method = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC); // this should be PUBLIC, but don't break misconfigured systems
    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
  }
  // Generate valid URL for both existing attachments and preview of new attachments (these have 'upload' in fid)     
  $href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
  if (_private_upload_is_file_private($file->filepath)) {
    variable_set('file_downloads', $download_method);
  }
  return $href;
}

The apachesolr_attachments.module can't be expected to support this.

I will instead recommend that the private_upload.module incorporate hook_apachesolr_process_results() to accomodate the necessary file url changes.

So, unless someone else has a problem with the apachesolr_attachments code, I would say this issue is fixed.

Thanks for the assistance here!

pwolanin’s picture

Version: 6.x-2.x-dev » 6.x-1.x-dev
Status: Needs review » Fixed

Ok, so it's fixed?

Status: Fixed » Closed (fixed)

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

Ajtis.pl’s picture

Issue summary: View changes

Fix it in
template.php

**
 * Implements template_preprocess_search_result
 * @param type $vars
 */
function {themename}_preprocess_search_result(&$variables)
{
        if($variables['result']['fields']['entity_type']=='file'){
            $node= file_load($result->entity_id);
            $uri = file_entity_download_uri($node);
            $variables['result']['link']=$uri;
        }
}