2013-08-17 11:07:18 +00:00
|
|
|
<?php
|
2015-03-26 10:44:34 +00:00
|
|
|
/**
|
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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 14:37:07 +00:00
|
|
|
namespace OCA\Files_Trashbin;
|
2013-08-17 11:07:18 +00:00
|
|
|
|
2014-03-19 12:53:59 +00:00
|
|
|
use OC\Files\FileInfo;
|
|
|
|
|
2013-08-17 11:07:18 +00:00
|
|
|
class Helper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Retrieves the contents of a trash bin directory.
|
2014-04-03 18:57:06 +00:00
|
|
|
*
|
2013-08-17 11:07:18 +00:00
|
|
|
* @param string $dir path to the directory inside the trashbin
|
|
|
|
* or empty to retrieve the root of the trashbin
|
2014-06-17 18:08:40 +00:00
|
|
|
* @param string $user
|
2014-04-03 18:57:06 +00: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 12:53:59 +00:00
|
|
|
* @return \OCP\Files\FileInfo[]
|
2013-08-17 11:07:18 +00:00
|
|
|
*/
|
2014-06-17 18:08:40 +00:00
|
|
|
public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false){
|
2013-08-17 11:07:18 +00:00
|
|
|
$result = array();
|
2014-02-25 19:46:41 +00:00
|
|
|
$timestamp = null;
|
2013-08-17 11:07:18 +00:00
|
|
|
|
2014-05-15 13:21:27 +00:00
|
|
|
$view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
|
2014-02-25 19:46:41 +00:00
|
|
|
|
|
|
|
if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
|
|
|
|
throw new \Exception('Directory does not exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
$dirContent = $view->opendir($dir);
|
|
|
|
if ($dirContent === false) {
|
|
|
|
return $result;
|
|
|
|
}
|
2013-10-28 19:22:06 +00:00
|
|
|
|
2014-12-16 13:24:48 +00:00
|
|
|
$mount = $view->getMount($dir);
|
|
|
|
$storage = $mount->getStorage();
|
2013-10-28 19:22:06 +00:00
|
|
|
$absoluteDir = $view->getAbsolutePath($dir);
|
2014-12-16 13:24:48 +00:00
|
|
|
$internalPath = $mount->getInternalPath($absoluteDir);
|
2013-10-28 19:22:06 +00:00
|
|
|
|
2014-02-25 19:46:41 +00:00
|
|
|
if (is_resource($dirContent)) {
|
2014-08-27 09:28:31 +00:00
|
|
|
$originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
|
2014-02-25 19:46:41 +00:00
|
|
|
while (($entryName = readdir($dirContent)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
|
|
|
|
$id = $entryName;
|
|
|
|
if ($dir === '' || $dir === '/') {
|
2015-09-21 09:41:26 +00:00
|
|
|
$size = $view->filesize($id);
|
2014-02-25 19:46:41 +00:00
|
|
|
$pathparts = pathinfo($entryName);
|
|
|
|
$timestamp = substr($pathparts['extension'], 1);
|
|
|
|
$id = $pathparts['filename'];
|
2015-09-21 09:41:26 +00:00
|
|
|
|
2014-02-25 19:46:41 +00:00
|
|
|
} else if ($timestamp === null) {
|
|
|
|
// for subfolders we need to calculate the timestamp only once
|
2015-09-21 09:41:26 +00:00
|
|
|
$size = $view->filesize($dir . '/' . $id);
|
2014-02-25 19:46:41 +00:00
|
|
|
$parts = explode('/', ltrim($dir, '/'));
|
|
|
|
$timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
|
2013-08-17 11:07:18 +00:00
|
|
|
}
|
2014-08-27 09:28:31 +00:00
|
|
|
$originalPath = '';
|
|
|
|
if (isset($originalLocations[$id][$timestamp])) {
|
|
|
|
$originalPath = $originalLocations[$id][$timestamp];
|
|
|
|
if (substr($originalPath, -1) === '/') {
|
|
|
|
$originalPath = substr($originalPath, 0, -1);
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 19:22:06 +00:00
|
|
|
$i = array(
|
|
|
|
'name' => $id,
|
|
|
|
'mtime' => $timestamp,
|
2015-08-05 11:52:46 +00:00
|
|
|
'mimetype' => $view->is_dir($dir . '/' . $entryName) ? 'httpd/unix-directory' : \OC_Helper::getFileNameMimeType($id),
|
2014-02-25 19:46:41 +00:00
|
|
|
'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file',
|
2013-10-28 19:22:06 +00:00
|
|
|
'directory' => ($dir === '/') ? '' : $dir,
|
2015-09-21 09:41:26 +00:00
|
|
|
'size' => $size,
|
2014-02-25 19:46:41 +00:00
|
|
|
);
|
2014-08-27 09:28:31 +00:00
|
|
|
if ($originalPath) {
|
|
|
|
$i['extraData'] = $originalPath.'/'.$id;
|
|
|
|
}
|
2014-12-16 13:24:48 +00:00
|
|
|
$result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
|
2013-08-17 11:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 19:46:41 +00:00
|
|
|
closedir($dirContent);
|
2013-08-17 11:07:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 18:57:06 +00:00
|
|
|
if ($sortAttribute !== '') {
|
|
|
|
return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
|
|
|
|
}
|
2013-10-28 19:22:06 +00:00
|
|
|
return $result;
|
2013-08-17 11:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-28 19:22:06 +00:00
|
|
|
* Format file infos for JSON
|
|
|
|
* @param \OCP\Files\FileInfo[] $fileInfos file infos
|
2013-08-17 11:07:18 +00:00
|
|
|
*/
|
2013-10-28 19:22:06 +00: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 15:28:41 +00:00
|
|
|
$entry['permissions'] = \OCP\Constants::PERMISSION_READ;
|
2013-10-28 19:22:06 +00:00
|
|
|
$files[] = $entry;
|
2013-08-17 11:07:18 +00:00
|
|
|
}
|
2013-10-28 19:22:06 +00:00
|
|
|
return $files;
|
2013-08-17 11:07:18 +00:00
|
|
|
}
|
|
|
|
}
|