From a0d917fe98efbb11905f7ddd12169e3675690555 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <thomas.mueller@tmit.eu>
Date: Sun, 15 Apr 2012 16:59:39 +0200
Subject: [PATCH] fixing oc-375 - a number is appended tp the filename

---
 files/ajax/upload.php |  3 ++-
 lib/helper.php        | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/files/ajax/upload.php b/files/ajax/upload.php
index af7a7acf70..76ea65fe93 100644
--- a/files/ajax/upload.php
+++ b/files/ajax/upload.php
@@ -47,7 +47,8 @@ $result=array();
 if(strpos($dir,'..') === false){
 	$fileCount=count($files['name']);
 	for($i=0;$i<$fileCount;$i++){
-		$target=stripslashes($dir) . $files['name'][$i];
+		// $target=stripslashes($dir) . $files['name'][$i];
+        $target = OC_Helper::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
 		if(is_uploaded_file($files['tmp_name'][$i]) and OC_Filesystem::fromTmpFile($files['tmp_name'][$i],$target)){
 			$meta=OC_FileCache::getCached($target);
 			$result[]=array( "status" => "success", 'mime'=>$meta['mimetype'],'size'=>$meta['size'],'name'=>$files['name'][$i]);
diff --git a/lib/helper.php b/lib/helper.php
index c33db5f10f..412f0e6b76 100755
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -492,4 +492,32 @@ class OC_Helper {
 			}
 		}
 	}
+
+    /**
+     * Adds a suffix to the name in case the file exists
+     *
+     * @param $path
+     * @param $filename
+     * @return string
+     */
+    public static function buildNotExistingFileName($path, $filename)
+    {
+        if ($pos = strrpos($filename, '.')) {
+            $name = substr($filename, 0, $pos);
+            $ext = substr($filename, $pos);
+        } else {
+            $name = $filename;
+        }
+
+        $newpath = $path . '/' . $filename;
+        $newname = $filename;
+        $counter = 2;
+        while (OC_Filesystem::file_exists($newpath)) {
+            $newname = $name . ' (' . $counter . ')' . $ext;
+            $newpath = $path . '/' . $newname;
+            $counter++;
+        }
+
+        return $newname;
+    }
 }
-- 
GitLab