1<?php 2/** 3 * @copyright Copyright (c) 2016, ownCloud, Inc. 4 * 5 * @author Bjoern Schiessle <bjoern@schiessle.org> 6 * @author Björn Schießle <bjoern@schiessle.org> 7 * @author Christoph Wurst <christoph@winzerhof-wurst.at> 8 * @author Joas Schilling <coding@schilljs.com> 9 * @author Jörn Friedrich Dreyer <jfd@butonic.de> 10 * @author Morris Jobke <hey@morrisjobke.de> 11 * @author Robin Appelman <robin@icewind.nl> 12 * @author Roeland Jago Douma <roeland@famdouma.nl> 13 * @author Victor Dubiniuk <dubiniuk@owncloud.com> 14 * @author Vincent Petry <vincent@nextcloud.com> 15 * 16 * @license AGPL-3.0 17 * 18 * This code is free software: you can redistribute it and/or modify 19 * it under the terms of the GNU Affero General Public License, version 3, 20 * as published by the Free Software Foundation. 21 * 22 * This program is distributed in the hope that it will be useful, 23 * but WITHOUT ANY WARRANTY; without even the implied warranty of 24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 * GNU Affero General Public License for more details. 26 * 27 * You should have received a copy of the GNU Affero General Public License, version 3, 28 * along with this program. If not, see <http://www.gnu.org/licenses/> 29 * 30 */ 31namespace OCA\Files_Trashbin; 32 33use OC\Files\FileInfo; 34use OCP\Constants; 35use OCP\Files\Cache\ICacheEntry; 36 37class Helper { 38 /** 39 * Retrieves the contents of a trash bin directory. 40 * 41 * @param string $dir path to the directory inside the trashbin 42 * or empty to retrieve the root of the trashbin 43 * @param string $user 44 * @param string $sortAttribute attribute to sort on or empty to disable sorting 45 * @param bool $sortDescending true for descending sort, false otherwise 46 * @return \OCP\Files\FileInfo[] 47 */ 48 public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) { 49 $result = []; 50 $timestamp = null; 51 52 $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); 53 54 if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { 55 throw new \Exception('Directory does not exists'); 56 } 57 58 $mount = $view->getMount($dir); 59 $storage = $mount->getStorage(); 60 $absoluteDir = $view->getAbsolutePath($dir); 61 $internalPath = $mount->getInternalPath($absoluteDir); 62 63 $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); 64 $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir))); 65 foreach ($dirContent as $entry) { 66 $entryName = $entry->getName(); 67 $name = $entryName; 68 if ($dir === '' || $dir === '/') { 69 $pathparts = pathinfo($entryName); 70 $timestamp = substr($pathparts['extension'], 1); 71 $name = $pathparts['filename']; 72 } elseif ($timestamp === null) { 73 // for subfolders we need to calculate the timestamp only once 74 $parts = explode('/', ltrim($dir, '/')); 75 $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); 76 } 77 $originalPath = ''; 78 $originalName = substr($entryName, 0, -strlen($timestamp) - 2); 79 if (isset($originalLocations[$originalName][$timestamp])) { 80 $originalPath = $originalLocations[$originalName][$timestamp]; 81 if (substr($originalPath, -1) === '/') { 82 $originalPath = substr($originalPath, 0, -1); 83 } 84 } 85 $type = $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file'; 86 $i = [ 87 'name' => $name, 88 'mtime' => $timestamp, 89 'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($name), 90 'type' => $type, 91 'directory' => ($dir === '/') ? '' : $dir, 92 'size' => $entry->getSize(), 93 'etag' => '', 94 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, 95 'fileid' => $entry->getId(), 96 ]; 97 if ($originalPath) { 98 if ($originalPath !== '.') { 99 $i['extraData'] = $originalPath . '/' . $originalName; 100 } else { 101 $i['extraData'] = $originalName; 102 } 103 } 104 $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); 105 } 106 107 if ($sortAttribute !== '') { 108 return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); 109 } 110 return $result; 111 } 112 113 /** 114 * Format file infos for JSON 115 * 116 * @param \OCP\Files\FileInfo[] $fileInfos file infos 117 */ 118 public static function formatFileInfos($fileInfos) { 119 $files = []; 120 foreach ($fileInfos as $i) { 121 $entry = \OCA\Files\Helper::formatFileInfo($i); 122 $entry['id'] = $i->getId(); 123 $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image 124 $entry['permissions'] = \OCP\Constants::PERMISSION_READ; 125 $files[] = $entry; 126 } 127 return $files; 128 } 129} 130