Index: media.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media/media.module,v
retrieving revision 1.66
diff -u -p -r1.66 media.module
--- media.module	13 Jan 2011 19:05:45 -0000	1.66
+++ media.module	13 Jan 2011 19:36:43 -0000
@@ -676,23 +676,40 @@ if (!function_exists('file_uri_to_object
    * Returns a file object which can be passed to file_save().
    *
    * @param $uri
-   *  A string containing the URI, path, or filename.
+   *   A string containing the URI, path, or filename.
+   * @param $use_existing
+   *   (Optional) If TRUE and there's an existing file in the {file_managed}
+   *   table with the passed in URI, then that file object is returned.
+   *   Otherwise, a new file object is returned.
    * @return
-   *  A file object, or FALSE on error.
+   *   A file object, or FALSE on error.
    */
-  function file_uri_to_object($uri) {
-    global $user;
-    $uri = file_stream_wrapper_uri_normalize($uri);
-    $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
-    $file = new StdClass;
-    $file->uid = $user->uid;
-    $file->filename = basename($uri);
-    $file->uri = $uri;
-    $file->filemime = file_get_mimetype($uri);
-    // This is gagged because some uris will not support it.
-    $file->filesize = @filesize($uri);
-    $file->timestamp = REQUEST_TIME;
-    $file->status = FILE_STATUS_PERMANENT;
+  function file_uri_to_object($uri, $use_existing = FALSE) {
+    if ($use_existing) {
+      $query = db_select('file_managed', 'f')
+        ->fields('f', array('fid'))
+        ->condition('uri', $uri)
+        ->execute()
+        ->fetchCol();
+      if (!empty($query)) {
+        $file = file_load(array_shift($query));
+      }
+    }
+    if (!$file) {
+      global $user;
+      $uri = file_stream_wrapper_uri_normalize($uri);
+      $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
+      $file = new StdClass;
+      $file->uid = $user->uid;
+      $file->filename = basename($uri);
+      $file->uri = $uri;
+      $file->filemime = file_get_mimetype($uri);
+      // This is gagged because some uris will not support it.
+      $file->filesize = @filesize($uri);
+      $file->timestamp = REQUEST_TIME;
+      $file->status = FILE_STATUS_PERMANENT;
+      $file->is_new = TRUE;
+    }
     return $file;
   }
 }
