summaryrefslogtreecommitdiffstats
path: root/apps/files/lib/helper.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/lib/helper.php')
-rw-r--r--apps/files/lib/helper.php87
1 files changed, 87 insertions, 0 deletions
diff --git a/apps/files/lib/helper.php b/apps/files/lib/helper.php
index 9170c6e3fc0..3c13b8ea6e2 100644
--- a/apps/files/lib/helper.php
+++ b/apps/files/lib/helper.php
@@ -45,5 +45,92 @@ class Helper
return \OC_Helper::mimetypeIcon($file['mimetype']);
}
+ /**
+ * 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;
+ $i['isPreviewAvailable'] = \OCP\Preview::isMimeSupported($i['mimetype']);
+ $i['icon'] = \OCA\files\lib\Helper::determineIcon($i);
+ $files[] = $i;
+ }
+
+ usort($files, array('\OCA\files\lib\Helper', 'fileCmp'));
+
+ 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;
+ }
+
+ /**
+ * 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;
+ }
}