2014-06-06 07:16:47 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-01-17 14:00:09 -05:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2014-06-06 07:16:47 -04:00
|
|
|
/**
|
2024-06-06 03:55:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-06-06 07:16:47 -04:00
|
|
|
*/
|
2018-01-17 14:00:09 -05:00
|
|
|
namespace OCA\Files_External\Controller;
|
2014-06-06 07:16:47 -04:00
|
|
|
|
2021-03-26 10:07:39 -04:00
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2023-06-14 10:28:26 -04:00
|
|
|
use OCA\Files_External\ResponseDefinitions;
|
2021-03-26 10:07:39 -04:00
|
|
|
use OCA\Files_External\Service\UserGlobalStoragesService;
|
|
|
|
|
use OCA\Files_External\Service\UserStoragesService;
|
2023-06-14 10:28:26 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2024-07-25 07:14:46 -04:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
2018-01-17 14:00:09 -05:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
|
use OCP\AppFramework\OCSController;
|
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
2023-06-14 10:28:26 -04:00
|
|
|
/**
|
2023-10-23 10:47:38 -04:00
|
|
|
* @psalm-import-type Files_ExternalMount from ResponseDefinitions
|
2023-06-14 10:28:26 -04:00
|
|
|
*/
|
2018-01-17 14:00:09 -05:00
|
|
|
class ApiController extends OCSController {
|
|
|
|
|
|
2023-07-13 03:58:24 -04:00
|
|
|
private UserGlobalStoragesService $userGlobalStoragesService;
|
|
|
|
|
private UserStoragesService $userStoragesService;
|
2018-01-17 14:00:09 -05:00
|
|
|
|
2021-03-26 10:07:39 -04:00
|
|
|
public function __construct(
|
|
|
|
|
string $appName,
|
|
|
|
|
IRequest $request,
|
|
|
|
|
UserGlobalStoragesService $userGlobalStorageService,
|
|
|
|
|
UserStoragesService $userStorageService
|
|
|
|
|
) {
|
2018-01-17 14:00:09 -05:00
|
|
|
parent::__construct($appName, $request);
|
2021-03-26 10:07:39 -04:00
|
|
|
$this->userGlobalStoragesService = $userGlobalStorageService;
|
|
|
|
|
$this->userStoragesService = $userStorageService;
|
2018-01-17 14:00:09 -05:00
|
|
|
}
|
2014-06-06 07:16:47 -04:00
|
|
|
|
2014-06-06 07:50:41 -04:00
|
|
|
/**
|
|
|
|
|
* Formats the given mount config to a mount entry.
|
2015-11-12 07:40:28 -05:00
|
|
|
*
|
2014-06-30 06:33:11 -04:00
|
|
|
* @param string $mountPoint mount point name, relative to the data dir
|
2021-03-26 10:07:39 -04:00
|
|
|
* @param StorageConfig $mountConfig mount config to format
|
2014-06-06 07:50:41 -04:00
|
|
|
*
|
2023-10-23 10:47:38 -04:00
|
|
|
* @return Files_ExternalMount
|
2014-06-06 07:50:41 -04:00
|
|
|
*/
|
2021-03-26 10:07:39 -04:00
|
|
|
private function formatMount(string $mountPoint, StorageConfig $mountConfig): array {
|
2014-06-30 06:33:11 -04:00
|
|
|
// split path from mount point
|
2018-01-17 14:00:09 -05:00
|
|
|
$path = \dirname($mountPoint);
|
2021-03-26 10:07:39 -04:00
|
|
|
if ($path === '.' || $path === '/') {
|
2014-06-06 07:50:41 -04:00
|
|
|
$path = '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 10:07:39 -04:00
|
|
|
$isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN;
|
2014-06-30 06:33:11 -04:00
|
|
|
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions = \OCP\Constants::PERMISSION_READ;
|
2014-06-06 07:50:41 -04:00
|
|
|
// personal mounts can be deleted
|
|
|
|
|
if (!$isSystemMount) {
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions |= \OCP\Constants::PERMISSION_DELETE;
|
2014-06-06 07:50:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$entry = [
|
2023-07-13 03:58:24 -04:00
|
|
|
'id' => $mountConfig->getId(),
|
|
|
|
|
'type' => 'dir',
|
2014-06-30 06:33:11 -04:00
|
|
|
'name' => basename($mountPoint),
|
2014-06-06 07:50:41 -04:00
|
|
|
'path' => $path,
|
2015-11-12 07:40:28 -05:00
|
|
|
'permissions' => $permissions,
|
2023-07-13 03:58:24 -04:00
|
|
|
'scope' => $isSystemMount ? 'system' : 'personal',
|
|
|
|
|
'backend' => $mountConfig->getBackend()->getText(),
|
2021-03-26 10:07:39 -04:00
|
|
|
'class' => $mountConfig->getBackend()->getIdentifier(),
|
2023-07-13 03:58:24 -04:00
|
|
|
'config' => $mountConfig->jsonSerialize(true),
|
2020-03-26 04:30:18 -04:00
|
|
|
];
|
2014-06-06 07:50:41 -04:00
|
|
|
return $entry;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 07:16:47 -04:00
|
|
|
/**
|
2023-06-14 10:28:26 -04:00
|
|
|
* Get the mount points visible for this user
|
2014-06-06 07:16:47 -04:00
|
|
|
*
|
2023-10-23 10:47:38 -04:00
|
|
|
* @return DataResponse<Http::STATUS_OK, Files_ExternalMount[], array{}>
|
2023-09-19 08:12:17 -04:00
|
|
|
*
|
|
|
|
|
* 200: User mounts returned
|
2014-06-06 07:16:47 -04:00
|
|
|
*/
|
2024-07-25 07:14:46 -04:00
|
|
|
#[NoAdminRequired]
|
2018-01-17 14:00:09 -05:00
|
|
|
public function getUserMounts(): DataResponse {
|
|
|
|
|
$entries = [];
|
2021-03-26 10:07:39 -04:00
|
|
|
$mountPoints = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->userGlobalStoragesService->getStorages() as $storage) {
|
|
|
|
|
$mountPoint = $storage->getMountPoint();
|
|
|
|
|
$mountPoints[$mountPoint] = $storage;
|
|
|
|
|
}
|
2014-06-06 07:16:47 -04:00
|
|
|
|
2021-03-26 10:07:39 -04:00
|
|
|
foreach ($this->userStoragesService->getStorages() as $storage) {
|
|
|
|
|
$mountPoint = $storage->getMountPoint();
|
|
|
|
|
$mountPoints[$mountPoint] = $storage;
|
|
|
|
|
}
|
|
|
|
|
foreach ($mountPoints as $mountPoint => $mount) {
|
2018-01-17 14:00:09 -05:00
|
|
|
$entries[] = $this->formatMount($mountPoint, $mount);
|
2014-06-06 07:16:47 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:00:09 -05:00
|
|
|
return new DataResponse($entries);
|
2014-06-06 07:16:47 -04:00
|
|
|
}
|
|
|
|
|
}
|