? 20081114-image_add.patch
Index: image_pub.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image_pub/Attic/image_pub.module,v
retrieving revision 1.11.2.7
diff -u -p -r1.11.2.7 image_pub.module
--- image_pub.module	18 Oct 2008 22:20:57 -0000	1.11.2.7
+++ image_pub.module	14 Nov 2008 23:00:45 -0000
@@ -208,6 +208,7 @@ function _image_pub_album_add($title, $d
   return NULL;
 }
 function _image_pub_image_add($album, $caption, $description, $srcfield) {
+  global $user;
   if (!isset($_FILES[$srcfield])) {
     return array('success' => FALSE,
                  'reason' => 'Forgetting a file?');
@@ -223,47 +224,35 @@ function _image_pub_image_add($album, $c
     }
   }
 
-  // move the file into the drupal filesystem
-  $filelocation = $_FILES[$srcfield]['tmp_name'];
-  $filename = basename($_FILES[$srcfield]['name']);
-
-  if (file_exists(file_directory_temp() . '/' . $filename)) {
-    $filename = file_create_filename($filename, variable_get('image_default_path', 'images'));
-  }
-  if (!move_uploaded_file($filelocation, file_directory_temp() . '/' . $filename)) {
-    return array('success' => FALSE, 'reason' => 'File upload error. Could not move uploaded file.');
+  // Create a drupal node
+  // In order for the image upload to work, the name, tmp_name and error 
+  // of the $_FILES[$srcfield] have to be copied to locations
+  // that file_check_upload() recognises
+  $_FILES['files']['name']['image'] = $_FILES[$srcfield]['name'];
+  $_FILES['files']['tmp_name']['image'] = $_FILES[$srcfield]['tmp_name'];
+  $_FILES['files']['error']['image'] = $_FILES[$srcfield]['error'];
+
+  $node = array('type' => 'image');
+  $values['title'] = $caption;
+  $values['uid'] = $user->uid;
+  $values['name'] = $user->name;
+  $values['body_filter']['body'] = $description;
+  $redirect_url = drupal_execute('node_form', $values, $node, NULL);
+
+  // Check that the uploaded image was accepted.
+  $errors = form_get_errors();
+  if (!empty($errors)) {
+    return array('success' => FALSE,
+                 'reason' => implode($errors, '. '));
   }
 
-  // create a drupal node
-  global $user;
-  $node = new StdClass();
-  $node->uid = $user->uid;
-  $node->name = $user->name;
-  $node->type ='image';
-  $node->title = $caption;
-  $node->body = $description;
-  $node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
-  
-  $node_options = variable_get('node_options_image', array('status', 'promote'));
-  foreach (array('status', 'promote', 'sticky', 'revision') as $key) {
-    $node->$key = in_array($key, $node_options) ? 1 : 0;
-  }
-  $node->taxonomy = array($album->tid);
-  $node->images['_original'] = file_directory_temp() . '/' . $filename;
-
-  if ($node = node_submit($node)) {
-    // let the image module generate the preview/thumbnail
-    _image_build_derivatives($node, true);
-  
-    // save the node
-    node_save($node);
-
-    // delete the temporary file
-    @unlink(file_directory_temp() . '/' . $filename);
-  }
-  // FIXME: check that the uploaded image was accepted.
-  watchdog('image_pub', t('%image was added.', array('%image' => $filename)));
-  return array('success' => TRUE, 'node' => $node);
+  // If it was accepted, tag it with the the album
+  $arguments = explode('/', $redirect_url);
+  $nid = $arguments[1];
+  taxonomy_node_save($nid, array($album->tid));
+
+  watchdog('image_pub', t('%image was added.', array('%image' => $_FILES[$srcfield]['name'])));
+  return array('success' => TRUE, 'redirect_url' => $redirect_url);
 }
 
 
@@ -615,7 +604,7 @@ function _image_pub_gr_add_image($albnam
   $result = _image_pub_image_add($album, $caption, $description, 'userfile');
 
   if (!$result['success']) {
-    _image_pub_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL, $result['reason']);
+    _image_pub_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL, $body, t('The file was received, but could not be processed or added to the album.') . ' ' . $result['reason']);
   } else {
     _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Added node '.$result['node']->nid);
   }
