The submit operation in hook_nodeapi() triggers too early, meaning you don't have access to node id nor node creation date when the node is created. This defeats the purpose of your module.

You should replace your hook_nodeapi() with the following:

function uploadpath_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      if (user_access('upload files')) {
          if (isset($node->files)) {
            foreach ($node->files as $key => $file) {
              if (0 === strpos($key, 'upload_')) {  // Only rewrite the name when adding the file, not when updating it
                // Get the new, prefixed file name
            
                $tokenized = token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node);
                $tokenized = str_replace(array(' ', "\n", "\t"), '_', $tokenized);
                $dest =  $tokenized . $file->filename;

                // Create the directory if it doesn't exist yet.
                $dirs = explode('/', dirname($dest));
                $directory = file_directory_path();
                while (count($dirs)) {
                  $directory .= '/' . array_shift($dirs);
                  file_check_directory($directory, FILE_CREATE_DIRECTORY);
                }

                // move file on filesystem
                if (file_move($file->filepath, $dest, FILE_EXISTS_REPLACE)) {
                    $node->files[$key] = $file;
                    // update existing file in database
                    db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file->filepath, $file->fid);
                }
              }
            }
          }
      }
      break;
  }
}

It will just move the file after the file has been saved by the upload module itself which is how it should be done anyway in my opinion.

Comments

Crell’s picture

Priority: Critical » Normal

Please submit a proper patch so that it can be reviewed. Thanks.

Also, I've not run into a problem so far, and the module does seem to work in at least some cases. In what cases do you see a bug?

mansion’s picture

You will run into the problem when you create a new node and you use the node creation date as tokens for your upload path because the node is not created yet, it doesn't have a node creation date. So the node should be saved first, then its attached files should be saved, then its attached files should be moved according to uploadpath settings.

Current use of 'submit' operation in nodeapi hook makes it impossible to use some tokens.
While not as fast because files must be moved twice instead of once, it is much more logical and compliant with nodeapi.

Crell’s picture

I see. Can you provide a patch?

http://drupal.org/patch/create

bsuttis’s picture

I can confirm this works much better, now I can assign a taxonomy term to a node and add a file all without having to first submit and re-edit to get the proper subdirectory for my files.

Thanks a lot, workflow much faster now.

Christefano-oldaccount’s picture

Status: Active » Needs review
StatusFileSize
new2.54 KB

This will be useful for something I'm working on. I haven't tested mansion's proposed changes yet but I rolled a patch.

nath’s picture

I will try this patch.
Hmm, it is for a very old version, so needs some changes to work.

whisk’s picture

StatusFileSize
new3.99 KB

Prepared a patch for uploadpath 1.1. Haven't tested it with "clean filenames" setting, but it should be ok.

merilainen’s picture

I get this error:

Looks like a unified context diff.
I can't seem to find a patch in there anywhere.

D'oh?

merilainen’s picture

I did it manually, but it doesn't replace the name of the document.

I uploaded testdocument.odt and gave my Document a title "My test document". Then the final file name is: testdocument_odt_48b698d6a1.odt instead of my_test_document_odt_48b698d6a1.odt.

Or have I misunderstood something?

whisk’s picture

I used following instructions http://drupal.org/node/324 to create the patch. What utility do you use? Does anybody have same problems?

merilainen’s picture

I tried with patch command in Unix and that gave me the error.
In Eclipse I managed to apply it also, but now somehow I messed up the workspace and I cannot try again.

davidwhthomas’s picture

Status: Needs review » Closed (fixed)

fixed in Drupal 6 version