2015-01-14 23:06:26 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-01-14 23:06:26 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2015-01-14 23:06:26 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2015-02-23 13:28:53 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 23:06:26 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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 Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 23:06:26 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2015-01-14 23:06:26 +03:00
|
|
|
namespace OCA\Files_Trashbin;
|
|
|
|
|
2015-01-23 16:19:36 +03:00
|
|
|
use OC\Files\Filesystem;
|
2015-01-14 23:06:26 +03:00
|
|
|
use OC\Files\Storage\Wrapper\Wrapper;
|
|
|
|
|
|
|
|
class Storage extends Wrapper {
|
|
|
|
|
|
|
|
private $mountPoint;
|
|
|
|
// remember already deleted files to avoid infinite loops if the trash bin
|
|
|
|
// move files across storages
|
|
|
|
private $deletedFiles = array();
|
|
|
|
|
2015-01-26 14:22:22 +03:00
|
|
|
/**
|
|
|
|
* Disable trash logic
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $disableTrash = false;
|
|
|
|
|
2015-01-14 23:06:26 +03:00
|
|
|
function __construct($parameters) {
|
|
|
|
$this->mountPoint = $parameters['mountPoint'];
|
|
|
|
parent::__construct($parameters);
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:22:22 +03:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public static function preRenameHook($params) {
|
|
|
|
// in cross-storage cases, a rename is a copy + unlink,
|
|
|
|
// that last unlink must not go to trash
|
|
|
|
self::$disableTrash = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public static function postRenameHook($params) {
|
|
|
|
self::$disableTrash = false;
|
|
|
|
}
|
|
|
|
|
2015-03-18 21:22:15 +03:00
|
|
|
/**
|
|
|
|
* Rename path1 to path2 by calling the wrapped storage.
|
|
|
|
*
|
|
|
|
* @param string $path1 first path
|
|
|
|
* @param string $path2 second path
|
|
|
|
*/
|
|
|
|
public function rename($path1, $path2) {
|
|
|
|
$result = $this->storage->rename($path1, $path2);
|
|
|
|
if ($result === false) {
|
|
|
|
// when rename failed, the post_rename hook isn't triggered,
|
|
|
|
// but we still want to reenable the trash logic
|
|
|
|
self::$disableTrash = false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:22:22 +03:00
|
|
|
/**
|
|
|
|
* Deletes the given file by moving it into the trashbin.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2015-01-14 23:06:26 +03:00
|
|
|
public function unlink($path) {
|
2015-04-07 13:46:07 +03:00
|
|
|
if (self::$disableTrash || !\OC_App::isEnabled('files_trashbin')) {
|
2015-01-26 14:22:22 +03:00
|
|
|
return $this->storage->unlink($path);
|
|
|
|
}
|
2015-01-23 16:19:36 +03:00
|
|
|
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
|
2015-01-14 23:06:26 +03:00
|
|
|
$result = true;
|
|
|
|
if (!isset($this->deletedFiles[$normalized])) {
|
2015-01-23 16:19:36 +03:00
|
|
|
$view = Filesystem::getView();
|
2015-01-14 23:06:26 +03:00
|
|
|
$this->deletedFiles[$normalized] = $normalized;
|
2015-01-23 16:19:36 +03:00
|
|
|
if ($filesPath = $view->getRelativePath($normalized)) {
|
|
|
|
$filesPath = trim($filesPath, '/');
|
2015-01-14 23:06:26 +03:00
|
|
|
$result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
|
2015-01-21 18:29:52 +03:00
|
|
|
// in cross-storage cases the file will be copied
|
|
|
|
// but not deleted, so we delete it here
|
2015-01-29 17:52:40 +03:00
|
|
|
if ($result) {
|
|
|
|
$this->storage->unlink($path);
|
|
|
|
}
|
2015-01-14 23:06:26 +03:00
|
|
|
} else {
|
|
|
|
$result = $this->storage->unlink($path);
|
|
|
|
}
|
|
|
|
unset($this->deletedFiles[$normalized]);
|
2015-01-28 18:30:42 +03:00
|
|
|
} else if ($this->storage->file_exists($path)) {
|
|
|
|
$result = $this->storage->unlink($path);
|
2015-01-14 23:06:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the storate wrapper callback
|
|
|
|
*/
|
|
|
|
public static function setupStorage() {
|
|
|
|
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
|
|
|
|
return new \OCA\Files_Trashbin\Storage(array('storage' => $storage, 'mountPoint' => $mountPoint));
|
2015-04-01 18:22:04 +03:00
|
|
|
}, 1);
|
2015-01-14 23:06:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|