--- uploadpath_o.module	2007-06-16 10:44:39.000000000 +1200
+++ uploadpath.module	2007-11-09 10:08:03.000000000 +1300
@@ -35,9 +35,19 @@ function uploadpath_admin_settings() {
 
   $form['uploadpath_prefix'] = array(
     '#type' => 'textfield',
-    '#title' => t('Pattern for the file prefix'),
+    '#title' => t('Default pattern for the file path prefix'),
     '#description' => t('Specify the pattern to prefix to file names uploaded with the 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('uploadpath_prefix', ''),
+    '#weight' => -9
+  );
+  
+  $form['node_types'] = array(
+    '#title' => t('Patterns for each node type'),
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#description' => t('Patterns for node types. If empty, the default pattern will be used.'),
+    '#weight' => -8
   );
   
   $form['token_help'] = array(
@@ -46,10 +56,22 @@ function uploadpath_admin_settings() {
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
     '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
+    '#weight' => -7
   );
   $form['token_help']['help'] = array(
     '#value' => theme('token_help', 'node'),
   );
+  
+  //textfields for node type settings
+  $node_types = node_get_types();
+  foreach($node_types as $type){
+  	$form['node_types']['uploadpath_prefix_'.$type->type] = array(
+    '#type' => 'textfield',
+    '#title' => t('Path pattern for '.$type->name),
+    '#description' => t('Specify the path pattern to prefix for '.$type->name.' type nodes'),
+    '#default_value' => variable_get('uploadpath_prefix_'.$type->type, ''),  	
+  	);
+  }
 
   return system_settings_form($form);
 }
@@ -63,8 +85,25 @@ function uploadpath_nodeapi(&$node, $op,
       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
+          	
+            //optional 
+            //bugfix: set node created and updated timestamp if not set for token replace
+            //see http://drupal.org/node/153737
+            if(!$node->created){$node->created = (int)time();}
+            if(!$node->changed){$node->changed = (int)$node->created;}
+            
+            //get the token path pattern
+            $pattern = variable_get('uploadpath_prefix_'.$node->type,false);
+            if(!$pattern){ 
+	            //default pattern
+	            $pattern = variable_get('uploadpath_prefix', '');
+            }
+            //Note, I tried variable_get('uploadpath_prefix_'.$node->type,variable_get('uploadpath_prefix', ''))
+            //but node type prefix always prevailed even if empty string so default wasn't called.
+            //above method works
+            
             // Get the new, prefixed file name
-            $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node)) . $node->files[$key]['filename'];
+            $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node)) . $node->files[$key]['filename'];
 
             // Create the directory if it doesn't exist yet.
             $dirs = explode('/', dirname($file_name));
