summaryrefslogtreecommitdiffstats
path: root/apps/files/lib/helper.php
blob: 282f0678a9ace233764e889cfead4c3563be2dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace OCA\files\lib;

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
		$storageInfo = \OC_Helper::getStorageInfo($dir);

		return array('uploadMaxFilesize' => $maxUploadFilesize,
					 'maxHumanFilesize'  => $maxHumanFilesize,
					 'usedSpacePercent'  => (int)$storageInfo['relative']);
	}

	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');
					}
					if ($sid[0] !== 'local') {
						return \OC_Helper::mimetypeIcon('dir-external');
					}
				}
			}
			return \OC_Helper::mimetypeIcon('dir');
		}

		if($file['isPreviewAvailable']) {
			$relativePath = substr($file['path'], 6);
			return \OC_Helper::previewIcon($relativePath);
		}
		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;
	}
}