2015-01-14 23:06:26 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @copyright (C) 2015 ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Bjoern Schiessle <schiessle@owncloud.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 Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-01-26 14:22:22 +03:00
|
|
|
if (self::$disableTrash) {
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|