Index: comment_upload.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/comment_upload/comment_upload.module,v
retrieving revision 1.6.2.11
diff -u -p -r1.6.2.11 comment_upload.module
--- comment_upload.module	13 May 2007 19:54:01 -0000	1.6.2.11
+++ comment_upload.module	30 Sep 2007 08:37:04 -0000
@@ -132,17 +132,56 @@ function comment_upload_form_alter($form
 function comment_upload_menu($may_cache) {
   $items = array();
   if ($may_cache) {
+
+    // Callback for JavaScript assisted uploads.
     $items[] = array(
       'path' => 'comment_upload/js',
       'callback' => 'comment_upload_js',
       'access' => user_access('upload files'),
       'type' => MENU_CALLBACK
     );
+
+    $items[] = array(
+      'path' => 'admin/settings/comment-upload-path',
+      'title' => t('Comment upload paths'),
+      'description' => t('Configure a prefix to apply to the path of uploaded files.'),
+      'access' => user_access('administer site configuration'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('comment_uploadpath_admin_settings'),
+      'type' => MENU_NORMAL_ITEM,
+    );
+
   }
   return $items;
 }
 
 /**
+ * Configuration callback for this module.
+ */
+function comment_uploadpath_admin_settings() {
+  $form = array();
+
+  $form['comment_uploadpath_prefix'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Pattern for the file prefix'),
+    '#description' => t('Specify the pattern to prefix to file names uploaded with the comment_upload module.  It will be appended after the site files directory (e.g., files) but before the file name itself.  Do not include a leading or trailing slash.  Spaces will be converted to underscores to avoid file system issues.'),
+    '#default_value' => variable_get('comment_uploadpath_prefix', ''),
+  );
+
+  $form['token_help'] = array(
+    '#title' => t('Replacement patterns'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['token_help']['help'] = array(
+    '#value' => theme('token_help', 'comment'),
+  );
+
+  return system_settings_form($form);
+}
+
+/**
  * Menu-callback for JavaScript-based uploads.
  */
 function comment_upload_js() {
@@ -209,6 +248,11 @@ function _comment_upload_save_files($com
     return;
   }
 
+  if (variable_get('comment_uploadpath_prefix', '')) {
+    comment_upload_change_upload_path($comment);
+  }
+
+
   foreach ($comment['files'] as $file) {
     $file = (object)$file;
 
@@ -339,3 +383,25 @@ function comment_upload_file_download($f
     }
   }
 }
+
+function comment_upload_change_upload_path(&$comment) {
+  $node = node_load($comment['nid']);
+  if (isset($comment['files'])) {
+    foreach ($comment['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
+        $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace(variable_get('comment_uploadpath_prefix', '') . '/', 'comment',  $comment)) . $comment['files'][$key]['filename'];
+
+        // Create the directory if it doesn't exist yet.
+        $dirs = explode('/', dirname($file_name));
+        $directory = file_directory_path();
+        while (count($dirs)) {
+          $directory .= '/' . array_shift($dirs);
+          file_check_directory($directory, FILE_CREATE_DIRECTORY);
+        }
+        // Change where the file will be saved to the specified directory.
+        $comment['files'][$key]['filename'] = $file_name;
+      }
+    }
+  }
+}
