Blame view
sources/apps/files/lib/helper.php
5.41 KB
|
03e52840d
|
1 |
<?php |
|
6d9380f96
|
2 3 4 5 6 7 |
/** * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ |
|
03e52840d
|
8 |
|
|
31b7f2792
|
9 |
namespace OCA\Files; |
|
03e52840d
|
10 |
|
|
6d9380f96
|
11 12 13 |
/** * Helper class for manipulating file information */ |
|
03e52840d
|
14 15 16 |
class Helper
{
public static function buildFileStorageStatistics($dir) {
|
|
03e52840d
|
17 18 |
// information about storage capacities $storageInfo = \OC_Helper::getStorageInfo($dir); |
|
6d9380f96
|
19 20 21 22 23 24 25 26 |
$l = new \OC_L10N('files');
$maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
$maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
$maxHumanFileSize = $l->t('Upload (max. %s)', array($maxHumanFileSize));
return array('uploadMaxFilesize' => $maxUploadFileSize,
'maxHumanFilesize' => $maxHumanFileSize,
'freeSpace' => $storageInfo['free'],
|
|
03e52840d
|
27 28 |
'usedSpacePercent' => (int)$storageInfo['relative']); } |
|
31b7f2792
|
29 |
|
|
6d9380f96
|
30 31 32 33 34 35 |
/** * Determine icon for a given file * * @param \OC\Files\FileInfo $file file info * @return string icon URL */ |
|
31b7f2792
|
36 37 |
public static function determineIcon($file) {
if($file['type'] === 'dir') {
|
|
6d9380f96
|
38 39 40 41 42 43 |
$icon = \OC_Helper::mimetypeIcon('dir');
// TODO: move this part to the client side, using mountType
if ($file->isShared()) {
$icon = \OC_Helper::mimetypeIcon('dir-shared');
} elseif ($file->isMounted()) {
$icon = \OC_Helper::mimetypeIcon('dir-external');
|
|
31b7f2792
|
44 |
} |
|
6d9380f96
|
45 46 |
}else{
$icon = \OC_Helper::mimetypeIcon($file->getMimetype());
|
|
31b7f2792
|
47 |
} |
|
6d9380f96
|
48 |
return substr($icon, 0, -3) . 'svg'; |
|
31b7f2792
|
49 50 51 52 53 |
} /** * Comparator function to sort files alphabetically and have * the directories appear first |
|
6d9380f96
|
54 55 56 57 |
* * @param \OCP\Files\FileInfo $a file * @param \OCP\Files\FileInfo $b file * @return int -1 if $a must come before $b, 1 otherwise |
|
31b7f2792
|
58 |
*/ |
|
6d9380f96
|
59 60 61 62 |
public static function compareFileNames($a, $b) {
$aType = $a->getType();
$bType = $b->getType();
if ($aType === 'dir' and $bType !== 'dir') {
|
|
31b7f2792
|
63 |
return -1; |
|
6d9380f96
|
64 |
} elseif ($aType !== 'dir' and $bType === 'dir') {
|
|
31b7f2792
|
65 66 |
return 1;
} else {
|
|
6d9380f96
|
67 |
return strnatcasecmp($a->getName(), $b->getName()); |
|
31b7f2792
|
68 69 70 71 |
} } /** |
|
6d9380f96
|
72 73 74 75 76 |
* Comparator function to sort files by date * * @param \OCP\Files\FileInfo $a file * @param \OCP\Files\FileInfo $b file * @return int -1 if $a must come before $b, 1 otherwise |
|
31b7f2792
|
77 |
*/ |
|
6d9380f96
|
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
public static function compareTimestamp($a, $b) {
$aTime = $a->getMTime();
$bTime = $b->getMTime();
return $aTime - $bTime;
}
/**
* Comparator function to sort files by size
*
* @param \OCP\Files\FileInfo $a file
* @param \OCP\Files\FileInfo $b file
* @return int -1 if $a must come before $b, 1 otherwise
*/
public static function compareSize($a, $b) {
$aSize = $a->getSize();
$bSize = $b->getSize();
return $aSize - $bSize;
}
/**
* Formats the file info to be returned as JSON to the client.
*
* @param \OCP\Files\FileInfo $i
* @return array formatted file info
*/
public static function formatFileInfo($i) {
$entry = array();
|
|
31b7f2792
|
105 |
|
|
6d9380f96
|
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
$entry['id'] = $i['fileid'];
$entry['parentId'] = $i['parent'];
$entry['date'] = \OCP\Util::formatDate($i['mtime']);
$entry['mtime'] = $i['mtime'] * 1000;
// only pick out the needed attributes
$entry['icon'] = \OCA\Files\Helper::determineIcon($i);
if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) {
$entry['isPreviewAvailable'] = true;
}
$entry['name'] = $i->getName();
$entry['permissions'] = $i['permissions'];
$entry['mimetype'] = $i['mimetype'];
$entry['size'] = $i['size'];
$entry['type'] = $i['type'];
$entry['etag'] = $i['etag'];
if (isset($i['displayname_owner'])) {
$entry['shareOwner'] = $i['displayname_owner'];
}
if (isset($i['is_share_mount_point'])) {
$entry['isShareMountPoint'] = $i['is_share_mount_point'];
}
$mountType = null;
if ($i->isShared()) {
$mountType = 'shared';
} else if ($i->isMounted()) {
$mountType = 'external';
}
if ($mountType !== null) {
if ($i->getInternalPath() === '') {
$mountType .= '-root';
|
|
31b7f2792
|
136 |
} |
|
6d9380f96
|
137 |
$entry['mountType'] = $mountType; |
|
31b7f2792
|
138 |
} |
|
6d9380f96
|
139 140 |
return $entry; } |
|
31b7f2792
|
141 |
|
|
6d9380f96
|
142 143 144 145 146 147 148 149 150 |
/**
* Format file info for JSON
* @param \OCP\Files\FileInfo[] $fileInfos file infos
*/
public static function formatFileInfos($fileInfos) {
$files = array();
foreach ($fileInfos as $i) {
$files[] = self::formatFileInfo($i);
}
|
|
31b7f2792
|
151 152 153 154 155 |
return $files; } /** |
|
6d9380f96
|
156 157 158 159 160 161 162 |
* Retrieves the contents of the given directory and * returns it as a sorted array of FileInfo. * * @param string $dir path to the directory * @param string $sortAttribute attribute to sort on * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] files |
|
31b7f2792
|
163 |
*/ |
|
6d9380f96
|
164 165 166 167 |
public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false) {
$content = \OC\Files\Filesystem::getDirectoryContent($dir);
return self::sortFiles($content, $sortAttribute, $sortDescending);
|
|
31b7f2792
|
168 169 170 |
} /** |
|
6d9380f96
|
171 172 173 174 175 176 |
* Sort the given file info array * * @param \OCP\Files\FileInfo[] $files files to sort * @param string $sortAttribute attribute to sort on * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] sorted files |
|
31b7f2792
|
177 |
*/ |
|
6d9380f96
|
178 179 180 181 182 183 |
public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
$sortFunc = 'compareFileNames';
if ($sortAttribute === 'mtime') {
$sortFunc = 'compareTimestamp';
} else if ($sortAttribute === 'size') {
$sortFunc = 'compareSize';
|
|
31b7f2792
|
184 |
} |
|
6d9380f96
|
185 186 187 |
usort($files, array('\OCA\Files\Helper', $sortFunc));
if ($sortDescending) {
$files = array_reverse($files);
|
|
31b7f2792
|
188 |
} |
|
6d9380f96
|
189 |
return $files; |
|
31b7f2792
|
190 |
} |
|
03e52840d
|
191 |
} |