From a0fa65f68ef62a09584176abd6563a5c4678bf2f Mon Sep 17 00:00:00 2001
From: Aaron Winborn <aaron@aaronwinborn.com>
Date: Thu, 10 Mar 2011 13:02:01 -0500
Subject: [PATCH] Issue #1065462 by aaron: Define function file_styles_uri_to_object

---
 contrib/file_styles/file_styles.module |   45 +++++++++++++++++++++++++++++++-
 1 files changed, 44 insertions(+), 1 deletions(-)

diff --git a/contrib/file_styles/file_styles.module b/contrib/file_styles/file_styles.module
index 2a8c362..f2224fb 100644
--- a/contrib/file_styles/file_styles.module
+++ b/contrib/file_styles/file_styles.module
@@ -30,7 +30,7 @@ function file_styles_styles_register() {
  */
 function file_styles_styles_filter($object, $element = NULL) {
   // Ensure we're working against the fully loaded file object.
-  $file = file_uri_to_object($object->uri);
+  $file = file_styles_uri_to_object($object->uri, TRUE);
 
   // Allow other modules to define their own file styles.
   // In general, they'll most likely want to check against the mimetype.
@@ -108,3 +108,46 @@ function file_styles_theme($existing, $type, $theme, $path) {
     ),
   );
 }
+
+/**
+  * Returns a file object which can be passed to file_save().
+  *
+  * @param $uri
+  *   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.
+  *
+  * @see http://drupal.org/node/685818
+  */
+function file_styles_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 (!isset($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;
+}
-- 
1.7.0.4

