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\Direct;
|
|
|
|
|
|
2018-04-13 11:11:25 -04:00
|
|
|
use OCA\DAV\Db\Direct;
|
2021-02-10 07:53:05 -05:00
|
|
|
use OCA\DAV\Events\BeforeFileDirectDownloadedEvent;
|
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2018-04-13 08:04:41 -04:00
|
|
|
use OCP\Files\File;
|
2018-04-13 11:11:25 -04:00
|
|
|
use OCP\Files\IRootFolder;
|
2018-04-13 08:04:41 -04:00
|
|
|
use Sabre\DAV\Exception\Forbidden;
|
2018-04-13 11:11:25 -04:00
|
|
|
use Sabre\DAV\Exception\NotFound;
|
2018-04-13 08:04:41 -04:00
|
|
|
use Sabre\DAV\IFile;
|
|
|
|
|
|
|
|
|
|
class DirectFile implements IFile {
|
|
|
|
|
/** @var File */
|
|
|
|
|
private $file;
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private Direct $direct,
|
|
|
|
|
private IRootFolder $rootFolder,
|
|
|
|
|
private IEventDispatcher $eventDispatcher,
|
|
|
|
|
) {
|
2018-04-13 08:04:41 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function put($data) {
|
2018-04-13 08:04:41 -04:00
|
|
|
throw new Forbidden();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function get() {
|
2018-04-13 11:11:25 -04:00
|
|
|
$this->getFile();
|
|
|
|
|
|
2021-02-10 07:53:05 -05:00
|
|
|
$this->eventDispatcher->dispatchTyped(new BeforeFileDirectDownloadedEvent($this->file));
|
|
|
|
|
|
2018-04-13 11:11:25 -04:00
|
|
|
return $this->file->fopen('rb');
|
2018-04-13 08:04:41 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function getContentType() {
|
2018-04-13 11:11:25 -04:00
|
|
|
$this->getFile();
|
|
|
|
|
|
|
|
|
|
return $this->file->getMimeType();
|
2018-04-13 08:04:41 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function getETag() {
|
2018-04-13 11:11:25 -04:00
|
|
|
$this->getFile();
|
|
|
|
|
|
2018-04-13 08:04:41 -04:00
|
|
|
return $this->file->getEtag();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 09:43:39 -05:00
|
|
|
/**
|
|
|
|
|
* @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
|
|
|
|
|
* @return int|float
|
|
|
|
|
*/
|
2018-04-23 16:15:29 -04:00
|
|
|
public function getSize() {
|
2018-04-13 11:11:25 -04:00
|
|
|
$this->getFile();
|
|
|
|
|
|
2018-04-13 08:04:41 -04:00
|
|
|
return $this->file->getSize();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function delete() {
|
2018-04-13 08:04:41 -04:00
|
|
|
throw new Forbidden();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function getName() {
|
2018-04-13 11:11:25 -04:00
|
|
|
return $this->direct->getToken();
|
2018-04-13 08:04:41 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function setName($name) {
|
2018-04-13 08:04:41 -04:00
|
|
|
throw new Forbidden();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 16:15:29 -04:00
|
|
|
public function getLastModified() {
|
2018-04-13 11:11:25 -04:00
|
|
|
$this->getFile();
|
|
|
|
|
|
2018-04-13 08:04:41 -04:00
|
|
|
return $this->file->getMTime();
|
2018-04-13 11:11:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getFile() {
|
|
|
|
|
if ($this->file === null) {
|
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
|
2024-02-09 03:54:52 -05:00
|
|
|
$file = $userFolder->getFirstNodeById($this->direct->getFileId());
|
2018-04-13 11:11:25 -04:00
|
|
|
|
2024-02-09 03:54:52 -05:00
|
|
|
if (!$file) {
|
2018-04-13 11:11:25 -04:00
|
|
|
throw new NotFound();
|
|
|
|
|
}
|
2024-02-09 03:54:52 -05:00
|
|
|
if (!$file instanceof File) {
|
2024-08-23 09:10:27 -04:00
|
|
|
throw new Forbidden('direct download not allowed on directories');
|
2024-02-09 03:54:52 -05:00
|
|
|
}
|
2018-04-13 11:11:25 -04:00
|
|
|
|
2024-02-09 03:54:52 -05:00
|
|
|
$this->file = $file;
|
2018-04-13 11:11:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->file;
|
2018-04-13 08:04:41 -04:00
|
|
|
}
|
|
|
|
|
}
|