nextcloud/apps/files/lib/helper.php

248 lines
7.3 KiB
PHP
Raw Normal View History

<?php
/**
2015-03-26 13:44:34 +03:00
* @author Björn Schießle <schiessle@owncloud.com>
* @author brumsel <brumsel@losecatcher.de>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Lukas Reschke <lukas@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
2013-09-20 18:46:33 +04:00
namespace OCA\Files;
use OCP\Files\FileInfo;
/**
* Helper class for manipulating file information
*/
class Helper {
/**
* @param string $dir
* @return array
* @throws \OCP\Files\NotFoundException
*/
public static function buildFileStorageStatistics($dir) {
// information about storage capacities
$storageInfo = \OC_Helper::getStorageInfo($dir);
$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 [
'uploadMaxFilesize' => $maxUploadFileSize,
'maxHumanFilesize' => $maxHumanFileSize,
'freeSpace' => $storageInfo['free'],
'usedSpacePercent' => (int)$storageInfo['relative'],
'owner' => $storageInfo['owner'],
'ownerDisplayName' => $storageInfo['ownerDisplayName'],
];
}
/**
* Determine icon for a given file
*
2014-07-30 18:29:18 +04:00
* @param \OCP\Files\FileInfo $file file info
* @return string icon URL
*/
public static function determineIcon($file) {
if($file['type'] === 'dir') {
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
// TODO: move this part to the client side, using mountType
if ($file->isShared()) {
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
} elseif ($file->isMounted()) {
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
}
}else{
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
}
return substr($icon, 0, -3) . 'svg';
}
/**
* Comparator function to sort files alphabetically and have
* the directories appear first
*
* @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 compareFileNames(FileInfo $a, FileInfo $b) {
$aType = $a->getType();
$bType = $b->getType();
if ($aType === 'dir' and $bType !== 'dir') {
return -1;
} elseif ($aType !== 'dir' and $bType === 'dir') {
return 1;
} else {
return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
}
}
/**
* 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
*/
public static function compareTimestamp(FileInfo $a, FileInfo $b) {
$aTime = $a->getMTime();
$bTime = $b->getMTime();
2014-10-09 00:40:57 +04:00
return ($aTime < $bTime) ? -1 : 1;
}
/**
* 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(FileInfo $a, FileInfo $b) {
$aSize = $a->getSize();
$bSize = $b->getSize();
2014-10-08 03:17:45 +04:00
return ($aSize < $bSize) ? -1 : 1;
}
/**
* Formats the file info to be returned as JSON to the client.
*
2014-04-08 01:03:19 +04:00
* @param \OCP\Files\FileInfo $i
* @return array formatted file info
*/
public static function formatFileInfo(FileInfo $i) {
$entry = array();
$entry['id'] = $i['fileid'];
2014-04-08 01:03:19 +04:00
$entry['parentId'] = $i['parent'];
$entry['mtime'] = $i['mtime'] * 1000;
// only pick out the needed attributes
2014-05-27 16:24:35 +04:00
$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['tags'])) {
$entry['tags'] = $i['tags'];
}
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';
}
$entry['mountType'] = $mountType;
}
if (isset($i['extraData'])) {
$entry['extraData'] = $i['extraData'];
}
return $entry;
}
/**
* Format file info for JSON
* @param \OCP\Files\FileInfo[] $fileInfos file infos
* @return array
*/
public static function formatFileInfos($fileInfos) {
$files = array();
foreach ($fileInfos as $i) {
$files[] = self::formatFileInfo($i);
}
return $files;
}
/**
* 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
* @param string $mimetypeFilter limit returned content to this mimetype or mimepart
* @return \OCP\Files\FileInfo[] files
*/
public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
$content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
return self::sortFiles($content, $sortAttribute, $sortDescending);
}
/**
* Populate the result set with file tags
*
2015-01-13 16:47:54 +03:00
* @param array $fileList
* @return array file list populated with tags
*/
2015-01-13 16:47:54 +03:00
public static function populateTags(array $fileList) {
$filesById = array();
foreach ($fileList as $fileData) {
$filesById[$fileData['fileid']] = $fileData;
}
$tagger = \OC::$server->getTagManager()->load('files');
$tags = $tagger->getTagsForObjects(array_keys($filesById));
if ($tags) {
foreach ($tags as $fileId => $fileTags) {
$filesById[$fileId]['tags'] = $fileTags;
}
}
return $fileList;
}
/**
* Sort the given file info array
*
2014-05-13 15:29:25 +04:00
* @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
*/
public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
$sortFunc = 'compareFileNames';
if ($sortAttribute === 'mtime') {
$sortFunc = 'compareTimestamp';
} else if ($sortAttribute === 'size') {
$sortFunc = 'compareSize';
}
usort($files, array('\OCA\Files\Helper', $sortFunc));
if ($sortDescending) {
$files = array_reverse($files);
}
return $files;
}
}