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;
|
|
|
|
|
2023-03-24 04:57:54 +00:00
|
|
|
use OCA\Memories\ClustersBackend;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
|
|
|
use OCA\Memories\Util;
|
2023-03-22 19:54:03 +00:00
|
|
|
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;
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
class ClustersController extends GenericApiController
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-23 18:08:36 +00:00
|
|
|
/** Current backend for this instance */
|
2023-03-24 04:57:54 +00:00
|
|
|
protected ClustersBackend\Backend $backend;
|
2023-03-22 19:54:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get list of clusters
|
|
|
|
*/
|
2023-03-23 18:08:36 +00:00
|
|
|
public function list(string $backend): Http\Response
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($backend) {
|
2023-03-23 18:08:36 +00:00
|
|
|
$this->init($backend);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
$list = $this->backend->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
|
|
|
|
*/
|
2023-03-23 18:08:36 +00:00
|
|
|
public function preview(string $backend, string $name): Http\Response
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($backend, $name) {
|
2023-03-23 18:08:36 +00:00
|
|
|
$this->init($backend);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
2023-03-22 23:55:20 +00:00
|
|
|
// Get list of some photos in this cluster
|
2023-03-23 18:08:36 +00:00
|
|
|
$photos = $this->backend->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
|
2023-03-23 18:08:36 +00:00
|
|
|
$this->backend->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
|
|
|
|
*/
|
2023-03-23 18:08:36 +00:00
|
|
|
public function download(string $backend, string $name): Http\Response
|
2023-03-22 19:54:03 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($backend, $name) {
|
2023-03-23 18:08:36 +00:00
|
|
|
$this->init($backend);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
|
|
|
// Get list of all files in this cluster
|
2023-03-23 18:08:36 +00:00
|
|
|
$photos = $this->backend->getPhotos($name);
|
|
|
|
$fileIds = array_map(fn ($p) => $this->backend->getFileId($p), $photos);
|
2023-03-22 19:54:03 +00:00
|
|
|
|
|
|
|
// Get download handle
|
2023-03-23 18:08:36 +00:00
|
|
|
$filename = $this->backend->clusterName($name);
|
2023-03-22 19:54:03 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2023-03-22 23:44:51 +00:00
|
|
|
/**
|
|
|
|
* Initialize and check if the app is enabled.
|
|
|
|
* Gets the root node if required.
|
2023-03-22 23:19:59 +00:00
|
|
|
*/
|
2023-03-23 18:08:36 +00:00
|
|
|
protected function init(string $backend): void
|
2023-03-22 23:19:59 +00:00
|
|
|
{
|
2023-03-24 04:16:53 +00:00
|
|
|
Util::getUser();
|
2023-03-22 23:44:51 +00:00
|
|
|
|
2023-03-24 04:57:54 +00:00
|
|
|
$this->backend = ClustersBackend\Manager::get($backend);
|
2023-03-23 18:08:36 +00:00
|
|
|
|
|
|
|
if (!$this->backend->isEnabled()) {
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::NotEnabled($this->backend->appName());
|
2023-03-22 23:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2023-03-22 23:55:20 +00:00
|
|
|
foreach ($photos as $img) {
|
2023-03-22 23:44:51 +00:00
|
|
|
// Get preview image
|
|
|
|
try {
|
2023-03-23 18:08:36 +00:00
|
|
|
$quality = $this->backend->getPreviewQuality();
|
2023-03-24 00:56:41 +00:00
|
|
|
|
|
|
|
$file = $this->fs->getUserFile($this->backend->getFileId($img));
|
|
|
|
$file = $previewManager->getPreview($file, $quality, $quality, false);
|
2023-03-22 23:44:51 +00:00
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
[$blob, $mimetype] = $this->backend->getPreviewBlob($file, $img);
|
2023-03-22 23:44:51 +00:00
|
|
|
|
|
|
|
$response = new DataDisplayResponse($blob, Http::STATUS_OK, [
|
|
|
|
'Content-Type' => $mimetype,
|
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::NotFound('preview from photos list');
|
2023-03-22 23:19:59 +00:00
|
|
|
}
|
2023-03-22 19:54:03 +00:00
|
|
|
}
|