Completed
Push — stable8.1 ( 524063...e0a38c )
by Morris
55:51
created

Helper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 94
ccs 45
cts 63
cp 0.7143
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getTrashFiles() 0 64 16
A formatFileInfos() 0 12 2
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Robin McCorkell <[email protected]>
8
 * @author Vincent Petry <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2015, ownCloud, Inc.
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
31
class Helper
32
{
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 12
	public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false){
44 12
		$result = array();
45 12
		$timestamp = null;
46
47 12
		$view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
48
49 12
		if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
50
			throw new \Exception('Directory does not exists');
51
		}
52
53 12
		$dirContent = $view->opendir($dir);
54 12
		if ($dirContent === false) {
55
			return $result;
56
		}
57
58 12
		$mount = $view->getMount($dir);
59 12
		$storage = $mount->getStorage();
60 12
		$absoluteDir = $view->getAbsolutePath($dir);
61 12
		$internalPath = $mount->getInternalPath($absoluteDir);
62
63 12
		if (is_resource($dirContent)) {
64 12
			$originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
65 12
			while (($entryName = readdir($dirContent)) !== false) {
66 12
				if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
67 12
					$id = $entryName;
68 12
					if ($dir === '' || $dir === '/') {
69 12
						$pathparts = pathinfo($entryName);
70 12
						$timestamp = substr($pathparts['extension'], 1);
71 12
						$id = $pathparts['filename'];
72 12
					} else if ($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 12
					$originalPath = '';
78 12
					if (isset($originalLocations[$id][$timestamp])) {
79 12
						$originalPath = $originalLocations[$id][$timestamp];
80 12
						if (substr($originalPath, -1) === '/') {
81
							$originalPath = substr($originalPath, 0, -1);
82
						}
83 12
					}
84
					$i = array(
85 12
						'name' => $id,
86 12
						'mtime' => $timestamp,
87 12
						'mimetype' => \OC_Helper::getFileNameMimeType($id),
88 12
						'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file',
89 12
						'directory' => ($dir === '/') ? '' : $dir,
90 12
						'etag' => '',
91 12
						'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE
92 12
					);
93 12
					if ($originalPath) {
94 12
						$i['extraData'] = $originalPath.'/'.$id;
95 12
					}
96 12
					$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 58 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...
97 12
				}
98 12
			}
99 12
			closedir($dirContent);
100 12
		}
101
102 12
		if ($sortAttribute !== '') {
103 11
			return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
104
		}
105 4
		return $result;
106
	}
107
108
	/**
109
	 * Format file infos for JSON
110
	 * @param \OCP\Files\FileInfo[] $fileInfos file infos
111
	 */
112
	public static function formatFileInfos($fileInfos) {
113
		$files = array();
114
		$id = 0;
115
		foreach ($fileInfos as $i) {
116
			$entry = \OCA\Files\Helper::formatFileInfo($i);
117
			$entry['id'] = $id++;
118
			$entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
119
			$entry['permissions'] = \OCP\Constants::PERMISSION_READ;
120
			$files[] = $entry;
121
		}
122
		return $files;
123
	}
124
}
125