Skip to content
Closed
4 changes: 2 additions & 2 deletions apps/files/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,6 @@ public function getNodeType($folderpath) {
$node = $this->userFolder->get($folderpath);
return $node->getType();
}


}
189 changes: 135 additions & 54 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ class ViewController extends Controller {
protected $activityHelper;

public function __construct(string $appName,
IRequest $request,
IURLGenerator $urlGenerator,
IL10N $l10n,
IConfig $config,
EventDispatcherInterface $eventDispatcherInterface,
IUserSession $userSession,
IAppManager $appManager,
IRootFolder $rootFolder,
Helper $activityHelper
IRequest $request,
IURLGenerator $urlGenerator,
IL10N $l10n,
IConfig $config,
EventDispatcherInterface $eventDispatcherInterface,
IUserSession $userSession,
IAppManager $appManager,
IRootFolder $rootFolder,
Helper $activityHelper
) {
parent::__construct($appName, $request);
$this->appName = $appName;
$this->request = $request;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
$this->config = $config;
$this->appName = $appName;
$this->request = $request;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
$this->config = $config;
$this->eventDispatcher = $eventDispatcherInterface;
$this->userSession = $userSession;
$this->appManager = $appManager;
$this->rootFolder = $rootFolder;
$this->activityHelper = $activityHelper;
$this->userSession = $userSession;
$this->appManager = $appManager;
$this->rootFolder = $rootFolder;
$this->activityHelper = $activityHelper;
}

/**
Expand All @@ -102,8 +102,8 @@ public function __construct(string $appName,
* @return string
*/
protected function renderScript($appName, $scriptName) {
$content = '';
$appPath = \OC_App::getAppPath($appName);
$content = '';
$appPath = \OC_App::getAppPath($appName);
$scriptPath = $appPath . '/' . $scriptName;
if (file_exists($scriptPath)) {
// TODO: sanitize path / script name ?
Expand Down Expand Up @@ -173,21 +173,21 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
$favoritesSublistArray = Array();

$navBarPositionPosition = 6;
$currentCount = 0;
$currentCount = 0;
foreach ($favElements['folders'] as $dir) {

$id = substr($dir, strrpos($dir, '/') + 1, strlen($dir));
$link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']);
$id = substr($dir, strrpos($dir, '/') + 1, strlen($dir));
$link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']);
$sortingValue = ++$currentCount;
$element = [
'id' => str_replace('/', '-', $dir),
'view' => 'files',
'href' => $link,
'dir' => $dir,
'order' => $navBarPositionPosition,
'folderPosition' => $sortingValue,
'name' => $id,
'icon' => 'files',
$element = [
'id' => str_replace('/', '-', $dir),
'view' => 'files',
'href' => $link,
'dir' => $dir,
'order' => $navBarPositionPosition,
'folderPosition' => $sortingValue,
'name' => $id,
'icon' => 'files',
'quickaccesselement' => 'true'
];

Expand All @@ -210,11 +210,12 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal

$nav->assign('navigationItems', $navItems);

$nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used']));
$nav->assign('usage', $this->showAsPartsOfUnit($storageInfo['used'], $this->getEnding($storageInfo['total'])));
if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
$totalSpace = $this->l10n->t('Unlimited');
$nav->assign('usage', $this->reduceStorageSizeBySensibleBoundaries($storageInfo['used']));
} else {
$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
$totalSpace = $this->reduceStorageSizeBySensibleBoundaries($storageInfo['total']);
}
$nav->assign('total_space', $totalSpace);
$nav->assign('quota', $storageInfo['quota']);
Expand All @@ -236,34 +237,34 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
$subcontent = $this->renderScript($subitem['appname'], $subitem['script']);
}
$contentItems[$subitem['id']] = [
'id' => $subitem['id'],
'id' => $subitem['id'],
'content' => $subcontent
];
}
}
$contentItems[$item['id']] = [
'id' => $item['id'],
'id' => $item['id'],
'content' => $content
];
}

$event = new GenericEvent(null, ['hiddenFields' => []]);
$this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts', $event);

