Store genesis to the real file owner instead of session starter
This commit is contained in:
parent
20da92fc4e
commit
a7195d6be6
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
98
lib/file.php
Normal file
98
lib/file.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* ownCloud - Documents App
|
||||
*
|
||||
* @author Victor Dubiniuk
|
||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
|
@ -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
|
||||
|
39
lib/view.php
39
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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user