--- dadaimc2drupal.orig/operations/base.batch.inc	2010-11-15 01:09:19.000000000 +0100
+++ dadaimc2drupal/operations/base.batch.inc	2010-12-26 16:24:29.000000000 +0100
@@ -381,34 +381,98 @@ class BaseBatchOperation extends Databas
    *
    */
   function dadaimc2drupal_urlpath($path) {
-    return '/'. file_directory_path() .'/usermedia/'. $path;
+    return '/'. $this->file_directory_path() .'/usermedia/'. $path;
   }
 
   /**
    *
    */
   function dadaimc2drupal_uploadpath($path) {
-    return file_directory_path() .'/usermedia/'. $path;
+    return $this->file_directory_path() .'/usermedia/'. $path;
   }
 
   /**
    *
    */
   function dadaimc2drupal_filepath($mime_class, $filename) {
-    return $mime_class .'/'. dadaimc2drupal_subdir($filename) .'/'. $filename;
+    return $mime_class .'/'. $this->dadaimc2drupal_subdir($filename) .'/'. $filename;
   }
 
   /**
    *
    */
   function dadaimc2drupal_filepath_thumbnail($mime_class, $filename) {
-    return $mime_class .'/'. dadaimc2drupal_subdir($filename) .'/thumb/'. $filename;
+    return $mime_class .'/'. $this->dadaimc2drupal_subdir($filename) .'/thumb/'. $filename;
   }
 
   /**
    *
    */
   function dadaimc2drupal_filepath_large($mime_class, $filename) {
-    return $mime_class .'/'. dadaimc2drupal_subdir($filename) .'/large/'. $filename;
+    return $mime_class .'/'. $this->dadaimc2drupal_subdir($filename) .'/large/'. $filename;
+  }
+  function file_directory_path() {
+    return variable_get('dadaimc2drupal_path', NULL);
+  }
+  
+  /* add media to an file/imagefield
+   * id is either objectid of media or id of parentarticle
+   * aftersummary is 'media' if media
+   */
+  function add_media_to_field(&$node, $id, $aftersummary = '') {
+    if ($aftersummary == 'media') {
+      $sql = "SELECT * FROM media WHERE parentid=%d AND filename <> '' ORDER BY sequence_number %s";
+    } else {
+      $sql = "SELECT * FROM media WHERE objectid=%d AND filename <> '' ORDER BY sequence_number %s";
+    }
+    $mq = $this->dbQuery($sql, $id, ($aftersummary == 'aftersummary' ? "DESC" : "ASC"));
+    if ($mq)
+      while ($media = mysql_fetch_array($mq)) {
+        //get absolute path
+        $fpabs = $this->dadaimc2drupal_urlpath($this->dadaimc2drupal_filepath($media['mime_class'], $media['filename']));
+    
+        if (file_exists($fpabs)) {
+          $filepath = "usermedia/".$this->dadaimc2drupal_filepath($media['mime_class'], $media['filename']);
+          $timestamp = strtotime($media['created_datetime']);
+          $file = $this->add_existing_file($filepath, $timestamp, 0);
+
+          watchdog('dadaimc2drupal', t("linking %filename to node %nid"), array('%filename' => $filepath, '%nid' => $node->nid), WATCHDOG_DEBUG);
+
+          if ($media['mime_class'] == 'image') {
+            $this->add_image_to_image_field(&$node, &$file, $media['orig_filename'], $media['caption']);
+          } else {
+            $this->add_file_to_file_field(&$node, &$file);
+          }
+        }
+    }
+  }
+
+  function add_image_to_image_field(&$node, &$file, $alt = '', $title = '') {
+//  mkdir(dirname($fp), 0644, TRUE); 
+
+    $file['list'] = 1;
+    $file['data'] = array('alt'=> $alt, 'title' => $title);
+    $node->field_picture[] = $file;
+    node_save($node);
+  }
+
+  function add_file_to_file_field(&$node, &$file) {
+    $node->field_media[] = $file;
+    node_save($node);
+  }
+
+  function add_existing_file($file_path, $timestamp, $uid=1) { 
+    $file = (object)array(
+      'filename' => basename($file_path),
+      'filepath' => $file_path,
+      'filemime' => file_get_mimetype($file_path),
+      'filesize' => filesize($file_path),
+      'origname' => basename($file_path),
+      'uid' => $uid,
+      'status' => FILE_STATUS_PERMANENT,
+      'timestamp' => $timestamp
+    );
+  drupal_write_record('files', $file);
+  return field_file_load($file_path);
   }
 }
