@ -25,6 +25,8 @@ namespace OCA\Documents;
class File {
protected $fileId;
protected $owner;
protected $path;
public function __construct($fileId){
if (!$fileId){
@ -34,22 +36,46 @@ class File {
$this->fileId = $fileId;
}
public static function getByShareToken($token){
$linkItem = \OCP\Share::getShareByToken($token);
if (is_array($linkItem) & & isset($linkItem['uid_owner'])) {
// seems to be a valid share
$rootLinkItem = \OCP\Share::resolveReShare($linkItem);
$fileOwner = $rootLinkItem['uid_owner'];
} else {
throw new \Exception('This file was probably unshared');
}
$file = new File($rootLinkItem['file_source']);
$file->setOwner($rootLinkItem['uid_owner']);
$file->setPath('/files' . $rootLinkItem['file_target']);
return $file;
}
public function getFileId(){
return $this->fileId;
}
public function setOwner($owner){
$this->owner = $owner;
}
public function setPath($path){
$this->path = $path;
}
/**
*
* @return string owner of the current file item
* @throws \Exception
*/
public function getOwnerViewAndPath(){
if (!$this->owner || !$this->path){
$fileInfo = \OC\Files\Cache\Cache::getById($this->fileId);
//is it shared
$sharedInfo = \OCP\Share::getItemSharedWithBySource(
'file',
$this->fileId,
\OCP\Share::FORMAT_NONE,
null,
true
);
$sharedInfo = $this->getSharedBySource();
if (is_array($sharedInfo)){
$owner = $sharedInfo['uid_owner'];
@ -66,6 +92,12 @@ class File {
$view = new View('/' . $owner);
$this->owner = $owner;
} else {
$view = new View('/' . $this->owner);
$path = $this->path;
}
if (!$view->file_exists($path)){
throw new \Exception($path . ' doesn\'t exist');
}
@ -74,25 +106,34 @@ class File {
}
public function getOwner(){
if (!$this->owner){
$fileInfo = \OC\Files\Cache\Cache::getById($this->fileId);
//is it shared
$sharedInfo = \OCP\Share::getItemSharedWithBySource(
'file',
$this->fileId,
\OCP\Share::FORMAT_NONE,
null,
true
);
$sharedInfo = $this->getSharedBySource();
if (!is_array($sharedInfo)){
$sharedInfo = $this->getSharedByLink();
}
if (is_array($sharedInfo)){
$owner = $sharedInfo['uid_owner'];
$this->owner = $sharedInfo['uid_owner'];
} else {
// owner is myself
$owner = \OCP\User::getUser();
$this->owner = \OCP\User::getUser();
}
}
return $this->owner;
}
return $owner;
protected function getSharedBySource(){
return \OCP\Share::getItemSharedWithBySource(
'file',
$this->fileId,
\OCP\Share::FORMAT_NONE,
null,
true
);
}
}