2023-03-22 19:54:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 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;
|
|
|
|
|
|
|
|
use OCA\Memories\Db\TimelineRoot;
|
|
|
|
use OCA\Memories\Errors;
|
|
|
|
use OCA\Memories\HttpResponseException;
|
|
|
|
use OCP\AppFramework\Http;
|
2023-03-22 23:44:51 +00:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2023-03-22 19:54:03 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
|
|
|
|
abstract class GenericClusterController extends GenericApiController
|
|
|
|
{
|
|
|
|
protected ?TimelineRoot $root;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Get list of clusters
|
|
|
|
*/
|
|
|
|
public function list(): Http\Response
|
|
|
|
{
|
2023-03-23 00:01:41 +00:00
|
|
|
return $this->guardEx(function () {
|
2023-03-22 19:54:03 +00:00
|
|
|
$this->init();
|
|
|
|
|
|
|
|
$list = $this->getClusters();
|
2023-03-23 00:42:30 +00:00
|
|
|
|
2023-03-22 19:54:03 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
2023-03-23 00:01:41 +00:00
|
|
|
});
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Get preview for a cluster
|
|
|
|
*/
|
|
|
|
public function preview(string $name): Http\Response
|
|
|
|
{
|
2023-03-23 00:01:41 +00:00
|
|
|
return $this->guardEx(function () use ($name) {
|
2023-03-22 19:54:03 +00:00
|
|
|
$this->init();
|
|
|
|
|
2023-03-22 23:55:20 +00:00
|
|
|
// Get list of some photos in this cluster
|
|
|
|
$photos = $this->getPhotos($name, 8);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
2023-03-22 23:55:20 +00:00
|
|
|
// If no photos found then return 404
|
|
|
|
if (0 === \count($photos)) {
|
2023-03-22 19:54:03 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2023-03-22 23:55:20 +00:00
|
|
|
// Put the photos in the correct order
|
|
|
|
$this->sortPhotosForPreview($photos);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
|
|
|
// Get preview from image list
|
2023-03-22 23:55:20 +00:00
|
|
|
return $this->getPreviewFromPhotoList($photos);
|
2023-03-23 00:01:41 +00:00
|
|
|
});
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* Download a cluster as a zip file
|
|
|
|
*/
|
|
|
|
public function download(string $name): Http\Response
|
|
|
|
{
|
2023-03-23 00:01:41 +00:00
|
|
|
return $this->guardEx(function () use ($name) {
|
2023-03-22 19:54:03 +00:00
|
|
|
$this->init();
|
|
|
|
|
|
|
|
// Get list of all files in this cluster
|
2023-03-22 23:55:20 +00:00
|
|
|
$photos = $this->getPhotos($name);
|
|
|
|
$fileIds = array_map([$this, 'getFileId'], $photos);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
|
|
|
// Get download handle
|
|
|
|
$filename = $this->clusterName($name);
|
|
|
|
$handle = \OCA\Memories\Controller\DownloadController::createHandle($filename, $fileIds);
|
|
|
|
|
|
|
|
return new JSONResponse(['handle' => $handle], Http::STATUS_OK);
|
2023-03-23 00:01:41 +00:00
|
|
|
});
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A human-readable name for the app.
|
|
|
|
* Used for error messages.
|
|
|
|
*/
|
|
|
|
abstract protected function appName(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the app is enabled for the current user.
|
|
|
|
*/
|
|
|
|
abstract protected function isEnabled(): bool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cluster list for the current user.
|
|
|
|
*/
|
|
|
|
abstract protected function getClusters(): array;
|
|
|
|
|
|
|
|
/**
|
2023-03-22 23:55:20 +00:00
|
|
|
* Get a list of photos with any extra parameters for the given cluster
|
2023-03-22 23:19:59 +00:00
|
|
|
* Used for preview generation and download.
|
2023-03-22 19:54:03 +00:00
|
|
|
*
|
|
|
|
* @param string $name Identifier for the cluster
|
2023-03-22 23:55:20 +00:00
|
|
|
* @param int $limit Maximum number of photos to return
|
2023-03-22 19:54:03 +00:00
|
|
|
*/
|
2023-03-22 23:55:20 +00:00
|
|
|
abstract protected function getPhotos(string $name, ?int $limit = null): array;
|
2023-03-22 19:54:03 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-22 23:44:51 +00:00
|
|
|
* Human readable name for the cluster.
|
2023-03-22 19:54:03 +00:00
|
|
|
*/
|
2023-03-22 23:44:51 +00:00
|
|
|
protected function clusterName(string $name)
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-22 23:44:51 +00:00
|
|
|
return $name;
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-22 23:55:20 +00:00
|
|
|
* Put the photo objects in priority list.
|
2023-03-22 23:44:51 +00:00
|
|
|
* Works on the array in place.
|
2023-03-22 19:54:03 +00:00
|
|
|
*/
|
2023-03-22 23:55:20 +00:00
|
|
|
protected function sortPhotosForPreview(array &$photos)
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-22 23:55:20 +00:00
|
|
|
shuffle($photos);
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-22 23:44:51 +00:00
|
|
|
* Quality to use for the preview file.
|
2023-03-22 19:54:03 +00:00
|
|
|
*/
|
2023-03-22 23:44:51 +00:00
|
|
|
protected function getPreviewQuality(): int
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-22 23:44:51 +00:00
|
|
|
return 512;
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|
2023-03-22 23:19:59 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-22 23:44:51 +00:00
|
|
|
* Perform any post processing and get the blob from the preview file.
|
|
|
|
*
|
2023-03-22 23:55:20 +00:00
|
|
|
* @param \OCP\Files\SimpleFS\ISimpleFile $file Preview file
|
|
|
|
* @param array $photo Photo object
|
2023-03-22 23:44:51 +00:00
|
|
|
*
|
|
|
|
* @return [Blob, mimetype] of data
|
2023-03-22 23:19:59 +00:00
|
|
|
*/
|
2023-03-22 23:55:20 +00:00
|
|
|
protected function getPreviewBlob($file, $photo): array
|
2023-03-22 23:19:59 +00:00
|
|
|
{
|
2023-03-22 23:44:51 +00:00
|
|
|
return [$file->getContent(), $file->getMimeType()];
|
2023-03-22 23:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-22 23:55:20 +00:00
|
|
|
* Get the file ID for a photo object.
|
2023-03-22 23:19:59 +00:00
|
|
|
*/
|
2023-03-22 23:55:20 +00:00
|
|
|
protected function getFileId(array $photo): int
|
2023-03-22 23:19:59 +00:00
|
|
|
{
|
2023-03-22 23:55:20 +00:00
|
|
|
return (int) $photo['fileid'];
|
2023-03-22 23:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-22 23:44:51 +00:00
|
|
|
* Should the timeline root be queried?
|
|
|
|
*/
|
|
|
|
protected function useTimelineRoot(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize and check if the app is enabled.
|
|
|
|
* Gets the root node if required.
|
2023-03-22 23:19:59 +00:00
|
|
|
*/
|
2023-03-22 23:44:51 +00:00
|
|
|
protected function init(): void
|
2023-03-22 23:19:59 +00:00
|
|
|
{
|
2023-03-22 23:44:51 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
|
|
|
throw new HttpResponseException(Errors::NotLoggedIn());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isEnabled()) {
|
|
|
|
throw new HttpResponseException(Errors::NotEnabled($this->appName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->root = null;
|
|
|
|
if ($this->useTimelineRoot()) {
|
|
|
|
$this->root = $this->getRequestRoot();
|
|
|
|
if ($this->root->isEmpty()) {
|
|
|
|
throw new HttpResponseException(Errors::NoRequestRoot());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-22 23:55:20 +00:00
|
|
|
* Given a list of photo objects, return the first preview image possible.
|
2023-03-22 23:44:51 +00:00
|
|
|
*/
|
2023-03-22 23:55:20 +00:00
|
|
|
private function getPreviewFromPhotoList(array $photos): Http\Response
|
2023-03-22 23:44:51 +00:00
|
|
|
{
|
|
|
|
// Get preview manager
|
|
|
|
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
|
|
|
|
|
|
|
// Try to get a preview
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->getUID());
|
2023-03-22 23:55:20 +00:00
|
|
|
foreach ($photos as $img) {
|
2023-03-22 23:44:51 +00:00
|
|
|
// Get the file
|
|
|
|
$files = $userFolder->getById($this->getFileId($img));
|
|
|
|
if (0 === \count($files)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check read permission
|
|
|
|
if (!$files[0]->isReadable()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get preview image
|
|
|
|
try {
|
|
|
|
$quality = $this->getPreviewQuality();
|
|
|
|
$file = $previewManager->getPreview($files[0], $quality, $quality, false);
|
|
|
|
|
|
|
|
[$blob, $mimetype] = $this->getPreviewBlob($file, $img);
|
|
|
|
|
|
|
|
$response = new DataDisplayResponse($blob, Http::STATUS_OK, [
|
|
|
|
'Content-Type' => $mimetype,
|
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 23:55:20 +00:00
|
|
|
return Errors::NotFound('preview from photos list');
|
2023-03-22 23:19:59 +00:00
|
|
|
}
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|