From 41d2b2a349564c7a55df9bc511c36e3889d4558d Mon Sep 17 00:00:00 2001
From: Kjartan Mannes <kjartan@2.no-reply.drupal.org>
Date: Sat, 27 Dec 2003 19:21:48 +0000
Subject: [PATCH] - Fixed fileuploads for nodes failing without preview.

---
 includes/file.inc              | 26 +++++++++++++++++---------
 modules/profile.module         |  2 +-
 modules/profile/profile.module |  2 +-
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/includes/file.inc b/includes/file.inc
index 963dce118940..f98b18f46743 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -14,7 +14,7 @@
  *
  *     function module_insert($node) {
  *       if ($node->file) {
- *         file_save_upload($node->file);
+ *         $file = file_save_upload($node->file);
  *       }
  *     }
  *   @endcode
@@ -97,12 +97,18 @@ function file_check_path(&$path) {
  * @param $source
  */
 function file_check_upload($source) {
-  if ($_FILES["edit"]["name"][$source] && is_uploaded_file($_FILES["edit"]["tmp_name"][$source])) {
+  if (is_object($source)) {
+    if (is_file($source->path)) {
+      return $source;
+    }
+  }
+  elseif ($_FILES["edit"]["name"][$source] && is_uploaded_file($_FILES["edit"]["tmp_name"][$source])) {
     $file->name = trim(basename($_FILES["edit"]["name"][$source]), '.');
     $file->type = $_FILES["edit"]["type"][$source];
     $file->path = $_FILES["edit"]["tmp_name"][$source];
     $file->error = $_FILES["edit"]["error"][$source];
     $file->size = $_FILES["edit"]["size"][$source];
+    $file->source = $_FILES["edit"]["size"][$source];
     return $file;
   }
 }
@@ -216,8 +222,10 @@ function file_move(&$source, $dest = 0, $replace = 0) {
   return 0;
 }
 
-function file_delete($source) {
-  unlink(file_create_path($source));
+function file_delete($path) {
+  if (is_file($path)) {
+    unlink($path);
+  }
 }
 
 /**
@@ -236,10 +244,10 @@ function file_delete($source) {
 function file_save_upload($source, $dest = 0, $replace = 0) {
   // Make sure $source exists in $_FILES.
   if ($file = file_check_upload($source)) {
-    if ($dest === 0) {
-      $dest = variable_get('file_directory_temp', ini_get('upload_tmp_dir'));
+    if (!$dest) {
+      $dest = variable_get('file_directory_temp', (PHP_OS == 'WINNT' ? 'c:\\windows\\temp' : '/tmp'));
       $temporary = 1;
-      if (file_exists($_SESSION['file_uploads'][$source]->path)) {
+      if (is_file($_SESSION['file_uploads'][$source]->path)) {
         // If this file was uploaded by this user before replace the temporary copy.
         $replace = 1;
       }
@@ -263,7 +271,7 @@ function file_save_upload($source, $dest = 0, $replace = 0) {
         return 0;
     }
 
-    unset($_SESSION['file_uploads'][$source]);
+    unset($_SESSION['file_uploads'][is_object($source) ? $source->source : $source]);
     if (file_move($file, $dest, $replace)) {
       if ($temporary) {
         $_SESSION['file_uploads'][$source] = $file;
@@ -273,7 +281,7 @@ function file_save_upload($source, $dest = 0, $replace = 0) {
     return 0;
   }
   else {
-    // In case if previews return previous file object.
+    // In case of previews return previous file object.
     if (file_exists($_SESSION['file_uploads'][$source]->path)) {
       return $_SESSION['file_uploads'][$source];
     }
diff --git a/modules/profile.module b/modules/profile.module
index 62e942b89bb9..215a57df9a0b 100644
--- a/modules/profile.module
+++ b/modules/profile.module
@@ -266,7 +266,7 @@ function _profile_validate_avatar(&$edit, $user) {
     $error = t("The uploaded image is too large; the maximum dimensions are %a pixels.", array("%a" => variable_get("profile_avatar_dimensions", "85x85")));
   }
   else if ($file = file_save_upload('profile_avatar', variable_get("profile_avatar_path", "avatars") . FILE_SEPARATOR .'avatar-'. $user->uid . $extension, 1)) {
-    $edit["profile_avatar"] = $file->name;
+    $edit["profile_avatar"] = $file->path;
   }
   else {
     $error = t("Failed to upload the avatar image; the '%directory' directory doesn't exist.", array("%directory" => variable_get("profile_avatar_path", "avatars")));
diff --git a/modules/profile/profile.module b/modules/profile/profile.module
index 62e942b89bb9..215a57df9a0b 100644
--- a/modules/profile/profile.module
+++ b/modules/profile/profile.module
@@ -266,7 +266,7 @@ function _profile_validate_avatar(&$edit, $user) {
     $error = t("The uploaded image is too large; the maximum dimensions are %a pixels.", array("%a" => variable_get("profile_avatar_dimensions", "85x85")));
   }
   else if ($file = file_save_upload('profile_avatar', variable_get("profile_avatar_path", "avatars") . FILE_SEPARATOR .'avatar-'. $user->uid . $extension, 1)) {
-    $edit["profile_avatar"] = $file->name;
+    $edit["profile_avatar"] = $file->path;
   }
   else {
     $error = t("Failed to upload the avatar image; the '%directory' directory doesn't exist.", array("%directory" => variable_get("profile_avatar_path", "avatars")));
-- 
GitLab