2023-03-10 20:15:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2023 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
|
|
|
use OCA\Memories\Util;
|
2023-03-10 20:15:13 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2023-10-15 19:46:35 +00:00
|
|
|
use OCP\Share\IManager;
|
|
|
|
use OCP\Share\IShare;
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class ShareController extends GenericApiController
|
2023-03-10 20:15:13 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get the tokens of a node shared using an external link
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function links(?int $id, ?string $path): Http\Response
|
2023-03-10 20:15:13 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id, $path) {
|
|
|
|
$file = $this->getNodeByIdOrPath($id, $path);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
$shares = \OC::$server->get(IManager::class)
|
|
|
|
->getSharesBy(Util::getUID(), IShare::TYPE_LINK, $file, true, 50, 0)
|
|
|
|
;
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
if (empty($shares)) {
|
|
|
|
throw Exceptions::NotFound('external links');
|
|
|
|
}
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
$links = array_map(fn ($s) => $this->makeShareResponse($s), $shares);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse($links, Http::STATUS_OK);
|
|
|
|
});
|
2023-03-10 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Share a node using an external link
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function createNode(?int $id, ?string $path): Http\Response
|
2023-03-10 20:15:13 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id, $path) {
|
|
|
|
$file = $this->getNodeByIdOrPath($id, $path);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
$manager = \OC::$server->get(IManager::class);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
$share = $manager->createShare(
|
|
|
|
$manager->newShare()
|
|
|
|
->setNode($file)
|
|
|
|
->setShareType(\OCP\Share\IShare::TYPE_LINK)
|
|
|
|
->setSharedBy(Util::getUID())
|
|
|
|
->setPermissions(\OCP\Constants::PERMISSION_READ),
|
|
|
|
);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse($this->makeShareResponse($share), Http::STATUS_OK);
|
|
|
|
});
|
2023-03-10 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Delete an external link share
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function deleteShare(string $id): Http\Response
|
2023-03-10 20:15:13 +00:00
|
|
|
{
|
2023-08-30 18:10:08 +00:00
|
|
|
return Util::guardEx(static function () use ($id) {
|
2023-03-23 20:32:23 +00:00
|
|
|
$uid = Util::getUID();
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
$manager = \OC::$server->get(\OCP\Share\IManager::class);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
$share = $manager->getShareById($id);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
if ($share->getSharedBy() !== $uid) {
|
|
|
|
throw Exceptions::Forbidden('You are not the owner of this share');
|
|
|
|
}
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
$manager->deleteShare($share);
|
2023-03-10 20:15:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
});
|
2023-03-10 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
private function getNodeByIdOrPath(?int $id, ?string $path): \OCP\Files\Node
|
2023-03-10 20:17:11 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
$uid = Util::getUID();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file = null;
|
|
|
|
if ($id) {
|
2023-03-23 21:45:56 +00:00
|
|
|
$file = $this->fs->getUserFile($id);
|
2023-03-23 20:32:23 +00:00
|
|
|
} elseif ($path) {
|
|
|
|
$file = Util::getUserFolder($uid)->get($path);
|
2023-03-10 21:25:34 +00:00
|
|
|
}
|
2023-10-15 19:46:35 +00:00
|
|
|
} catch (\OCP\Files\NotFoundException) {
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::NotFoundFile($path ?? $id);
|
2023-03-10 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$file || !$file->isShareable()) {
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::Forbidden('File not sharable');
|
2023-03-10 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
2023-03-10 21:25:34 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
private function makeShareResponse(IShare $share): array
|
2023-03-10 21:29:41 +00:00
|
|
|
{
|
2023-10-15 19:46:35 +00:00
|
|
|
$token = $share->getToken();
|
|
|
|
$url = \OC::$server->get(\OCP\IURLGenerator::class)
|
|
|
|
->linkToRouteAbsolute('memories.Public.showShare', ['token' => $token])
|
|
|
|
;
|
2023-03-10 21:25:34 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
/**
|
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType
|
|
|
|
* @psalm-suppress DocblockTypeContradiction
|
|
|
|
*/
|
|
|
|
$expiration = $share->getExpirationDate()?->getTimestamp();
|
2023-03-10 21:25:34 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $share->getFullId(),
|
|
|
|
'label' => $share->getLabel(),
|
2023-10-15 19:46:35 +00:00
|
|
|
'token' => $token,
|
2023-03-10 21:25:34 +00:00
|
|
|
'url' => $url,
|
|
|
|
'hasPassword' => $share->getPassword() ? true : false,
|
2023-10-15 19:46:35 +00:00
|
|
|
'expiration' => $expiration,
|
2023-03-10 21:25:34 +00:00
|
|
|
'editable' => $share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE,
|
|
|
|
];
|
|
|
|
}
|
2023-03-10 20:15:13 +00:00
|
|
|
}
|