wrong url in "File attachments"

lewmich - June 27, 2007 - 09:38
Project:Upload path
Version:6.x-1.0
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

When you attach a file to a node, you go to "File attachments", browse file, click attach.... after uploading the file under Description there is an url to the attached. We can copy this url and paste it in Body of our node (for example to make additional link).

The problem is that this url doesn't take into account that "Upload path" is being used. So this url shows for example:

http://domain.com/files/nissan.jpg

but after submiting a node the attachment is saved in

http://domain.com/files/blog/user/nissan.jpg

So the first url showed under Description can only confuse the user. Can we block showing that url or change somehow to show the correct one?

#1

David Stosik - August 14, 2007 - 10:01

Hello,
I need this too.

Thanks,
David

#2

dennys - August 24, 2007 - 17:06

Please consider to fix it. Although the function still works, but users will be confused.

#3

Crell - August 24, 2007 - 21:16

This is a known issue. However, I really don't know how to fix it given the way the upload module works. :-/ If anyone has suggestions on how to do so, please share. (Or a patch would be even better.)

#4

dennys - August 25, 2007 - 10:30

A stupid way: modify modules/upload/upload.module, remark the 2 lines 777,778 and user will NOT see the url. If they doesn't see it, then they will not be confused :p

      //$description = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
      //$description = "<small>". check_plain($description) ."</small>";

But I think if we need this function, maybe we need to modify upload module, not upload path module.

#5

dennys - August 25, 2007 - 12:52

I tried to modify upload.module, it seems ok.

old:

      //$description = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));

new:

      $description = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, token_replace(variable_get('uploadpath_prefix', ''), 'node', $node))));

But I found another issue, I can use "u[author-uid]" as the prefix in upload path module, but I cannot use it in upload module. Is it due to the node is not generated yet, so the [author-uid] is null?
When I change to use "u[user-id]" as the prefix in upload path, it's ok in both upload and upload path.

#6

Crell - August 27, 2007 - 15:29

Thanks dennys, although modifying core is not something this module can do. :-) As for the other issue, please file that as a new issue. It's very hard to track issues that are mixed into one thread. Thanks.

#7

Murz - October 13, 2008 - 14:34

Subscribe. Will be good to see the real path in state after uploading file, but I don't know how to do it without hacking the upload.module. Maybe use a hook_form_alter() for upload file submitting form?

#8

livingegg - December 31, 2008 - 22:01

Subscribing.

#9

joshvalentin - February 12, 2009 - 16:44

I changed upload.module as well to solve this, using a str_replace to replace "files/" with "files/".$usersrole where $usersrole is the logged in user's role. This custom code was on the line after the one stated above, where I use the str_replace on the $description=" line. I have the uploadpath module customized to upload files to user's ROLES. Using a customized token module with uploadpath and changing the upload.module file, I have a nice setup that works for Drupal 5.7.

#10

David Stosik - February 14, 2009 - 13:12

#11

jureb - June 12, 2009 - 20:25
Version:5.x-1.x-dev» 6.x-1.0

Is this feature of correct filepath at upload (and before the node post) really impossible to somehow get implemented in Uploadpath module?

I just installed Uploadpath module and before that moved all the old files to new locations, changed all the paths in files table, corrected all the urls in node_revisions table and it all works perfectly, besides this feature - my problem is, that I have a lot of users on my site and majority will have problems with inserting images. Yes, I'll make some banner on the content/add page, but this feature would be far better solution. If there is any way to help.

At least someone gives me the change of Drupal 6.x upload module (even though I wouldn't like to change core).

#12

Deciphered - June 18, 2009 - 01:58

One solution would be to have the module check the Body field for the old paths and replace them with the new paths after relocating/renaming the files.

While it would be great to know the correct path on upload, node tokens are not set in stone until the node has been saved.

#13

bjorpe - July 1, 2009 - 21:31

I started working on an uploadpath_form_alter hook to avoid hacking core. It's possible to get working for simple cases.

The major problem with finding a gereral solution is that lots of tokens will not work when adding a new node (node object/information is missing). Possibly some node information could be gathered in a temporary node object, but it world not be 100% correct.

Also, it would be good for implementing this to extract the rewriting code of uploadpath_nodeapi into a separate function.

Here's the code if anyone wants to have a try (working with only a standard prefix):

function uploadpath_form_alter($form_id, &$form) {
  if ($form_id == 'upload_js') {
    if (isset($_POST['vid']) && is_numeric($_POST['vid'])) {
      //Load the node object
      $node = node_load(array('vid' => $_POST['vid']));
    }
    else {
      // This is a new node.
      $node = new stdClass();
    }
    // Alter the description of all newly uploaded files
    for ($i=0; isset($form['files']['upload_'.$i]); $i++) {
      $description = file_create_url(file_create_filename($form['files']['upload_'.$i]['filename']['#value'], token_replace(variable_get('uploadpath_prefix', ''), 'node', $node)));
      $form['files']['upload_'.$i]['description']['#description'] = $description;
    }
  }
}

#14

AliraSirin - November 24, 2009 - 11:04

Thanks for the idea, I made some tweaking with bjorpe's code and now it really works for default token [type/yyyy/mm] and any other token which contains only type and/or date items. Someone may proceed with tweaking and at least add other type-based tokens support (now it is possible).

function uploadpath_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'upload_js') {
  if (isset($_POST['vid']) && is_numeric($_POST['vid'])) {
      //Load the node object
      $node = node_load(array('vid' => $_POST['vid']));
    }
    else {
      // This is a new node.
      $node = new stdClass();
      $node->type=str_replace('_node_form', '', $form["#post"]["form_id"]);
      $node->created=time();
    }
  // Alter the description of all newly uploaded files
    foreach ($form['files'] as $k=>$v) {
      if (is_numeric($k)){
        $description = file_create_url(file_create_filename($form['files'][$k]['filename']['#value'], token_replace(variable_get('uploadpath_prefix', '[type]/[yyyy]/[mm]'), 'node', $node)));
        $form['files'][$k]['description']['#description'] = $description;
      }
    }
  }
}

 
 

Drupal is a registered trademark of Dries Buytaert.