From 7f02dac3183549ed42950335e8ffa1c8e2175f03 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 20 Sep 2013 09:57:10 +0300 Subject: [PATCH] Save to new file if hash is changed. Ref #16 --- ajax/documentController.php | 10 ++-------- ajax/sessionController.php | 8 ++++++++ lib/helper.php | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ajax/documentController.php b/ajax/documentController.php index 810cfdb9..733fc2b1 100644 --- a/ajax/documentController.php +++ b/ajax/documentController.php @@ -18,16 +18,10 @@ class DocumentController extends Controller{ public static function create($args){ $uid = self::preDispatch(); $view = new \OC\Files\View('/' . $uid . '/files'); - $name = '/New Document.odt'; - $fileNum = 0; + $path = Helper::getNewFileName($view, '/New Document.odt'); - while ($view->file_exists($name)){ - $fileNum += 1; - $name = preg_replace('/(\.odt|\(\d+\)\.odt)$/', ' (' .$fileNum . ').odt', $name); - }; - $view->file_put_contents( - $name, + $path, base64_decode(self::ODT_TEMPLATE) ); } diff --git a/ajax/sessionController.php b/ajax/sessionController.php index 2b73c14f..e20ce937 100644 --- a/ajax/sessionController.php +++ b/ajax/sessionController.php @@ -82,6 +82,14 @@ class SessionController extends Controller{ throw new \Exception('Document does not exist or is not writable for this user'); } + if ($view->file_exists($path)){ + $currentHash = sha1($view->file_get_contents($path)); + if ($currentHash !== $session['genesis_hash']){ + // Original file was modified externally. Save to a new one + $path = Helper::getNewFileName($view, $path); + } + } + if ($view->file_put_contents($path, $content)){ //Document saved successfully. Cleaning session data Session::cleanUp($sessionID); diff --git a/lib/helper.php b/lib/helper.php index 019a6917..8b4743a4 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -15,10 +15,20 @@ class Helper{ const APP_ID = 'documents'; + public static function getNewFileName($view, $path){ + $fileNum = 0; + + while ($view->file_exists($path)){ + $fileNum += 1; + $path = preg_replace('/(\.odt|\(\d+\)\.odt)$/', ' (' .$fileNum . ').odt', $path); + }; + + return $path; + } + public static function getRandomColor(){ $str = dechex(floor(rand(0, 16777215))); - $str = str_pad($str, 6, "0", STR_PAD_LEFT); - return '#' . $str; + return '#' . str_pad($str, 6, "0", STR_PAD_LEFT); } public static function debugLog($message){