2016-10-17 02:42:01 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-05-23 08:25:51 -04:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2016-10-17 02:42:01 -04:00
|
|
|
/**
|
2024-06-02 09:26:54 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-10-17 02:42:01 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_Trashbin\Controller;
|
|
|
|
|
|
2018-09-19 13:00:58 -04:00
|
|
|
use OCA\Files_Trashbin\Trash\ITrashManager;
|
2016-10-17 02:42:01 -04:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
|
use OCP\AppFramework\Http;
|
2024-07-25 07:14:46 -04:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
2024-12-17 07:17:22 -05:00
|
|
|
use OCP\AppFramework\Http\Attribute\OpenAPI;
|
2016-10-17 02:42:01 -04:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2018-05-23 08:25:51 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-10-17 02:42:01 -04:00
|
|
|
use OCP\Files\Folder;
|
|
|
|
|
use OCP\Files\IMimeTypeDetector;
|
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
|
use OCP\IPreview;
|
|
|
|
|
use OCP\IRequest;
|
2018-09-19 13:00:58 -04:00
|
|
|
use OCP\IUserSession;
|
2016-10-17 02:42:01 -04:00
|
|
|
|
2024-12-17 07:17:22 -05:00
|
|
|
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
|
2016-10-17 02:42:01 -04:00
|
|
|
class PreviewController extends Controller {
|
2018-09-19 13:00:58 -04:00
|
|
|
public function __construct(
|
|
|
|
|
string $appName,
|
|
|
|
|
IRequest $request,
|
2024-10-18 06:04:22 -04:00
|
|
|
private IRootFolder $rootFolder,
|
|
|
|
|
private ITrashManager $trashManager,
|
|
|
|
|
private IUserSession $userSession,
|
|
|
|
|
private IMimeTypeDetector $mimeTypeDetector,
|
|
|
|
|
private IPreview $previewManager,
|
|
|
|
|
private ITimeFactory $time,
|
2018-09-19 13:00:58 -04:00
|
|
|
) {
|
2016-10-17 02:42:01 -04:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-14 10:36:05 -04:00
|
|
|
* Get the preview for a file
|
|
|
|
|
*
|
|
|
|
|
* @param int $fileId ID of the file
|
|
|
|
|
* @param int $x Width of the preview
|
|
|
|
|
* @param int $y Height of the preview
|
|
|
|
|
* @param bool $a Whether to not crop the preview
|
|
|
|
|
*
|
2024-09-24 09:53:13 -04:00
|
|
|
* @return Http\FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, list<empty>, array{}>
|
2023-06-14 10:36:05 -04:00
|
|
|
*
|
|
|
|
|
* 200: Preview returned
|
|
|
|
|
* 400: Getting preview is not possible
|
|
|
|
|
* 404: Preview not found
|
2016-10-17 02:42:01 -04:00
|
|
|
*/
|
2024-07-25 07:14:46 -04:00
|
|
|
#[NoAdminRequired]
|
|
|
|
|
#[NoCSRFRequired]
|
2016-10-17 02:42:01 -04:00
|
|
|
public function getPreview(
|
2020-01-07 17:24:06 -05:00
|
|
|
int $fileId = -1,
|
2023-02-05 05:22:50 -05:00
|
|
|
int $x = 32,
|
|
|
|
|
int $y = 32,
|
|
|
|
|
bool $a = false,
|
2016-10-17 02:42:01 -04:00
|
|
|
) {
|
2020-01-07 17:24:06 -05:00
|
|
|
if ($fileId === -1 || $x === 0 || $y === 0) {
|
2016-10-17 02:42:01 -04:00
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2018-09-19 13:00:58 -04:00
|
|
|
$file = $this->trashManager->getTrashNodeById($this->userSession->getUser(), $fileId);
|
2018-10-08 10:42:22 -04:00
|
|
|
if ($file === null) {
|
2018-09-19 13:00:58 -04:00
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
2016-10-17 02:42:01 -04:00
|
|
|
}
|
2018-10-08 10:42:22 -04:00
|
|
|
if ($file instanceof Folder) {
|
|
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
}
|
2016-10-17 02:42:01 -04:00
|
|
|
|
2018-09-19 13:00:58 -04:00
|
|
|
$pathParts = pathinfo($file->getName());
|
2019-08-06 04:55:13 -04:00
|
|
|
$extension = $pathParts['extension'] ?? '';
|
2018-09-19 13:00:58 -04:00
|
|
|
$fileName = $pathParts['filename'];
|
2018-05-23 08:25:51 -04:00
|
|
|
/*
|
|
|
|
|
* Files in the root of the trashbin are timetamped.
|
|
|
|
|
* So we have to strip that in order to properly detect the mimetype of the file.
|
|
|
|
|
*/
|
2018-09-19 13:00:58 -04:00
|
|
|
if (preg_match('/d\d+/', $extension)) {
|
2018-05-23 08:25:51 -04:00
|
|
|
$mimeType = $this->mimeTypeDetector->detectPath($fileName);
|
|
|
|
|
} else {
|
2018-09-19 13:00:58 -04:00
|
|
|
$mimeType = $this->mimeTypeDetector->detectPath($file->getName());
|
2018-05-23 08:25:51 -04:00
|
|
|
}
|
2016-10-17 02:42:01 -04:00
|
|
|
|
2023-04-11 05:24:09 -04:00
|
|
|
$f = $this->previewManager->getPreview($file, $x, $y, !$a, IPreview::MODE_FILL, $mimeType);
|
2024-10-10 06:40:31 -04:00
|
|
|
$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
|
2018-05-23 08:25:51 -04:00
|
|
|
|
|
|
|
|
// Cache previews for 24H
|
|
|
|
|
$response->cacheFor(3600 * 24);
|
|
|
|
|
return $response;
|
2016-10-17 02:42:01 -04:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
2017-05-01 17:41:37 -04:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
2016-10-17 02:42:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|