Cleanup file and storage models

pull/1/head
Victor Dubiniuk 9 years ago
parent 61d2a6cb6f
commit a6be42cb26

@ -27,19 +27,23 @@ use \OC\Files\View;
class File { class File {
protected $fileId; protected $fileId;
protected $owner; protected $owner;
protected $path;
protected $sharing; protected $sharing;
protected $token =''; protected $token ='';
protected $passwordProtected = false; protected $passwordProtected = false;
protected $ownerView;
protected $ownerViewFiles;
protected $path;
protected $pathFiles;
public function __construct($fileId, $shareOps = null, $token = null){
public function __construct($fileId, $shareOps = null){
if (!$fileId){ if (!$fileId){
throw new \Exception('No valid file has been passed'); throw new \Exception('No valid file has been passed');
} }
$this->fileId = $fileId; $this->fileId = $fileId;
$this->sharing = $shareOps; $this->sharing = $shareOps;
$this->token = $token;
$this->initViews();
} }
@ -52,8 +56,7 @@ class File {
throw new \Exception('This file was probably unshared'); throw new \Exception('This file was probably unshared');
} }
$file = new File($rootLinkItem['file_source'], $rootLinkItem); $file = new File($rootLinkItem['file_source'], $rootLinkItem, $token);
$file->setToken($token);
if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){ if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){
$file->setPasswordProtected(true); $file->setPasswordProtected(true);
@ -70,14 +73,6 @@ class File {
return $this->fileId; return $this->fileId;
} }
public function setOwner($owner){
$this->owner = $owner;
}
public function setPath($path){
$this->path = $path;
}
public function setToken($token){ public function setToken($token){
$this->token = $token; $this->token = $token;
} }
@ -136,7 +131,6 @@ class File {
public function setPasswordProtected($value){ public function setPasswordProtected($value){
$this->passwordProtected = $value; $this->passwordProtected = $value;
} }
/** /**
* *
@ -144,9 +138,25 @@ class File {
* @throws \Exception * @throws \Exception
*/ */
public function getOwnerViewAndPath($useDefaultRoot = false){ 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'])){ 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'])) { if (!\OC::$server->getUserManager()->userExists($this->sharing['uid_owner'])) {
throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist '); throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist ');
} }
@ -156,39 +166,28 @@ class File {
} else { } else {
throw new \Exception($this->fileId . ' is a broken share'); throw new \Exception($this->fileId . ' is a broken share');
} }
$view = new View('/' . $owner . '/files');
} else { } else {
$owner = \OC::$server->getUserSession()->getUser()->getUID(); $this->owner = \OC::$server->getUserSession()->getUser()->getUID();
$root = '/' . $owner;
if ($useDefaultRoot){
$root .= '/' . 'files';
}
$view = new View($root);
} }
$path = $view->getPath($this->fileId); $this->ownerView = new View('/' . $this->owner);
if (!$path){ $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'); 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'); throw new \Exception($this->path . ' doesn\'t exist');
} }
return array($view, $this->path); if (!$this->ownerViewFiles->file_exists($this->pathFiles)) {
} throw new \Exception($this->pathFiles . ' doesn\'t exist');
public function getOwner(){
if (!$this->owner){
$this->getOwnerViewAndPath();
} }
return $this->owner;
} }
protected function getPassword(){ protected function getPassword(){
return $this->sharing['share_with']; return $this->sharing['share_with'];
} }

@ -100,7 +100,7 @@ class Storage {
public static function getSupportedMimetypes(){ public static function getSupportedMimetypes(){
return array_merge( return array_merge(
array(self::MIMETYPE_LIBREOFFICE_WORDPROCESSOR), array(self::MIMETYPE_LIBREOFFICE_WORDPROCESSOR),
Filter::getAll() Filter::getAll()
); );
} }
} }

Loading…
Cancel
Save