]> source.dussan.org Git - nextcloud-server.git/commitdiff
fixing oc-375 - a number is appended tp the filename
authorThomas Müller <thomas.mueller@tmit.eu>
Sun, 15 Apr 2012 14:59:39 +0000 (16:59 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Sun, 15 Apr 2012 15:00:49 +0000 (17:00 +0200)
files/ajax/upload.php
lib/helper.php

index af7a7acf70292521ec176e22dd272ba1bbb91055..76ea65fe933a03c8058549c03be4e22cefba5719 100644 (file)
@@ -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]);
index c33db5f10fe3cd8a1f75951c33603b8f93973026..412f0e6b7643377493c8cf5209d9eaba0b4fb9f8 100755 (executable)
@@ -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;
+    }
 }