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) {
|
|
|
|
$l = new \OC_L10N('files');
|
|
|
|
$maxUploadFilesize = \OCP\Util::maxUploadFilesize($dir);
|
|
|
|
$maxHumanFilesize = \OCP\Util::humanFileSize($maxUploadFilesize);
|
|
|
|
$maxHumanFilesize = $l->t('Upload') . ' max. ' . $maxHumanFilesize;
|
|
|
|
|
|
|
|
// information about storage capacities
|
2013-08-27 02:59:58 +04:00
|
|
|
$storageInfo = \OC_Helper::getStorageInfo($dir);
|
2013-01-19 03:31:09 +04:00
|
|
|
|
|
|
|
return array('uploadMaxFilesize' => $maxUploadFilesize,
|
|
|
|
'maxHumanFilesize' => $maxHumanFilesize,
|
|
|
|
'usedSpacePercent' => (int)$storageInfo['relative']);
|
|
|
|
}
|
2013-09-12 02:39:52 +04:00
|
|
|
|
|
|
|
public static function determineIcon($file) {
|
|
|
|
if($file['type'] === 'dir') {
|
|
|
|
$dir = $file['directory'];
|
|
|
|
$absPath = \OC\Files\Filesystem::getView()->getAbsolutePath($dir.'/'.$file['name']);
|
|
|
|
$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') {
|
|
|
|
return \OC_Helper::mimetypeIcon('dir-shared');
|
|
|
|
}
|
2013-11-06 18:11:14 +04:00
|
|
|
if ($sid[0] !== 'local' and $sid[0] !== 'home') {
|
2013-09-12 02:39:52 +04:00
|
|
|
return \OC_Helper::mimetypeIcon('dir-external');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \OC_Helper::mimetypeIcon('dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($file['isPreviewAvailable']) {
|
2013-09-23 12:26:46 +04:00
|
|
|
$pathForPreview = $file['directory'] . '/' . $file['name'];
|
2013-10-28 14:22:34 +04:00
|
|
|
return \OC_Helper::previewIcon($pathForPreview) . '&c=' . $file['etag'];
|
2013-09-12 02:39:52 +04:00
|
|
|
}
|
|
|
|
return \OC_Helper::mimetypeIcon($file['mimetype']);
|
|
|
|
}
|
|
|
|
|
2013-08-17 15:07:18 +04:00
|
|
|
/**
|
|
|
|
* Comparator function to sort files alphabetically and have
|
|
|
|
* the directories appear first
|
|
|
|
* @param array $a file
|
|
|
|
* @param array $b file
|
|
|
|
* @return -1 if $a must come before $b, 1 otherwise
|
|
|
|
*/
|
|
|
|
public static function fileCmp($a, $b) {
|
|
|
|
if ($a['type'] === 'dir' and $b['type'] !== 'dir') {
|
|
|
|
return -1;
|
|
|
|
} elseif ($a['type'] !== 'dir' and $b['type'] === 'dir') {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return strnatcasecmp($a['name'], $b['name']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the contents of the given directory and
|
|
|
|
* returns it as a sorted array.
|
|
|
|
* @param string $dir path to the directory
|
|
|
|
* @return array of files
|
|
|
|
*/
|
|
|
|
public static function getFiles($dir) {
|
|
|
|
$content = \OC\Files\Filesystem::getDirectoryContent($dir);
|
|
|
|
$files = array();
|
|
|
|
|
|
|
|
foreach ($content as $i) {
|
|
|
|
$i['date'] = \OCP\Util::formatDate($i['mtime']);
|
|
|
|
if ($i['type'] === 'file') {
|
|
|
|
$fileinfo = pathinfo($i['name']);
|
|
|
|
$i['basename'] = $fileinfo['filename'];
|
|
|
|
if (!empty($fileinfo['extension'])) {
|
|
|
|
$i['extension'] = '.' . $fileinfo['extension'];
|
|
|
|
} else {
|
|
|
|
$i['extension'] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$i['directory'] = $dir;
|
2013-09-17 15:33:47 +04:00
|
|
|
$i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($i['mimetype']);
|
2013-09-20 18:46:33 +04:00
|
|
|
$i['icon'] = \OCA\Files\Helper::determineIcon($i);
|
2013-08-17 15:07:18 +04:00
|
|
|
$files[] = $i;
|
|
|
|
}
|
2013-09-12 02:39:52 +04:00
|
|
|
|
2013-09-20 18:46:33 +04:00
|
|
|
usort($files, array('\OCA\Files\Helper', 'fileCmp'));
|
2013-08-17 15:07:18 +04:00
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits the given path into a breadcrumb structure.
|
|
|
|
* @param string $dir path to process
|
|
|
|
* @return array where each entry is a hash of the absolute
|
|
|
|
* directory path and its name
|
|
|
|
*/
|
|
|
|
public static function makeBreadcrumb($dir){
|
|
|
|
$breadcrumb = array();
|
|
|
|
$pathtohere = '';
|
|
|
|
foreach (explode('/', $dir) as $i) {
|
|
|
|
if ($i !== '') {
|
|
|
|
$pathtohere .= '/' . $i;
|
|
|
|
$breadcrumb[] = array('dir' => $pathtohere, 'name' => $i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $breadcrumb;
|
|
|
|
}
|
2013-08-30 01:45:02 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the numeric permissions for the given directory.
|
|
|
|
* @param string $dir directory without trailing slash
|
|
|
|
* @return numeric permissions
|
|
|
|
*/
|
|
|
|
public static function getDirPermissions($dir){
|
|
|
|
$permissions = \OCP\PERMISSION_READ;
|
|
|
|
if (\OC\Files\Filesystem::isCreatable($dir . '/')) {
|
|
|
|
$permissions |= \OCP\PERMISSION_CREATE;
|
|
|
|
}
|
|
|
|
if (\OC\Files\Filesystem::isUpdatable($dir . '/')) {
|
|
|
|
$permissions |= \OCP\PERMISSION_UPDATE;
|
|
|
|
}
|
|
|
|
if (\OC\Files\Filesystem::isDeletable($dir . '/')) {
|
|
|
|
$permissions |= \OCP\PERMISSION_DELETE;
|
|
|
|
}
|
|
|
|
if (\OC\Files\Filesystem::isSharable($dir . '/')) {
|
|
|
|
$permissions |= \OCP\PERMISSION_SHARE;
|
|
|
|
}
|
|
|
|
return $permissions;
|
|
|
|
}
|
2013-01-19 03:31:09 +04:00
|
|
|
}
|