Completed
Push — stable10 ( 0f7090...d7faff )
by Morris
118:06 queued 106:32
created

Helper::formatFileInfos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
namespace OCA\Files_Trashbin;
27
28
use OC\Files\FileInfo;
29
use OCP\Constants;
30
use OCP\Files\Cache\ICacheEntry;
31
32
class Helper {
33
	/**
34
	 * Retrieves the contents of a trash bin directory.
35
	 *
36
	 * @param string $dir path to the directory inside the trashbin
37
	 * or empty to retrieve the root of the trashbin
38
	 * @param string $user
39
	 * @param string $sortAttribute attribute to sort on or empty to disable sorting
40
	 * @param bool $sortDescending true for descending sort, false otherwise
41
	 * @return \OCP\Files\FileInfo[]
42
	 */
43
	public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) {
44
		$result = array();
45
		$timestamp = null;
46
47
		$view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
48
49
		if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
50
			throw new \Exception('Directory does not exists');
51
		}
52
53
		$mount = $view->getMount($dir);
54
		$storage = $mount->getStorage();
55
		$absoluteDir = $view->getAbsolutePath($dir);
56
		$internalPath = $mount->getInternalPath($absoluteDir);
57
58
		$originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
59
		$dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir)));
60
		foreach ($dirContent as $entry) {
61
			$entryName = $entry->getName();
62
			$id = $entry->getId();
63
			$name = $entryName;
64
			if ($dir === '' || $dir === '/') {
65
				$pathparts = pathinfo($entryName);
66
				$timestamp = substr($pathparts['extension'], 1);
67
				$name = $pathparts['filename'];
68
69
			} else if ($timestamp === null) {
70
				// for subfolders we need to calculate the timestamp only once
71
				$parts = explode('/', ltrim($dir, '/'));
72
				$timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
73
			}
74
			$originalPath = '';
75
			if (isset($originalLocations[$id][$timestamp])) {
76
				$originalPath = $originalLocations[$id][$timestamp];
77
				if (substr($originalPath, -1) === '/') {
78
					$originalPath = substr($originalPath, 0, -1);
79
				}
80
			}
81
			$type = $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file';
82
			$i = array(
83
				'name' => $name,
84
				'mtime' => $timestamp,
85
				'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($name),
86
				'type' => $type,
87
				'directory' => ($dir === '/') ? '' : $dir,
88
				'size' => $entry->getSize(),
89
				'etag' => '',
90
				'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE
91
			);
92
			if ($originalPath) {
93
				$i['extraData'] = $originalPath . '/' . $id;
94
			}
95
			$result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
0 ignored issues
show
Bug introduced by
It seems like $mount defined by $view->getMount($dir) on line 53 can be null; however, OC\Files\FileInfo::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
		}
97
98
		if ($sortAttribute !== '') {
99
			return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
100
		}
101
		return $result;
102
	}
103
104
	/**
105
	 * Format file infos for JSON
106
	 *
107
	 * @param \OCP\Files\FileInfo[] $fileInfos file infos
108
	 */
109
	public static function formatFileInfos($fileInfos) {
110
		$files = array();
111
		$id = 0;
112
		foreach ($fileInfos as $i) {
113
			$entry = \OCA\Files\Helper::formatFileInfo($i);
114
			$entry['id'] = $id++;
115
			$entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
116
			$entry['permissions'] = \OCP\Constants::PERMISSION_READ;
117
			$files[] = $entry;
118
		}
119
		return $files;
120
	}
121
}
122