2022-10-29 18:05:05 +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\AppInfo\Application;
|
|
|
|
use OCA\Memories\Db\TimelineQuery;
|
2022-11-16 07:45:01 +00:00
|
|
|
use OCA\Memories\Db\TimelineRoot;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCA\Memories\Exif;
|
|
|
|
use OCP\App\IAppManager;
|
|
|
|
use OCP\AppFramework\Controller;
|
2023-02-05 21:43:25 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2022-11-10 06:19:44 +00:00
|
|
|
use OCP\Files\File;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2023-02-24 08:21:38 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
class ApiBase extends Controller
|
|
|
|
{
|
|
|
|
protected IConfig $config;
|
|
|
|
protected IUserSession $userSession;
|
|
|
|
protected IRootFolder $rootFolder;
|
|
|
|
protected IAppManager $appManager;
|
|
|
|
protected TimelineQuery $timelineQuery;
|
2023-01-18 03:02:00 +00:00
|
|
|
protected IDBConnection $connection;
|
2023-02-24 08:21:38 +00:00
|
|
|
protected LoggerInterface $logger;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
IConfig $config,
|
|
|
|
IUserSession $userSession,
|
|
|
|
IDBConnection $connection,
|
|
|
|
IRootFolder $rootFolder,
|
2023-02-24 08:21:38 +00:00
|
|
|
IAppManager $appManager,
|
|
|
|
LoggerInterface $logger
|
2022-10-29 18:05:05 +00:00
|
|
|
) {
|
|
|
|
parent::__construct(Application::APPNAME, $request);
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->rootFolder = $rootFolder;
|
|
|
|
$this->appManager = $appManager;
|
2023-02-24 08:21:38 +00:00
|
|
|
$this->logger = $logger;
|
2022-10-29 18:05:05 +00:00
|
|
|
$this->timelineQuery = new TimelineQuery($connection);
|
|
|
|
}
|
|
|
|
|
2023-01-18 03:02:00 +00:00
|
|
|
/** Get logged in user's UID or throw exception */
|
2022-12-04 17:22:59 +00:00
|
|
|
protected function getUID(): string
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($this->getShareToken()) {
|
|
|
|
$user = null;
|
|
|
|
} elseif (null === $user) {
|
2023-01-18 03:02:00 +00:00
|
|
|
throw new \Exception('User not logged in');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $user ? $user->getUID() : '';
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:45:01 +00:00
|
|
|
/** Get the TimelineRoot object relevant to the request */
|
|
|
|
protected function getRequestRoot()
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-01-18 03:02:00 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = new TimelineRoot();
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
// Albums have no folder
|
2023-01-18 03:02:00 +00:00
|
|
|
if ($this->albumsIsEnabled() && $this->request->getParam('album')) {
|
|
|
|
if (null !== $user) {
|
|
|
|
return $root;
|
|
|
|
}
|
|
|
|
if (($token = $this->getShareToken()) && $this->timelineQuery->getAlbumByLink($token)) {
|
|
|
|
return $root;
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Public shared folder
|
2022-12-03 04:21:34 +00:00
|
|
|
if ($share = $this->getShareNode()) { // can throw
|
2022-11-16 07:45:01 +00:00
|
|
|
$root->addFolder($share);
|
|
|
|
|
|
|
|
return $root;
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Anything else needs a user
|
|
|
|
if (null === $user) {
|
2022-11-16 08:16:01 +00:00
|
|
|
throw new \Exception('User not logged in');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
$uid = $user->getUID();
|
|
|
|
|
|
|
|
$folder = null;
|
|
|
|
$folderPath = $this->request->getParam('folder');
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($uid);
|
|
|
|
|
2022-11-16 08:16:01 +00:00
|
|
|
try {
|
|
|
|
if (null !== $folderPath) {
|
|
|
|
$folder = $userFolder->get(Exif::removeExtraSlash($folderPath));
|
|
|
|
$root->addFolder($folder);
|
|
|
|
} else {
|
|
|
|
$timelinePath = $this->request->getParam('timelinePath', Exif::getPhotosPath($this->config, $uid));
|
|
|
|
$timelinePath = Exif::removeExtraSlash($timelinePath);
|
|
|
|
|
|
|
|
// Multiple timeline path support
|
|
|
|
$paths = explode(';', $timelinePath);
|
|
|
|
foreach ($paths as &$path) {
|
|
|
|
$folder = $userFolder->get(trim($path));
|
|
|
|
$root->addFolder($folder);
|
|
|
|
}
|
|
|
|
$root->addMountPoints();
|
|
|
|
}
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
$msg = $e->getMessage();
|
2022-11-15 13:40:46 +00:00
|
|
|
|
2022-11-16 08:16:01 +00:00
|
|
|
throw new \Exception("Folder not found: {$msg}");
|
2022-11-16 07:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $root;
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 04:07:06 +00:00
|
|
|
/**
|
|
|
|
* Get a file with ID for the current user.
|
|
|
|
*/
|
|
|
|
protected function getUserFile(int $fileId): ?File
|
|
|
|
{
|
2022-12-03 05:11:09 +00:00
|
|
|
// Don't check self for share token
|
|
|
|
if ($this->getShareToken()) {
|
|
|
|
return $this->getShareFile($fileId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check both user folder and album
|
2022-12-03 04:07:06 +00:00
|
|
|
return $this->getUserFolderFile($fileId) ??
|
2023-01-18 03:02:00 +00:00
|
|
|
$this->getAlbumFile($fileId);
|
2022-12-03 04:07:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:34:58 +00:00
|
|
|
/**
|
2022-11-10 06:43:58 +00:00
|
|
|
* Get a file with ID from user's folder.
|
2022-11-10 06:34:58 +00:00
|
|
|
*/
|
2022-12-03 04:07:06 +00:00
|
|
|
protected function getUserFolderFile(int $id): ?File
|
2022-11-10 06:19:44 +00:00
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
|
|
|
|
|
2022-12-03 04:07:06 +00:00
|
|
|
return $this->getOneFileFromFolder($userFolder, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a file with ID from an album.
|
|
|
|
*/
|
|
|
|
protected function getAlbumFile(int $id): ?File
|
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
2022-11-10 06:19:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-03 04:07:06 +00:00
|
|
|
$uid = $user->getUID();
|
2022-11-10 06:19:44 +00:00
|
|
|
|
2022-12-03 05:42:52 +00:00
|
|
|
$owner = $this->timelineQuery->albumHasUserFile($uid, $id);
|
|
|
|
if (!$owner) {
|
2022-11-10 06:34:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-03 05:42:52 +00:00
|
|
|
$folder = $this->rootFolder->getUserFolder($owner);
|
|
|
|
|
|
|
|
return $this->getOneFileFromFolder($folder, $id);
|
2022-12-03 04:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a file with ID from a public share.
|
|
|
|
*
|
|
|
|
* @param int $fileId
|
|
|
|
*/
|
|
|
|
protected function getShareFile(int $id): ?File
|
|
|
|
{
|
|
|
|
try {
|
2023-01-18 03:02:00 +00:00
|
|
|
// Album share
|
|
|
|
if ($this->request->getParam('album')) {
|
|
|
|
$album = $this->timelineQuery->getAlbumByLink($this->getShareToken());
|
|
|
|
if (null === $album) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-18 19:32:04 +00:00
|
|
|
$owner = $this->timelineQuery->albumHasFile((int) $album['album_id'], $id);
|
2023-01-18 03:02:00 +00:00
|
|
|
if (!$owner) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$folder = $this->rootFolder->getUserFolder($owner);
|
|
|
|
|
|
|
|
return $this->getOneFileFromFolder($folder, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Folder share
|
2022-12-03 04:21:34 +00:00
|
|
|
if ($share = $this->getShareNode()) {
|
|
|
|
return $this->getOneFileFromFolder($share, $id);
|
2022-12-03 04:07:06 +00:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:21:34 +00:00
|
|
|
return null;
|
2022-11-10 06:19:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 22:39:48 +00:00
|
|
|
protected function isRecursive()
|
|
|
|
{
|
2023-01-18 05:18:27 +00:00
|
|
|
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
|
2022-11-03 22:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function isArchive()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('archive');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isMonthView()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('monthView');
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:44:52 +00:00
|
|
|
protected function isReverse()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('reverse');
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
protected function getShareToken()
|
|
|
|
{
|
2023-01-18 03:02:00 +00:00
|
|
|
return $this->request->getParam('token');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 04:29:34 +00:00
|
|
|
protected function getShareObject()
|
2022-12-03 04:21:34 +00:00
|
|
|
{
|
2022-12-03 04:29:34 +00:00
|
|
|
// Get token from request
|
2022-12-03 04:21:34 +00:00
|
|
|
$token = $this->getShareToken();
|
|
|
|
if (null === $token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:29:34 +00:00
|
|
|
// Get share by token
|
2022-12-04 17:57:31 +00:00
|
|
|
$share = \OC::$server->get(\OCP\Share\IManager::class)->getShareByToken($token);
|
2022-12-04 17:20:46 +00:00
|
|
|
if (!PublicController::validateShare($share)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-12-03 04:29:34 +00:00
|
|
|
|
|
|
|
// Check if share is password protected
|
|
|
|
if (($password = $share->getPassword()) !== null) {
|
2022-12-04 17:57:31 +00:00
|
|
|
$session = \OC::$server->get(\OCP\ISession::class);
|
2022-12-04 17:40:58 +00:00
|
|
|
|
2022-12-03 04:29:34 +00:00
|
|
|
// https://github.com/nextcloud/server/blob/0447b53bda9fe95ea0cbed765aa332584605d652/lib/public/AppFramework/PublicShareController.php#L119
|
2023-01-18 03:02:00 +00:00
|
|
|
if (
|
|
|
|
$session->get('public_link_authenticated_token') !== $token
|
|
|
|
|| $session->get('public_link_authenticated_password_hash') !== $password
|
|
|
|
) {
|
2022-12-03 04:42:39 +00:00
|
|
|
throw new \Exception('Share is password protected and user is not authenticated');
|
2022-12-03 04:29:34 +00:00
|
|
|
}
|
2022-12-03 04:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:29:34 +00:00
|
|
|
protected function getShareNode()
|
|
|
|
{
|
|
|
|
$share = $this->getShareObject();
|
|
|
|
if (null === $share) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get node from share
|
|
|
|
$node = $share->getNode(); // throws exception if not found
|
|
|
|
if (!$node instanceof Folder || !$node->isReadable() || !$node->isShareable()) {
|
|
|
|
throw new \Exception('Share not found or invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:43:25 +00:00
|
|
|
/**
|
|
|
|
* Given a list of file ids, return the first preview image possible.
|
|
|
|
*/
|
2023-02-20 16:12:16 +00:00
|
|
|
protected function getPreviewFromImageList(array $list, int $quality = 512)
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
|
|
|
// Get preview manager
|
|
|
|
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
|
|
|
|
|
|
|
// Try to get a preview
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->getUID());
|
|
|
|
foreach ($list as &$img) {
|
|
|
|
// Get the file
|
|
|
|
$files = $userFolder->getById($img);
|
|
|
|
if (0 === \count($files)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check read permission
|
|
|
|
if (!($files[0]->getPermissions() & \OCP\Constants::PERMISSION_READ)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get preview image
|
|
|
|
try {
|
2023-02-09 05:55:12 +00:00
|
|
|
$preview = $previewManager->getPreview($files[0], $quality, $quality, false);
|
2023-02-05 21:43:25 +00:00
|
|
|
$response = new DataDisplayResponse($preview->getContent(), Http::STATUS_OK, [
|
|
|
|
'Content-Type' => $preview->getMimeType(),
|
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
/**
|
|
|
|
* Check if albums are enabled for this user.
|
|
|
|
*/
|
|
|
|
protected function albumsIsEnabled(): bool
|
|
|
|
{
|
|
|
|
return \OCA\Memories\Util::albumsIsEnabled($this->appManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if tags is enabled for this user.
|
|
|
|
*/
|
|
|
|
protected function tagsIsEnabled(): bool
|
|
|
|
{
|
|
|
|
return \OCA\Memories\Util::tagsIsEnabled($this->appManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if recognize is enabled for this user.
|
|
|
|
*/
|
|
|
|
protected function recognizeIsEnabled(): bool
|
|
|
|
{
|
|
|
|
return \OCA\Memories\Util::recognizeIsEnabled($this->appManager);
|
|
|
|
}
|
2022-12-03 04:07:06 +00:00
|
|
|
|
2022-12-08 21:00:53 +00:00
|
|
|
// Check if facerecognition is installed and enabled for this user.
|
|
|
|
protected function facerecognitionIsInstalled(): bool
|
|
|
|
{
|
|
|
|
return \OCA\Memories\Util::facerecognitionIsInstalled($this->appManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if facerecognition is enabled for this user.
|
|
|
|
*/
|
|
|
|
protected function facerecognitionIsEnabled(): bool
|
|
|
|
{
|
|
|
|
return \OCA\Memories\Util::facerecognitionIsEnabled($this->config, $this->getUID());
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:43:25 +00:00
|
|
|
/**
|
|
|
|
* Check if geolocation is enabled for this user.
|
|
|
|
*/
|
2023-02-06 03:46:44 +00:00
|
|
|
protected function placesIsEnabled(): bool
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
2023-02-06 03:55:39 +00:00
|
|
|
return \OCA\Memories\Util::placesGISType() > 0;
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 04:07:06 +00:00
|
|
|
/**
|
|
|
|
* Helper to get one file or null from a fiolder.
|
|
|
|
*/
|
|
|
|
private function getOneFileFromFolder(Folder $folder, int $id): ?File
|
|
|
|
{
|
|
|
|
// Check for permissions and get numeric Id
|
|
|
|
$file = $folder->getById($id);
|
|
|
|
if (0 === \count($file)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if node is a file
|
|
|
|
if (!$file[0] instanceof File) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check read permission
|
|
|
|
if (!($file[0]->getPermissions() & \OCP\Constants::PERMISSION_READ)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file[0];
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|