Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed calculation of used space #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@
'url' => '/mime/img/{iconname}.svg',
'verb' => 'GET',
],
[
'name' => 'ajax#getStorageStats',
'url' => '/ajax/getstoragestats',
'verb' => 'GET',
],
[
'name' => 'Api#getStorageStats',
'url' => '/api/v1/stats',
'verb' => 'GET'
],
]
];
59 changes: 59 additions & 0 deletions lib/Controller/AjaxController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 T-Systems International
*
* @author M. Mura <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\NMCTheme\Controller;

use OCA\NMCTheme\Service\NMCFilesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\NotFoundException;
use OCP\IRequest;

class AjaxController extends Controller {

/** @var NMCFilesService */
private $filesService;

public function __construct(string $appName, IRequest $request, NMCFilesService $filesService) {
parent::__construct($appName, $request);
$this->filesService = $filesService;
}

/**
* @NoAdminRequired
*/
public function getStorageStats(string $dir = '/'): JSONResponse {
$storageInfo = $this->filesService->buildFileStorageStatistics($dir ?: '/');
$l = \OC::$server->getL10N('files');

$maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
$maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
$maxHumanFileSize = $l->t('Upload (max. %s)', [$maxHumanFileSize]);

$storageInfo['uploadMaxFilesize'] = $maxUploadFileSize;
$storageInfo['maxHumanFilesize'] = $maxHumanFileSize;
$storageInfo['usedSpacePercent'] = $storageInfo['relative'];

try {
return new JSONResponse([
'status' => 'success',
'data' => $storageInfo,
]);
} catch (NotFoundException $e) {
return new JSONResponse([
'status' => 'error',
'data' => [
'message' => 'Folder not found'
],
]);
}
}
}
36 changes: 36 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 T-Systems International
*
* @author M. Mura <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\NMCTheme\Controller;

use OCA\NMCTheme\Service\NMCFilesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

class ApiController extends Controller {

/** @var NMCFilesService */
private $filesService;

public function __construct(string $appName, IRequest $request, NMCFilesService $filesService) {
parent::__construct($appName, $request);
$this->filesService = $filesService;
}

/**
* @NoAdminRequired
*/
public function getStorageStats(string $dir = '/'): JSONResponse {
$storageInfo = $this->filesService->buildFileStorageStatistics($dir ?: '/');
return new JSONResponse(['message' => 'ok', 'data' => $storageInfo]);
}
}
12 changes: 11 additions & 1 deletion lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
namespace OCA\NMCTheme\Listener;

use OC\Security\CSP\ContentSecurityPolicyNonceManager;
use OCA\NMCTheme\Service\NMCFilesService;
use OCA\Theming\Util;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IL10N;
Expand All @@ -25,18 +27,24 @@ class BeforeTemplateRenderedListener implements IEventListener {
private ContentSecurityPolicyNonceManager $nonceManager;
private Util $themingUtil;
private INavigationManager $navManager;
private IInitialState $initialState;
private NMCFilesService $filesService;
private IL10N $l;

public function __construct(
IURLGenerator $urlGenerator,
ContentSecurityPolicyNonceManager $nonceManager,
Util $themingUtil,
INavigationManager $navManager,
IInitialState $initialState,
NMCFilesService $filesService,
IL10N $l) {
$this->urlGenerator = $urlGenerator;
$this->nonceManager = $nonceManager;
$this->themingUtil = $themingUtil;
$this->navManager = $navManager;
$this->initialState = $initialState;
$this->filesService = $filesService;
$this->l = $l;
}

Expand Down Expand Up @@ -88,7 +96,9 @@ public function handle(Event $event): void {
[ 'nonce' => $this->nonceManager->getNonce(),
'src' => $mimetypelist . '?nmcv=' . $this->themingUtil->getCacheBuster() ],
''); // the empty text is needed to generate HTML5 valid tags


// provide corrected storageStats for v27
$this->initialState->provideInitialState('storageStats', $this->filesService->buildFileStorageStatistics());

// you can add additional styles, links and scripts before rendering
// keep src for future use: \OCP\Util::addScript("nmctheme", "../dist/l10nappender");
Expand Down
43 changes: 43 additions & 0 deletions lib/Service/NMCFilesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\NMCTheme\Service;

use OC\Files\View;
use OCP\IL10N;

class NMCFilesService {
Expand Down Expand Up @@ -68,4 +69,46 @@ public function addFilesAppNavigationEntries() {
];
});
}

public static function buildFileStorageStatistics(string $dir = '/') {
// information about storage capacities
$storageInfo = \OC_Helper::getStorageInfo($dir);
$trashbin = self::getTrashbinSize(\OC_User::getUser());
$free = $storageInfo['free'] - $trashbin;
$used = $storageInfo['used'] + $trashbin;
$relative = self::getUsedRelative($storageInfo['quota'], $storageInfo['total'], $used);

return [
'freeSpace' => $free,
'quota' => $storageInfo['quota'],
'total' => $storageInfo['total'],
'used' => $used,
'relative' => $relative,
'trashbin' => $trashbin,
'owner' => $storageInfo['owner'],
'ownerDisplayName' => $storageInfo['ownerDisplayName'],
'mountType' => $storageInfo['mountType'],
'mountPoint' => $storageInfo['mountPoint'],
];
}

private static function getTrashbinSize($user) {
$view = new View('/' . $user);
$fileInfo = $view->getFileInfo('/files_trashbin');
return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
}

private static function getUsedRelative($quota, $total, $used) {
if ($total > 0) {
if ($quota > 0 && $total > $quota) {
$total = $quota;
}
// prevent division by zero or error codes (negative values)
$relative = round(($used / $total) * 10000) / 100;
} else {
$relative = 0;
}

return $relative;
}
}
4 changes: 2 additions & 2 deletions src/components/StorageQuota.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export default {
try {
const response = await axios.get(
IS_LEGACY_VERSION
? generateUrl('/apps/files/ajax/getstoragestats')
: generateUrl('/apps/files/api/v1/stats'),
? generateUrl('/apps/nmctheme/ajax/getstoragestats')
: generateUrl('/apps/nmctheme/api/v1/stats'),
)
if (!response?.data?.data) {
throw new Error('Invalid storage stats')
Expand Down
9 changes: 7 additions & 2 deletions src/components/filesSettings.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ const CONFIG_SETTINGS_MAPPING = {
}

export const loadStats = async () => {
let browserState = loadState('files', 'storageStats', null)
let browserState = loadState('nmctheme', 'storageStats', null)
// v25
if (!browserState) {
try {
const response = await axios.get(generateUrl('/apps/files/ajax/getstoragestats'))
let statsUrl = generateUrl('/apps/nmctheme/api/v1/stats')
// v25
if (IS_LEGACY_VERSION) {
statsUrl = generateUrl('/apps/nmctheme/ajax/getstoragestats')
}
const response = await axios.get(statsUrl)
if (!response?.data?.data) {
throw new Error('Invalid storage stats')
}
Expand Down
Loading