From a6be42cb266d198a5def8b53bff796b93787aff4 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Thu, 17 Sep 2015 21:33:24 +0300 Subject: [PATCH] Cleanup file and storage models --- lib/file.php | 77 ++++++++++++++++++++++++------------------------- lib/storage.php | 2 +- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/lib/file.php b/lib/file.php index ed4acf99..58542673 100644 --- a/lib/file.php +++ b/lib/file.php @@ -27,19 +27,23 @@ use \OC\Files\View; class File { protected $fileId; protected $owner; - protected $path; protected $sharing; protected $token =''; protected $passwordProtected = false; + protected $ownerView; + protected $ownerViewFiles; + protected $path; + protected $pathFiles; - - public function __construct($fileId, $shareOps = null){ + public function __construct($fileId, $shareOps = null, $token = null){ if (!$fileId){ throw new \Exception('No valid file has been passed'); } $this->fileId = $fileId; $this->sharing = $shareOps; + $this->token = $token; + $this->initViews(); } @@ -52,8 +56,7 @@ class File { throw new \Exception('This file was probably unshared'); } - $file = new File($rootLinkItem['file_source'], $rootLinkItem); - $file->setToken($token); + $file = new File($rootLinkItem['file_source'], $rootLinkItem, $token); if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){ $file->setPasswordProtected(true); @@ -70,14 +73,6 @@ class File { return $this->fileId; } - public function setOwner($owner){ - $this->owner = $owner; - } - - public function setPath($path){ - $this->path = $path; - } - public function setToken($token){ $this->token = $token; } @@ -136,7 +131,6 @@ class File { public function setPasswordProtected($value){ $this->passwordProtected = $value; } - /** * @@ -144,9 +138,25 @@ class File { * @throws \Exception */ public function getOwnerViewAndPath($useDefaultRoot = false){ - if ($this->isPublicShare()){ + return $useDefaultRoot ? [$this->ownerViewFiles, $this->pathFiles] : [$this->ownerView, $this->path]; + } + + public function getOwner(){ + return $this->owner; + } + + public function getOwnerView($relativeToFiles = false){ + return $relativeToFiles ? $this->ownerViewFiles : $this->ownerView; + } + + public function getPath($relativeToFiles = false){ + return $relativeToFiles ? $this->pathFiles : $this->path; + } + + protected function initViews(){ + if ($this->isPublicShare()) { if (isset($this->sharing['uid_owner'])){ - $owner = $this->sharing['uid_owner']; + $this->owner = $this->sharing['uid_owner']; if (!\OC::$server->getUserManager()->userExists($this->sharing['uid_owner'])) { throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist '); } @@ -156,39 +166,28 @@ class File { } else { throw new \Exception($this->fileId . ' is a broken share'); } - $view = new View('/' . $owner . '/files'); } else { - $owner = \OC::$server->getUserSession()->getUser()->getUID(); - $root = '/' . $owner; - if ($useDefaultRoot){ - $root .= '/' . 'files'; - } - $view = new View($root); + $this->owner = \OC::$server->getUserSession()->getUser()->getUID(); } - - $path = $view->getPath($this->fileId); - if (!$path){ + + $this->ownerView = new View('/' . $this->owner); + $this->ownerViewFiles = new View('/' . $this->owner . '/files'); + $this->path = $this->ownerView->getPath($this->fileId); + $this->pathFiles = $this->ownerViewFiles->getPath($this->fileId); + + if (!$this->path || !$this->pathFiles) { throw new \Exception($this->fileId . ' can not be resolved'); } - $this->path = $path; - $this->owner = $owner; - if (!$view->file_exists($this->path)){ + if (!$this->ownerView->file_exists($this->path)) { throw new \Exception($this->path . ' doesn\'t exist'); } - - return array($view, $this->path); - } - - - public function getOwner(){ - if (!$this->owner){ - $this->getOwnerViewAndPath(); + + if (!$this->ownerViewFiles->file_exists($this->pathFiles)) { + throw new \Exception($this->pathFiles . ' doesn\'t exist'); } - return $this->owner; } - protected function getPassword(){ return $this->sharing['share_with']; } diff --git a/lib/storage.php b/lib/storage.php index dc39122f..b3c2ce02 100644 --- a/lib/storage.php +++ b/lib/storage.php @@ -100,7 +100,7 @@ class Storage { public static function getSupportedMimetypes(){ return array_merge( array(self::MIMETYPE_LIBREOFFICE_WORDPROCESSOR), - Filter::getAll() + Filter::getAll() ); } }