2013-08-17 15:07:18 +04:00
|
|
|
<?php
|
|
|
|
|
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;
|
|
|
|
|
2013-08-17 15:07:18 +04:00
|
|
|
class Helper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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-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
|
|
|
*/
|
2014-04-03 22:57:06 +04:00
|
|
|
public static function getTrashFiles($dir, $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
|
|
|
$user = \OCP\User::getUser();
|
|
|
|
|
2014-02-25 23:46:41 +04:00
|
|
|
$view = new \OC_Filesystemview('/' . $user . '/files_trashbin/files');
|
|
|
|
|
|
|
|
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 23:22:06 +04:00
|
|
|
|
|
|
|
list($storage, $internalPath) = $view->resolvePath($dir);
|
|
|
|
$absoluteDir = $view->getAbsolutePath($dir);
|
|
|
|
|
2014-02-25 23:46:41 +04:00
|
|
|
if (is_resource($dirContent)) {
|
|
|
|
while (($entryName = readdir($dirContent)) !== false) {
|
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
|
|
|
|
$id = $entryName;
|
|
|
|
if ($dir === '' || $dir === '/') {
|
|
|
|
$pathparts = pathinfo($entryName);
|
|
|
|
$timestamp = substr($pathparts['extension'], 1);
|
|
|
|
$id = $pathparts['filename'];
|
|
|
|
} 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);
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
2013-10-28 23:22:06 +04:00
|
|
|
$i = array(
|
|
|
|
'name' => $id,
|
|
|
|
'mtime' => $timestamp,
|
|
|
|
'mimetype' => \OC_Helper::getFileNameMimeType($id),
|
2014-02-25 23:46:41 +04:00
|
|
|
'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file',
|
2013-10-28 23:22:06 +04:00
|
|
|
'directory' => ($dir === '/') ? '' : $dir,
|
2014-02-25 23:46:41 +04:00
|
|
|
);
|
2013-10-28 23:22:06 +04:00
|
|
|
$result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i);
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 23:46:41 +04:00
|
|
|
closedir($dirContent);
|
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
|
|
|
|
* @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
|
|
|
|
$entry['permissions'] = \OCP\PERMISSION_READ;
|
|
|
|
if (\OCP\App::isEnabled('files_encryption')) {
|
|
|
|
$entry['isPreviewAvailable'] = false;
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|