2013-01-19 03:31:09 +04:00
|
|
|
<?php
|
2014-04-11 19:00:39 +04:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author brumsel <brumsel@losecatcher.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Michael Jobst <mjobst+github@tecratech.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-04-11 19:00:39 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-20 18:46:33 +04:00
|
|
|
namespace OCA\Files;
|
2013-01-19 03:31:09 +04:00
|
|
|
|
2014-10-24 16:13:40 +04:00
|
|
|
use OCP\Files\FileInfo;
|
2017-08-22 19:46:47 +03:00
|
|
|
use OCP\ITagManager;
|
2014-10-24 16:13:40 +04:00
|
|
|
|
2014-04-11 19:00:39 +04:00
|
|
|
/**
|
|
|
|
* Helper class for manipulating file information
|
|
|
|
*/
|
2015-02-04 17:58:16 +03:00
|
|
|
class Helper {
|
|
|
|
/**
|
|
|
|
* @param string $dir
|
|
|
|
* @return array
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
2013-01-19 03:31:09 +04:00
|
|
|
public static function buildFileStorageStatistics($dir) {
|
2014-01-27 18:56:57 +04:00
|
|
|
// information about storage capacities
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo($dir);
|
2016-10-28 22:46:28 +03:00
|
|
|
$l = \OC::$server->getL10N('files');
|
2014-04-28 19:59:50 +04:00
|
|
|
$maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
|
|
|
|
$maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
|
2020-03-26 11:30:18 +03:00
|
|
|
$maxHumanFileSize = $l->t('Upload (max. %s)', [$maxHumanFileSize]);
|
2013-01-19 03:31:09 +04:00
|
|
|
|
2015-02-04 17:58:16 +03:00
|
|
|
return [
|
|
|
|
'uploadMaxFilesize' => $maxUploadFileSize,
|
|
|
|
'maxHumanFilesize' => $maxHumanFileSize,
|
|
|
|
'freeSpace' => $storageInfo['free'],
|
2017-12-21 12:48:43 +03:00
|
|
|
'quota' => $storageInfo['quota'],
|
|
|
|
'used' => $storageInfo['used'],
|
2015-06-05 19:21:41 +03:00
|
|
|
'usedSpacePercent' => (int)$storageInfo['relative'],
|
|
|
|
'owner' => $storageInfo['owner'],
|
|
|
|
'ownerDisplayName' => $storageInfo['ownerDisplayName'],
|
2015-02-04 17:58:16 +03:00
|
|
|
];
|
2013-01-19 03:31:09 +04:00
|
|
|
}
|
2013-09-12 02:39:52 +04:00
|
|
|
|
2013-10-28 23:22:06 +04:00
|
|
|
/**
|
|
|
|
* Determine icon for a given file
|
|
|
|
*
|
2014-07-30 18:29:18 +04:00
|
|
|
* @param \OCP\Files\FileInfo $file file info
|
2013-10-28 23:22:06 +04:00
|
|
|
* @return string icon URL
|
|
|
|
*/
|
2013-09-12 02:39:52 +04:00
|
|
|
public static function determineIcon($file) {
|
|
|
|
if($file['type'] === 'dir') {
|
2015-12-18 16:00:20 +03:00
|
|
|
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
|
2014-07-10 19:25:46 +04:00
|
|
|
// TODO: move this part to the client side, using mountType
|
2014-05-02 19:37:16 +04:00
|
|
|
if ($file->isShared()) {
|
2015-12-18 16:00:20 +03:00
|
|
|
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
|
2014-05-02 19:37:16 +04:00
|
|
|
} elseif ($file->isMounted()) {
|
2015-12-18 16:00:20 +03:00
|
|
|
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
|
2013-09-12 02:39:52 +04:00
|
|
|
}
|
2014-02-19 17:47:29 +04:00
|
|
|
}else{
|
2015-12-18 16:00:20 +03:00
|
|
|
$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
|
2013-09-12 02:39:52 +04:00
|
|
|
}
|
|
|
|
|
2014-02-19 17:47:29 +04:00
|
|
|
return substr($icon, 0, -3) . 'svg';
|
2013-09-12 02:39:52 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 15:07:18 +04:00
|
|
|
/**
|
|
|
|
* Comparator function to sort files alphabetically and have
|
|
|
|
* the directories appear first
|
2014-03-19 16:53:59 +04:00
|
|
|
*
|
|
|
|
* @param \OCP\Files\FileInfo $a file
|
|
|
|
* @param \OCP\Files\FileInfo $b file
|
|
|
|
* @return int -1 if $a must come before $b, 1 otherwise
|
2013-08-17 15:07:18 +04:00
|
|
|
*/
|
2014-10-24 16:13:40 +04:00
|
|
|
public static function compareFileNames(FileInfo $a, FileInfo $b) {
|
2014-03-19 16:53:59 +04:00
|
|
|
$aType = $a->getType();
|
|
|
|
$bType = $b->getType();
|
|
|
|
if ($aType === 'dir' and $bType !== 'dir') {
|
2013-08-17 15:07:18 +04:00
|
|
|
return -1;
|
2014-03-19 16:53:59 +04:00
|
|
|
} elseif ($aType !== 'dir' and $bType === 'dir') {
|
2013-08-17 15:07:18 +04:00
|
|
|
return 1;
|
|
|
|
} else {
|
2014-02-18 15:29:05 +04:00
|
|
|
return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 22:57:06 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-10-24 16:13:40 +04:00
|
|
|
public static function compareTimestamp(FileInfo $a, FileInfo $b) {
|
2014-04-03 22:57:06 +04:00
|
|
|
$aTime = $a->getMTime();
|
|
|
|
$bTime = $b->getMTime();
|
2014-10-09 00:40:57 +04:00
|
|
|
return ($aTime < $bTime) ? -1 : 1;
|
2014-04-03 22:57:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-10-24 16:13:40 +04:00
|
|
|
public static function compareSize(FileInfo $a, FileInfo $b) {
|
2014-04-03 22:57:06 +04:00
|
|
|
$aSize = $a->getSize();
|
|
|
|
$bSize = $b->getSize();
|
2014-10-08 03:17:45 +04:00
|
|
|
return ($aSize < $bSize) ? -1 : 1;
|
2014-04-03 22:57:06 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 15:07:18 +04:00
|
|
|
/**
|
2013-10-28 23:22:06 +04:00
|
|
|
* 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
|
2013-10-28 23:22:06 +04:00
|
|
|
* @return array formatted file info
|
2013-08-17 15:07:18 +04:00
|
|
|
*/
|
2014-10-24 16:13:40 +04:00
|
|
|
public static function formatFileInfo(FileInfo $i) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$entry = [];
|
2013-08-17 15:07:18 +04:00
|
|
|
|
2013-10-28 23:22:06 +04:00
|
|
|
$entry['id'] = $i['fileid'];
|
2014-04-08 01:03:19 +04:00
|
|
|
$entry['parentId'] = $i['parent'];
|
2013-10-28 23:22:06 +04:00
|
|
|
$entry['mtime'] = $i['mtime'] * 1000;
|
|
|
|
// only pick out the needed attributes
|
2014-05-27 16:24:35 +04:00
|
|
|
$entry['name'] = $i->getName();
|
2013-10-28 23:22:06 +04:00
|
|
|
$entry['permissions'] = $i['permissions'];
|
|
|
|
$entry['mimetype'] = $i['mimetype'];
|
|
|
|
$entry['size'] = $i['size'];
|
|
|
|
$entry['type'] = $i['type'];
|
|
|
|
$entry['etag'] = $i['etag'];
|
2014-11-18 20:53:45 +03:00
|
|
|
if (isset($i['tags'])) {
|
|
|
|
$entry['tags'] = $i['tags'];
|
|
|
|
}
|
2013-10-28 23:22:06 +04:00
|
|
|
if (isset($i['displayname_owner'])) {
|
|
|
|
$entry['shareOwner'] = $i['displayname_owner'];
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
2014-04-17 17:54:45 +04:00
|
|
|
if (isset($i['is_share_mount_point'])) {
|
|
|
|
$entry['isShareMountPoint'] = $i['is_share_mount_point'];
|
|
|
|
}
|
2014-07-10 19:25:46 +04:00
|
|
|
$mountType = null;
|
2017-04-26 15:53:11 +03:00
|
|
|
$mount = $i->getMountPoint();
|
|
|
|
$mountType = $mount->getMountType();
|
|
|
|
if ($mountType !== '') {
|
2014-07-10 19:25:46 +04:00
|
|
|
if ($i->getInternalPath() === '') {
|
|
|
|
$mountType .= '-root';
|
|
|
|
}
|
|
|
|
$entry['mountType'] = $mountType;
|
|
|
|
}
|
2014-08-27 13:28:31 +04:00
|
|
|
if (isset($i['extraData'])) {
|
|
|
|
$entry['extraData'] = $i['extraData'];
|
|
|
|
}
|
2013-10-28 23:22:06 +04:00
|
|
|
return $entry;
|
|
|
|
}
|
2013-09-12 02:39:52 +04:00
|
|
|
|
2013-10-28 23:22:06 +04:00
|
|
|
/**
|
|
|
|
* Format file info for JSON
|
|
|
|
* @param \OCP\Files\FileInfo[] $fileInfos file infos
|
2014-10-24 16:13:40 +04:00
|
|
|
* @return array
|
2013-10-28 23:22:06 +04:00
|
|
|
*/
|
|
|
|
public static function formatFileInfos($fileInfos) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$files = [];
|
2013-10-28 23:22:06 +04:00
|
|
|
foreach ($fileInfos as $i) {
|
|
|
|
$files[] = self::formatFileInfo($i);
|
|
|
|
}
|
2013-08-17 15:07:18 +04:00
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-28 23:22:06 +04:00
|
|
|
* Retrieves the contents of the given directory and
|
|
|
|
* returns it as a sorted array of FileInfo.
|
|
|
|
*
|
|
|
|
* @param string $dir path to the directory
|
2014-04-03 22:57:06 +04:00
|
|
|
* @param string $sortAttribute attribute to sort on
|
|
|
|
* @param bool $sortDescending true for descending sort, false otherwise
|
2015-04-13 11:35:45 +03:00
|
|
|
* @param string $mimetypeFilter limit returned content to this mimetype or mimepart
|
2013-10-28 23:22:06 +04:00
|
|
|
* @return \OCP\Files\FileInfo[] files
|
2013-08-17 15:07:18 +04:00
|
|
|
*/
|
2015-04-13 11:35:45 +03:00
|
|
|
public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
|
|
|
|
$content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
|
2013-10-28 23:22:06 +04:00
|
|
|
|
2014-04-03 22:57:06 +04:00
|
|
|
return self::sortFiles($content, $sortAttribute, $sortDescending);
|
|
|
|
}
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
/**
|
|
|
|
* Populate the result set with file tags
|
|
|
|
*
|
2015-01-13 16:47:54 +03:00
|
|
|
* @param array $fileList
|
2016-11-10 11:13:25 +03:00
|
|
|
* @param string $fileIdentifier identifier attribute name for values in $fileList
|
2017-08-22 19:46:47 +03:00
|
|
|
* @param ITagManager $tagManager
|
2015-01-13 16:47:54 +03:00
|
|
|
* @return array file list populated with tags
|
2014-11-18 20:53:45 +03:00
|
|
|
*/
|
2017-08-22 19:46:47 +03:00
|
|
|
public static function populateTags(array $fileList, $fileIdentifier = 'fileid', ITagManager $tagManager) {
|
|
|
|
$ids = [];
|
2014-11-18 20:53:45 +03:00
|
|
|
foreach ($fileList as $fileData) {
|
2017-08-22 19:46:47 +03:00
|
|
|
$ids[] = $fileData[$fileIdentifier];
|
2014-11-18 20:53:45 +03:00
|
|
|
}
|
2017-08-22 19:46:47 +03:00
|
|
|
$tagger = $tagManager->load('files');
|
|
|
|
$tags = $tagger->getTagsForObjects($ids);
|
2016-11-10 11:13:25 +03:00
|
|
|
|
|
|
|
if (!is_array($tags)) {
|
|
|
|
throw new \UnexpectedValueException('$tags must be an array');
|
|
|
|
}
|
|
|
|
|
2017-08-22 19:46:47 +03:00
|
|
|
// Set empty tag array
|
|
|
|
foreach ($fileList as $key => $fileData) {
|
|
|
|
$fileList[$key]['tags'] = [];
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:13:25 +03:00
|
|
|
if (!empty($tags)) {
|
2014-11-18 20:53:45 +03:00
|
|
|
foreach ($tags as $fileId => $fileTags) {
|
2016-11-10 11:13:25 +03:00
|
|
|
|
2017-08-22 19:46:47 +03:00
|
|
|
foreach ($fileList as $key => $fileData) {
|
|
|
|
if ($fileId !== $fileData[$fileIdentifier]) {
|
|
|
|
continue;
|
2016-11-10 11:13:25 +03:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:46:47 +03:00
|
|
|
$fileList[$key]['tags'] = $fileTags;
|
2016-11-10 11:13:25 +03:00
|
|
|
}
|
|
|
|
}
|
2014-11-18 20:53:45 +03:00
|
|
|
}
|
2017-08-22 19:46:47 +03:00
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
return $fileList;
|
|
|
|
}
|
|
|
|
|
2014-04-03 22:57:06 +04:00
|
|
|
/**
|
|
|
|
* Sort the given file info array
|
|
|
|
*
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param \OCP\Files\FileInfo[] $files files to sort
|
2014-04-03 22:57:06 +04:00
|
|
|
* @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) {
|
2014-04-11 19:00:39 +04:00
|
|
|
$sortFunc = 'compareFileNames';
|
2014-04-03 22:57:06 +04:00
|
|
|
if ($sortAttribute === 'mtime') {
|
2014-04-11 19:00:39 +04:00
|
|
|
$sortFunc = 'compareTimestamp';
|
2014-04-03 22:57:06 +04:00
|
|
|
} else if ($sortAttribute === 'size') {
|
2014-04-11 19:00:39 +04:00
|
|
|
$sortFunc = 'compareSize';
|
2014-04-03 22:57:06 +04:00
|
|
|
}
|
2020-03-26 11:30:18 +03:00
|
|
|
usort($files, [Helper::class, $sortFunc]);
|
2014-04-03 22:57:06 +04:00
|
|
|
if ($sortDescending) {
|
|
|
|
$files = array_reverse($files);
|
|
|
|
}
|
|
|
|
return $files;
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
2013-01-19 03:31:09 +04:00
|
|
|
}
|