$params = [];
$params['usedSpacePercent'] = (int) $storageInfo['relative'];
$params['owner'] = $storageInfo['owner'];
$params['ownerDisplayName'] = $storageInfo['ownerDisplayName'];
$params['isPublic'] = false;
$params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
$params = [];
$params['usedSpacePercent'] = (int)$storageInfo['relative'];
$params['owner'] = $storageInfo['owner'];
$params['ownerDisplayName'] = $storageInfo['ownerDisplayName'];
$params['isPublic'] = false;
$params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
$params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
$showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
$params['showHiddenFiles'] = $showHidden ? 1 : 0;
$params['fileNotFound'] = $fileNotFound ? 1 : 0;
$params['appNavigation'] = $nav;
$params['appContents'] = $contentItems;
$params['hiddenFields'] = $event->getArgument('hiddenFields');
$showHidden = (bool)$this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
$params['showHiddenFiles'] = $showHidden ? 1 : 0;
$params['fileNotFound'] = $fileNotFound ? 1 : 0;
$params['appNavigation'] = $nav;
$params['appContents'] = $contentItems;
$params['hiddenFields'] = $event->getArgument('hiddenFields');

$response = new TemplateResponse(
$this->appName,
Expand All @@ -285,14 +286,14 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
* @throws \OCP\Files\NotFoundException
*/
private function showFile($fileId) {
$uid = $this->userSession->getUser()->getUID();
$uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->getUserFolder($uid);
$files = $baseFolder->getById($fileId);
$params = [];
$files = $baseFolder->getById($fileId);
$params = [];

if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$files = $baseFolder->getById($fileId);
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$files = $baseFolder->getById($fileId);
$params['view'] = 'trashbin';
}

Expand All @@ -312,4 +313,84 @@ private function showFile($fileId) {
}
throw new \OCP\Files\NotFoundException();
}

/**
* Cuts of an int containing the amount of bytes of a given size, to a sensible way. It converts the bytecount to a human readable size, and then cuts it down under a certain threshhold.
*
* @param int $storage storagevalue to be cut off
* @return int human readable filesize which is reduced to a sensible value.
*/
private function reduceStorageSizeBySensibleBoundaries($storage) {
$reducedStorageSize = \OC_Helper::humanFileSize($storage);
$ending = $this->getEnding($storage);

if ($storage > 10485760 && $storage < 1073741824) { //Over 10MB under 1GB
$reducedStorageSize = round($reducedStorageSize);
} else if ($storage > 10737418240 && $storage < 1099511531399) { //Over 10GB under 1TB
$reducedStorageSize = round($reducedStorageSize);
} else {
return $reducedStorageSize;
}

return $reducedStorageSize . " " . $ending;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to squash all the commits into one?


/**
* Returns either '<0.1' or a readable Size, depending on the unit. If the given size is smaller than 10% of the given unit, '<0.1' is returned.
*
* @param $bytes bytes of the used space
* @param $unit unit of the total assigned space
* @return String Size
*/
private function showAsPartsOfUnit($bytes,$unit) {

if ($unit == "KB") {
if ($bytes < 102) {
return "<0.1";
}
}

if ($unit == "MB") {
if ($bytes < 104857) {
return "<0.1";
}
}

if ($unit == "GB") {
if ($bytes < 107374182) {
return "<0.1";
}
}

if ($unit == "TB") {
if ($bytes < 109951162777) {
return "<0.1";
}
}

return $this->stripEnding($bytes);
}

/**
* Returns the humanFileSize, but without the unit.
*
* @param $bytes bytes
* @return String Size
*/
private function stripEnding($bytes) {
$sizeHuman = \OC_Helper::humanFileSize($bytes);
return trim(substr($sizeHuman, 0, (strrpos($sizeHuman, "B") - 1)));
}

/**
* Returns the unit of humanFileSize
*
* @param $bytes bytes
* @return String Unit
*/
private function getEnding($bytes) {
$sizeHuman = \OC_Helper::humanFileSize($bytes);
return trim(substr($sizeHuman, (strrpos($sizeHuman, "B") - 1), strlen($sizeHuman)));
}

}