diff --git a/ajax/documentController.php b/ajax/documentController.php index 733fc2b1..19528333 100644 --- a/ajax/documentController.php +++ b/ajax/documentController.php @@ -36,8 +36,8 @@ class DocumentController extends Controller{ $session = Session::getSession(@$args['es_id']); $filename = isset($session['genesis_url']) ? $session['genesis_url'] : ''; - $documentsView = View::initDocumentsView($session['owner']); - $download = new Download($documentsView, $filename); + $documentsView = new View('/' . $session['owner']); + $download = new Download($documentsView->initDocumentsView(), $filename); $download->sendResponse(); } diff --git a/ajax/sessionController.php b/ajax/sessionController.php index 3aa9f49e..e28e034b 100644 --- a/ajax/sessionController.php +++ b/ajax/sessionController.php @@ -18,24 +18,29 @@ class SessionController extends Controller{ $uid = self::preDispatch(); $fileId = intval(@$args['file_id']); try{ - $path = Storage::getFilePath($fileId); + $file = new File($fileId); + list($ownerView, $path) = $file->getOwnerViewAndPath(); $session = Session::getSessionByFileId($fileId); //If there is no existing session we need to start a new one if (!$session || empty($session)){ - $documentsView = View::initDocumentsView($uid); - $genesisPath = View::storeDocument($uid, $path); + $genesisPath = $ownerView->storeDocument($ownerView, $path); if (!$genesisPath){ throw new \Exception('Unable to copy document. Check permissions and make sure you have enought free space.'); } - $hash = View::getHashByGenesis($uid, $genesisPath); - $session = Session::add($genesisPath, $hash, $fileId); + $hash = $ownerView->getHashByGenesis($genesisPath); + $session = Session::add( + $genesisPath, + $hash, + $file->getOwner(), + $fileId + ); } - $session['permissions'] = View::getFilePermissions($path); + $session['permissions'] = $ownerView->getFilePermissions($path); $session['member_id'] = (string) Member::add($session['es_id'], $uid, Helper::getRandomColor()); \OCP\JSON::success($session); exit(); @@ -70,8 +75,8 @@ class SessionController extends Controller{ throw new \Exception('Session does not exist'); } - $path = Storage::getFilePath($session['file_id']); - $view = new \OC\Files\View('/' . $uid); + $file = new File($session['file_id']); + list($view, $path) = $file->getOwnerViewAndPath(); $isWritable = ($view->file_exists($path) && $view->isUpdatable($path)) || $view->isCreatable($path); if (!$isWritable){ @@ -153,5 +158,4 @@ class SessionController extends Controller{ $tmpl->assign('sessions', $sessions); echo $tmpl->fetchPage(); } - } diff --git a/lib/file.php b/lib/file.php new file mode 100644 index 00000000..ff12796b --- /dev/null +++ b/lib/file.php @@ -0,0 +1,98 @@ +. + * + */ + + +namespace OCA\Documents; + +class File { + protected $fileId; + + public function __construct($fileId){ + if (!$fileId){ + throw new \Exception('No valid file has been passed'); + } + + $this->fileId = $fileId; + } + + /** + * + * @return string owner of the current file item + * @throws \Exception + */ + public function getOwnerViewAndPath(){ + $fileInfo = \OC\Files\Cache\Cache::getById($this->fileId); + + //is it shared + $sharedInfo = \OCP\Share::getItemSharedWithBySource( + 'file', + $this->fileId, + \OCP\Share::FORMAT_NONE, + null, + true + ); + + if (is_array($sharedInfo)){ + $owner = $sharedInfo['uid_owner']; + $path = $sharedInfo['path']; + } else { + // owner is myself + $owner = \OCP\User::getUser(); + $path = @$fileInfo[1]; + } + + if (!$path){ + throw new \Exception($this->fileId . ' can not be resolved'); + } + + $view = new View('/' . $owner); + + if (!$view->file_exists($path)){ + throw new \Exception($path . ' doesn\'t exist'); + } + + return array($view, $path); + } + + public function getOwner(){ + $fileInfo = \OC\Files\Cache\Cache::getById($this->fileId); + + //is it shared + $sharedInfo = \OCP\Share::getItemSharedWithBySource( + 'file', + $this->fileId, + \OCP\Share::FORMAT_NONE, + null, + true + ); + + if (is_array($sharedInfo)){ + $owner = $sharedInfo['uid_owner']; + } else { + // owner is myself + $owner = \OCP\User::getUser(); + } + + return $owner; + } + +} \ No newline at end of file diff --git a/lib/session.php b/lib/session.php index 1d1d16ec..bbd95a72 100644 --- a/lib/session.php +++ b/lib/session.php @@ -14,7 +14,7 @@ namespace OCA\Documents; class Session extends Db{ const DB_TABLE = '`*PREFIX*documents_session`'; - public static function add($genesis, $hash, $fileId){ + public static function add($genesis, $hash, $owner, $fileId){ $query = \OCP\DB::prepare(' INSERT INTO ' . self::DB_TABLE . ' (`es_id`, `genesis_url`, `genesis_hash`, `owner`, `file_id`) VALUES (?, ?, ?, ?, ?) @@ -24,7 +24,7 @@ class Session extends Db{ 'es_id' => self::getUniqueSessionId(), 'genesis_url' => $genesis, 'genesis_hash' => $hash, - 'owner' => \OCP\User::getUser(), + 'owner' => $owner, 'file_id' => $fileId ); $result = $query->execute(array_values($data)); diff --git a/lib/storage.php b/lib/storage.php index bc3decc0..ec1c6cab 100755 --- a/lib/storage.php +++ b/lib/storage.php @@ -41,46 +41,6 @@ class Storage { return $list; } - /** - * @brief Retrieve path by fileId - * @param int $fileId - * @throws \Exception - */ - public static function getFilePath($fileId){ - if (!$fileId){ - throw new \Exception('No valid file has been passed'); - } - - $fileInfo = \OC\Files\Cache\Cache::getById($fileId); - $path = @$fileInfo[1]; - - if (!$path){ - throw new \Exception($fileId . ' can not be resolved'); - } - - $internalPath = preg_replace('/^\/?files\//', '', $path); - if (!\OC\Files\Filesystem::file_exists($internalPath)){ - $sharedInfo = \OCP\Share::getItemSharedWithBySource( - 'file', - $fileId, - \OCP\Share::FORMAT_NONE, - null, - true - ); - if (!$sharedInfo){ - throw new \Exception($path . ' can not be resolved in shared cache'); - } - // this file is shared - $internalPath = 'Shared' . $sharedInfo['file_target']; - } - - if (!\OC\Files\Filesystem::file_exists($internalPath)){ - throw new \Exception($path . ' doesn\'t exist'); - } - - return 'files/' . $internalPath; - } - /** * @brief Cleanup session data on removing the document * @param array diff --git a/lib/view.php b/lib/view.php index 575eebcf..e56af7f0 100644 --- a/lib/view.php +++ b/lib/view.php @@ -15,56 +15,45 @@ class View extends \OC\Files\View{ const DOCUMENTS_DIRNAME='/documents'; protected static $documentsView; - public static function initDocumentsView($uid){ - $view = new \OC\Files\View('/' . $uid); - if (!$view->is_dir(self::DOCUMENTS_DIRNAME)) { - $view->mkdir(self::DOCUMENTS_DIRNAME); + public function initDocumentsView(){ + if (!$this->is_dir(self::DOCUMENTS_DIRNAME)) { + $this->mkdir(self::DOCUMENTS_DIRNAME); } - //if (!self::$documentsView){ - // self::$documentsView = new \OC\Files\View('/' . $uid . self::DOCUMENTS_DIRNAME); - //} - - // it was a bad idea to use a static method. - // to be changed later - return new \OC\Files\View('/' . $uid . self::DOCUMENTS_DIRNAME); + return new \OC\Files\View( $this->getRoot() . self::DOCUMENTS_DIRNAME); } - public static function getFilePermissions($path){ - $view = new \OC\Files\View('/' . \OCP\User::getUser()); + public function getFilePermissions($path){ $permissions = 0; - if ($view->isReadable($path)) { + if ($this->isReadable($path)) { $permissions |= \OCP\PERMISSION_READ; } - if ($view->isSharable($path)) { + if ($this->isSharable($path)) { $permissions |= \OCP\PERMISSION_SHARE; } return $permissions; } - public static function storeDocument($uid, $filePath){ + public function storeDocument($ownerView, $filePath){ $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; - $view = new \OC\Files\View('/' . $uid); - - if (!$view->file_exists($filePath)){ + if (!$ownerView->file_exists($filePath)){ throw new \Exception($filePath . ' doesn\'t exist'); } - if (!$view->is_file($filePath)){ + if (!$ownerView->is_file($filePath)){ throw new \Exception('Object ' . $filePath . ' is not a file.'); } - $newName = '/' . sha1($view->file_get_contents($filePath)) . '.odt'; + $newName = '/' . sha1($ownerView->file_get_contents($filePath)) . '.odt'; - $view->copy($filePath, self::DOCUMENTS_DIRNAME . $newName); + $ownerView->copy($filePath, self::DOCUMENTS_DIRNAME . $newName); \OC_FileProxy::$enabled = $proxyStatus; return $newName; } - public static function getHashByGenesis($uid, $genesisPath){ - $documentsView = self::initDocumentsView($uid); - return sha1($documentsView->file_get_contents($genesisPath)); + public function getHashByGenesis($genesisPath){ + return sha1($this->file_get_contents(self::DOCUMENTS_DIRNAME . $genesisPath)); } }