2018-04-13 08:04:41 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-04-13 08:04:41 -04:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-04-13 08:04:41 -04:00
|
|
|
/**
|
2024-05-27 11:39:07 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-04-13 08:04:41 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\DAV\Controller;
|
|
|
|
|
|
2018-04-13 10:54:24 -04:00
|
|
|
use OCA\DAV\Db\Direct;
|
2018-04-13 08:04:41 -04:00
|
|
|
use OCA\DAV\Db\DirectMapper;
|
2023-03-20 13:46:52 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2024-07-25 07:14:45 -04:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
2018-04-13 10:54:24 -04:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2018-04-26 04:34:57 -04:00
|
|
|
use OCP\AppFramework\OCS\OCSBadRequestException;
|
2022-06-13 09:50:43 -04:00
|
|
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
2023-11-23 04:22:34 -05:00
|
|
|
use OCP\AppFramework\OCS\OCSNotFoundException;
|
2018-04-13 08:04:41 -04:00
|
|
|
use OCP\AppFramework\OCSController;
|
2018-04-13 10:54:24 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2022-06-13 09:50:43 -04:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2022-07-15 11:11:54 -04:00
|
|
|
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
|
2018-04-26 04:34:57 -04:00
|
|
|
use OCP\Files\File;
|
2018-04-13 08:04:41 -04:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
|
use OCP\IRequest;
|
2018-04-13 10:54:24 -04:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
use OCP\Security\ISecureRandom;
|
2018-04-13 08:04:41 -04:00
|
|
|
|
|
|
|
|
class DirectController extends OCSController {
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
|
|
|
|
string $appName,
|
2023-11-23 04:22:34 -05:00
|
|
|
IRequest $request,
|
2024-10-18 06:04:22 -04:00
|
|
|
private IRootFolder $rootFolder,
|
|
|
|
|
private string $userId,
|
|
|
|
|
private DirectMapper $mapper,
|
|
|
|
|
private ISecureRandom $random,
|
|
|
|
|
private ITimeFactory $timeFactory,
|
|
|
|
|
private IURLGenerator $urlGenerator,
|
|
|
|
|
private IEventDispatcher $eventDispatcher,
|
|
|
|
|
) {
|
2018-04-13 08:04:41 -04:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 10:54:24 -04:00
|
|
|
/**
|
2023-03-20 13:46:52 -04:00
|
|
|
* Get a direct link to a file
|
|
|
|
|
*
|
|
|
|
|
* @param int $fileId ID of the file
|
|
|
|
|
* @param int $expirationTime Duration until the link expires
|
|
|
|
|
* @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>
|
|
|
|
|
* @throws OCSNotFoundException File not found
|
|
|
|
|
* @throws OCSBadRequestException Getting direct link is not possible
|
|
|
|
|
* @throws OCSForbiddenException Missing permissions to get direct link
|
|
|
|
|
*
|
|
|
|
|
* 200: Direct link returned
|
2018-04-13 10:54:24 -04:00
|
|
|
*/
|
2024-07-25 07:14:45 -04:00
|
|
|
#[NoAdminRequired]
|
2020-09-23 17:57:57 -04:00
|
|
|
public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
|
2018-04-13 10:54:24 -04:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
|
|
|
|
|
2024-02-09 03:54:52 -05:00
|
|
|
$file = $userFolder->getFirstNodeById($fileId);
|
2018-04-13 10:54:24 -04:00
|
|
|
|
2024-02-09 03:54:52 -05:00
|
|
|
if (!$file) {
|
2018-04-13 10:54:24 -04:00
|
|
|
throw new OCSNotFoundException();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 17:47:13 -04:00
|
|
|
if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
|
|
|
|
|
throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 04:34:57 -04:00
|
|
|
if (!($file instanceof File)) {
|
|
|
|
|
throw new OCSBadRequestException('Direct download only works for files');
|
|
|
|
|
}
|
2018-04-13 10:54:24 -04:00
|
|
|
|
2022-07-15 11:11:54 -04:00
|
|
|
$event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath()));
|
|
|
|
|
$this->eventDispatcher->dispatchTyped($event);
|
2022-06-13 09:50:43 -04:00
|
|
|
|
2022-07-15 11:11:54 -04:00
|
|
|
if ($event->isSuccessful() === false) {
|
2022-06-13 09:50:43 -04:00
|
|
|
throw new OCSForbiddenException('Permission denied to download file');
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 04:34:57 -04:00
|
|
|
//TODO: at some point we should use the directdownlaod function of storages
|
|
|
|
|
$direct = new Direct();
|
|
|
|
|
$direct->setUserId($this->userId);
|
|
|
|
|
$direct->setFileId($fileId);
|
2018-04-13 10:54:24 -04:00
|
|
|
|
2021-07-07 11:52:46 -04:00
|
|
|
$token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
|
2018-04-26 04:34:57 -04:00
|
|
|
$direct->setToken($token);
|
2020-09-23 17:57:57 -04:00
|
|
|
$direct->setExpiration($this->timeFactory->getTime() + $expirationTime);
|
2018-04-13 10:54:24 -04:00
|
|
|
|
2018-04-26 04:34:57 -04:00
|
|
|
$this->mapper->insert($direct);
|
2018-04-13 10:54:24 -04:00
|
|
|
|
2024-09-19 05:10:31 -04:00
|
|
|
$url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/' . $token);
|
2018-04-13 08:04:41 -04:00
|
|
|
|
2018-04-13 10:54:24 -04:00
|
|
|
return new DataResponse([
|
2018-04-13 11:40:52 -04:00
|
|
|
'url' => $url,
|
2018-04-13 10:54:24 -04:00
|
|
|
]);
|
2018-04-13 08:04:41 -04:00
|
|
|
}
|
|
|
|
|
}
|