2013-01-19 03:31:09 +04:00
|
|
|
<?php
|
|
|
|
|
2013-09-20 18:46:33 +04:00
|
|
|
namespace OCA\Files;
|
2013-01-19 03:31:09 +04:00
|
|
|
|
|
|
|
class Helper
|
|
|
|
{
|
|
|
|
public static function buildFileStorageStatistics($dir) {
|
2014-01-27 18:56:57 +04:00
|
|
|
// information about storage capacities
|
|
|
|
$storageInfo = \OC_Helper::getStorageInfo($dir);
|
|
|
|
|
2013-01-19 03:31:09 +04:00
|
|
|
$l = new \OC_L10N('files');
|
2014-01-27 18:56:57 +04:00
|
|
|
$maxUploadFilesize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
|
2013-01-19 03:31:09 +04:00
|
|
|
$maxHumanFilesize = \OCP\Util::humanFileSize($maxUploadFilesize);
|
2014-04-21 04:28:34 +04:00
|
|
|
$maxHumanFilesize = $l->t('Upload (max. %s)', array($maxHumanFilesize));
|
2013-01-19 03:31:09 +04:00
|
|
|
|
|
|
|
return array('uploadMaxFilesize' => $maxUploadFilesize,
|
|
|
|
'maxHumanFilesize' => $maxHumanFilesize,
|
2014-01-29 17:24:31 +04:00
|
|
|
'freeSpace' => $storageInfo['free'],
|
2013-01-19 03:31:09 +04:00
|
|
|
'usedSpacePercent' => (int)$storageInfo['relative']);
|
|
|
|
}
|
2013-09-12 02:39:52 +04:00
|
|
|
|
2013-10-28 23:22:06 +04:00
|
|
|
/**
|
|
|
|
* Determine icon for a given file
|
|
|
|
*
|
|
|
|
* @param \OC\Files\FileInfo $file file info
|
|
|
|
* @return string icon URL
|
|
|
|
*/
|
2013-09-12 02:39:52 +04:00
|
|
|
public static function determineIcon($file) {
|
|
|
|
if($file['type'] === 'dir') {
|
|
|
|
$dir = $file['directory'];
|
2014-02-19 18:08:05 +04:00
|
|
|
$icon = \OC_Helper::mimetypeIcon('dir');
|
2013-10-28 23:22:06 +04:00
|
|
|
$absPath = $file->getPath();
|
2013-09-12 02:39:52 +04:00
|
|
|
$mount = \OC\Files\Filesystem::getMountManager()->find($absPath);
|
|
|
|
if (!is_null($mount)) {
|
|
|
|
$sid = $mount->getStorageId();
|
|
|
|
if (!is_null($sid)) {
|
|
|
|
$sid = explode(':', $sid);
|
|
|
|
if ($sid[0] === 'shared') {
|
2014-02-19 18:08:05 +04:00
|
|
|
$icon = \OC_Helper::mimetypeIcon('dir-shared');
|
2014-04-08 18:56:10 +04:00
|
|
|
} elseif ($sid[0] !== 'local' and $sid[0] !== 'home') {
|
2014-02-19 18:08:05 +04:00
|
|
|
$icon = \OC_Helper::mimetypeIcon('dir-external');
|
2013-09-12 02:39:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 17:47:29 +04:00
|
|
|
}else{
|
2013-10-28 23:22:06 +04:00
|
|
|
$icon = \OC_Helper::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
|
|
|
*/
|
|
|
|
public static function fileCmp($a, $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-03-19 16:53:59 +04:00
|
|
|
return strnatcasecmp($a->getName(), $b->getName());
|
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
|
|
|
*/
|
2013-10-28 23:22:06 +04:00
|
|
|
public static function formatFileInfo($i) {
|
|
|
|
$entry = array();
|
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['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['name'];
|
|
|
|
$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'];
|
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'];
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
public static function formatFileInfos($fileInfos) {
|
|
|
|
$files = array();
|
|
|
|
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
|
|
|
|
* @return \OCP\Files\FileInfo[] files
|
2013-08-17 15:07:18 +04:00
|
|
|
*/
|
2013-10-28 23:22:06 +04:00
|
|
|
public static function getFiles($dir) {
|
|
|
|
$content = \OC\Files\Filesystem::getDirectoryContent($dir);
|
|
|
|
|
|
|
|
usort($content, array('\OCA\Files\Helper', 'fileCmp'));
|
|
|
|
return $content;
|
2013-08-17 15:07:18 +04:00
|
|
|
}
|
2013-01-19 03:31:09 +04:00
|
|
|
}
|