2013-08-17 15:07:18 +04:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2013-09-20 18:37:07 +04:00
|
|
|
namespace OCA\Files_Trashbin;
|
2013-08-17 15:07:18 +04:00
|
|
|
|
2014-03-19 16:53:59 +04:00
|
|
|
use OC\Files\FileInfo;
|
2016-01-08 15:07:23 +03:00
|
|
|
use OCP\Constants;
|
2016-05-03 00:13:38 +03:00
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
2014-03-19 16:53:59 +04:00
|
|
|
|
2016-05-03 00:13:38 +03:00
|
|
|
class Helper {
|
2013-08-17 15:07:18 +04:00
|
|
|
/**
|
|
|
|
* Retrieves the contents of a trash bin directory.
|
2014-04-03 22:57:06 +04:00
|
|
|
*
|
2013-08-17 15:07:18 +04:00
|
|
|
* @param string $dir path to the directory inside the trashbin
|
|
|
|
* or empty to retrieve the root of the trashbin
|
2014-06-17 22:08:40 +04:00
|
|
|
* @param string $user
|
2014-04-03 22:57:06 +04:00
|
|
|
* @param string $sortAttribute attribute to sort on or empty to disable sorting
|
|
|
|
* @param bool $sortDescending true for descending sort, false otherwise
|
2014-03-19 16:53:59 +04:00
|
|
|
* @return \OCP\Files\FileInfo[]
|
2013-08-17 15:07:18 +04:00
|
|
|
*/
|
2016-05-03 00:13:38 +03:00
|
|
|
public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) {
|
2013-08-17 15:07:18 +04:00
|
|
|
$result = array();
|
2014-02-25 23:46:41 +04:00
|
|
|
$timestamp = null;
|
2013-08-17 15:07:18 +04:00
|
|
|
|
2014-05-15 17:21:27 +04:00
|
|
|
$view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
|
2014-02-25 23:46:41 +04:00
|
|
|
|
|
|
|
if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
|
|
|
|
throw new \Exception('Directory does not exists');
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:24:48 +03:00
|
|
|
$mount = $view->getMount($dir);
|
|
|
|
$storage = $mount->getStorage();
|
2013-10-28 23:22:06 +04:00
|
|
|
$absoluteDir = $view->getAbsolutePath($dir);
|
2014-12-16 16:24:48 +03:00
|
|
|
$internalPath = $mount->getInternalPath($absoluteDir);
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2016-05-03 00:13:38 +03:00
|
|
|
$originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
|
|
|
|
$dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir)));
|
|
|
|
foreach ($dirContent as $entry) {
|
|
|
|
$entryName = $entry->getName();
|
|
|
|
$name = $entryName;
|
|
|
|
if ($dir === '' || $dir === '/') {
|
|
|
|
$pathparts = pathinfo($entryName);
|
|
|
|
$timestamp = substr($pathparts['extension'], 1);
|
|
|
|
$name = $pathparts['filename'];
|
2015-09-21 12:41:26 +03:00
|
|
|
|
2016-05-03 00:13:38 +03:00
|
|
|
} else if ($timestamp === null) {
|
|
|
|
// for subfolders we need to calculate the timestamp only once
|
|
|
|
$parts = explode('/', ltrim($dir, '/'));
|
|
|
|
$timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
|
|
|
|
}
|
|
|
|
$originalPath = '';
|
2017-02-27 22:00:47 +03:00
|
|
|
$originalName = substr($entryName, 0, -strlen($timestamp)-2);
|
|
|
|
if (isset($originalLocations[$originalName][$timestamp])) {
|
|
|
|
$originalPath = $originalLocations[$originalName][$timestamp];
|
2016-05-03 00:13:38 +03:00
|
|
|
if (substr($originalPath, -1) === '/') {
|
|
|
|
$originalPath = substr($originalPath, 0, -1);
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 16:22:19 +03:00
|
|
|
$type = $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file';
|
2016-05-03 00:13:38 +03:00
|
|
|
$i = array(
|
|
|
|
'name' => $name,
|
|
|
|
'mtime' => $timestamp,
|
2016-09-09 16:22:19 +03:00
|
|
|
'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($name),
|
|
|
|
'type' => $type,
|
2016-05-03 00:13:38 +03:00
|
|
|
'directory' => ($dir === '/') ? '' : $dir,
|
|
|
|
'size' => $entry->getSize(),
|
|
|
|
'etag' => '',
|
2018-05-10 17:03:28 +03:00
|
|
|
'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
|
|
|
|
'fileid' => $entry->getId(),
|
2016-05-03 00:13:38 +03:00
|
|
|
);
|
|
|
|
if ($originalPath) {
|
2017-03-23 02:17:37 +03:00
|
|
|
if ($originalPath !== '.') {
|
|
|
|
$i['extraData'] = $originalPath . '/' . $originalName;
|
|
|
|
} else {
|
|
|
|
$i['extraData'] = $originalName;
|
|
|
|
}
|
2016-05-03 00:13:38 +03:00
|
|
|
}
|
|
|
|
$result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
|
|
|
|
2014-04-03 22:57:06 +04:00
|
|
|
if ($sortAttribute !== '') {
|
|
|
|
return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
|
|
|
|
}
|
2013-10-28 23:22:06 +04:00
|
|
|
return $result;
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-28 23:22:06 +04:00
|
|
|
* Format file infos for JSON
|
2016-05-03 00:13:38 +03:00
|
|
|
*
|
2013-10-28 23:22:06 +04:00
|
|
|
* @param \OCP\Files\FileInfo[] $fileInfos file infos
|
2013-08-17 15:07:18 +04:00
|
|
|
*/
|
2013-10-28 23:22:06 +04:00
|
|
|
public static function formatFileInfos($fileInfos) {
|
|
|
|
$files = array();
|
|
|
|
$id = 0;
|
|
|
|
foreach ($fileInfos as $i) {
|
|
|
|
$entry = \OCA\Files\Helper::formatFileInfo($i);
|
|
|
|
$entry['id'] = $id++;
|
|
|
|
$entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
|
2014-11-25 18:28:41 +03:00
|
|
|
$entry['permissions'] = \OCP\Constants::PERMISSION_READ;
|
2013-10-28 23:22:06 +04:00
|
|
|
$files[] = $entry;
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
2013-10-28 23:22:06 +04:00
|
|
|
return $files;
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
|
|
|
}
|