2022-08-13 03:34:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-08-13 03:34:05 +00:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.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/>.
|
|
|
|
*/
|
|
|
|
|
2022-08-18 18:27:25 +00:00
|
|
|
namespace OCA\Memories\Controller;
|
2022-08-13 03:34:05 +00:00
|
|
|
|
2022-08-18 18:27:25 +00:00
|
|
|
use OCA\Memories\AppInfo\Application;
|
2022-08-20 02:53:21 +00:00
|
|
|
use OCA\Memories\Db\TimelineQuery;
|
2022-09-25 13:21:40 +00:00
|
|
|
use OCA\Memories\Db\TimelineWrite;
|
2022-09-24 01:54:14 +00:00
|
|
|
use OCA\Memories\Exif;
|
2022-10-07 20:21:46 +00:00
|
|
|
use OCP\App\IAppManager;
|
2022-08-13 03:34:05 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
2022-10-17 02:52:44 +00:00
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
2022-10-17 17:41:58 +00:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2022-10-19 17:10:36 +00:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\AppFramework\Http\StreamResponse;
|
2022-10-17 17:41:58 +00:00
|
|
|
use OCP\Files\FileInfo;
|
|
|
|
use OCP\Files\Folder;
|
2022-10-19 17:10:36 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
2022-08-13 03:34:05 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
2022-10-19 17:10:36 +00:00
|
|
|
use OCP\IPreview;
|
2022-08-13 03:34:05 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
class ApiController extends Controller
|
|
|
|
{
|
2022-09-09 07:31:42 +00:00
|
|
|
private IConfig $config;
|
|
|
|
private IUserSession $userSession;
|
2022-08-13 03:34:05 +00:00
|
|
|
private IDBConnection $connection;
|
2022-09-09 07:31:42 +00:00
|
|
|
private IRootFolder $rootFolder;
|
2022-10-07 20:21:46 +00:00
|
|
|
private IAppManager $appManager;
|
2022-09-09 07:31:42 +00:00
|
|
|
private TimelineQuery $timelineQuery;
|
2022-09-25 13:21:40 +00:00
|
|
|
private TimelineWrite $timelineWrite;
|
2022-10-25 00:47:25 +00:00
|
|
|
private IPreview $preview;
|
2022-08-13 03:34:05 +00:00
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
IConfig $config,
|
|
|
|
IUserSession $userSession,
|
2022-08-17 20:39:48 +00:00
|
|
|
IDBConnection $connection,
|
2022-10-07 20:21:46 +00:00
|
|
|
IRootFolder $rootFolder,
|
2022-10-17 17:41:58 +00:00
|
|
|
IAppManager $appManager,
|
2022-10-25 00:47:25 +00:00
|
|
|
IPreview $preview
|
2022-10-19 17:15:14 +00:00
|
|
|
) {
|
2022-09-09 07:31:42 +00:00
|
|
|
parent::__construct(Application::APPNAME, $request);
|
2022-08-13 03:34:05 +00:00
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
$this->config = $config;
|
|
|
|
$this->userSession = $userSession;
|
2022-08-13 03:34:05 +00:00
|
|
|
$this->connection = $connection;
|
2022-09-09 07:31:42 +00:00
|
|
|
$this->rootFolder = $rootFolder;
|
2022-10-07 20:21:46 +00:00
|
|
|
$this->appManager = $appManager;
|
2022-10-25 00:47:25 +00:00
|
|
|
$this->previewManager = $preview;
|
2022-09-12 01:33:38 +00:00
|
|
|
$this->timelineQuery = new TimelineQuery($this->connection);
|
2022-10-25 00:47:25 +00:00
|
|
|
$this->timelineWrite = new TimelineWrite($connection, $preview);
|
2022-09-12 01:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function days(): JSONResponse
|
|
|
|
{
|
2022-08-13 03:34:05 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-09-09 07:31:42 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
2022-09-24 01:54:14 +00:00
|
|
|
$uid = $user->getUID();
|
2022-08-13 03:34:05 +00:00
|
|
|
|
2022-09-24 01:54:14 +00:00
|
|
|
// Get the folder to show
|
|
|
|
$folder = $this->getRequestFolder();
|
2022-10-19 17:10:36 +00:00
|
|
|
$recursive = null === $this->request->getParam('folder');
|
|
|
|
$archive = null !== $this->request->getParam('archive');
|
|
|
|
if (null === $folder) {
|
|
|
|
return new JSONResponse(['message' => 'Folder not found'], Http::STATUS_NOT_FOUND);
|
2022-09-24 01:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
2022-10-08 06:46:08 +00:00
|
|
|
try {
|
|
|
|
$list = $this->timelineQuery->getDays(
|
|
|
|
$folder,
|
|
|
|
$uid,
|
|
|
|
$recursive,
|
|
|
|
$archive,
|
2022-10-24 16:21:38 +00:00
|
|
|
$this->getTransformations(true),
|
2022-10-08 06:46:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Preload some day responses
|
|
|
|
$this->preloadDays($list, $folder, $recursive, $archive);
|
|
|
|
|
|
|
|
// Add subfolder info if querying non-recursively
|
|
|
|
if (!$recursive) {
|
|
|
|
array_unshift($list, $this->getSubfoldersEntry($folder));
|
|
|
|
}
|
2022-09-24 01:54:14 +00:00
|
|
|
|
2022-10-08 06:46:08 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
} catch (\Exception $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-10-08 06:46:08 +00:00
|
|
|
}
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 20:37:12 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function dayPost(): JSONResponse
|
|
|
|
{
|
2022-10-06 20:37:12 +00:00
|
|
|
$id = $this->request->getParam('body_ids');
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $id) {
|
2022-10-06 20:37:12 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-10-06 20:37:12 +00:00
|
|
|
return $this->day($id);
|
|
|
|
}
|
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function day(string $id): JSONResponse
|
|
|
|
{
|
2022-08-14 23:31:47 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-09-09 07:31:42 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
2022-09-24 01:54:14 +00:00
|
|
|
$uid = $user->getUID();
|
|
|
|
|
2022-10-06 22:01:28 +00:00
|
|
|
// Check for wildcard
|
|
|
|
$day_ids = [];
|
2022-10-19 17:10:36 +00:00
|
|
|
if ('*' === $id) {
|
2022-10-06 22:01:28 +00:00
|
|
|
$day_ids = null;
|
|
|
|
} else {
|
|
|
|
// Split at commas and convert all parts to int
|
|
|
|
$day_ids = array_map(function ($part) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return (int) $part;
|
|
|
|
}, explode(',', $id));
|
2022-10-06 22:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if $day_ids is empty
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null !== $day_ids && 0 === \count($day_ids)) {
|
2022-10-06 22:01:28 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
}
|
2022-10-06 19:24:45 +00:00
|
|
|
|
2022-09-24 01:54:14 +00:00
|
|
|
// Get the folder to show
|
|
|
|
$folder = $this->getRequestFolder();
|
2022-10-19 17:10:36 +00:00
|
|
|
$recursive = null === $this->request->getParam('folder');
|
|
|
|
$archive = null !== $this->request->getParam('archive');
|
|
|
|
if (null === $folder) {
|
2022-09-24 01:54:14 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
2022-08-14 23:31:47 +00:00
|
|
|
|
2022-09-24 01:54:14 +00:00
|
|
|
// Run actual query
|
2022-10-08 06:46:08 +00:00
|
|
|
try {
|
|
|
|
$list = $this->timelineQuery->getDay(
|
|
|
|
$folder,
|
|
|
|
$uid,
|
|
|
|
$day_ids,
|
|
|
|
$recursive,
|
|
|
|
$archive,
|
2022-10-24 16:21:38 +00:00
|
|
|
$this->getTransformations(false),
|
2022-10-08 06:46:08 +00:00
|
|
|
);
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-10-08 06:46:08 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
} catch (\Exception $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-10-08 06:46:08 +00:00
|
|
|
}
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* Get subfolders entry for days response.
|
2022-09-09 07:31:42 +00:00
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function getSubfoldersEntry(Folder &$folder)
|
|
|
|
{
|
2022-09-25 09:46:01 +00:00
|
|
|
// Ugly: get the view of the folder with reflection
|
|
|
|
// This is unfortunately the only way to get the contents of a folder
|
|
|
|
// matching a MIME type without using SEARCH, which is deep
|
|
|
|
$rp = new \ReflectionProperty('\OC\Files\Node\Node', 'view');
|
|
|
|
$rp->setAccessible(true);
|
|
|
|
$view = $rp->getValue($folder);
|
|
|
|
|
|
|
|
// Get the subfolders
|
|
|
|
$folders = $view->getDirectoryContent($folder->getPath(), FileInfo::MIMETYPE_FOLDER, $folder);
|
2022-09-09 07:31:42 +00:00
|
|
|
|
|
|
|
// Sort by name
|
2022-10-19 17:10:36 +00:00
|
|
|
usort($folders, function ($a, $b) {
|
2022-09-09 07:31:42 +00:00
|
|
|
return strnatcmp($a->getName(), $b->getName());
|
|
|
|
});
|
|
|
|
|
2022-09-25 09:46:01 +00:00
|
|
|
// Process to response type
|
|
|
|
return [
|
2022-10-19 17:10:36 +00:00
|
|
|
'dayid' => \OCA\Memories\Util::$TAG_DAYID_FOLDERS,
|
|
|
|
'count' => \count($folders),
|
|
|
|
'detail' => array_map(function ($node) {
|
2022-09-09 07:31:42 +00:00
|
|
|
return [
|
2022-10-19 17:10:36 +00:00
|
|
|
'fileid' => $node->getId(),
|
|
|
|
'name' => $node->getName(),
|
|
|
|
'isfolder' => 1,
|
|
|
|
'path' => $node->getPath(),
|
2022-09-09 07:31:42 +00:00
|
|
|
];
|
2022-09-25 09:46:01 +00:00
|
|
|
}, $folders, []),
|
2022-09-09 07:31:42 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-10-06 21:37:18 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get list of tags with counts of images
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function tags(): JSONResponse
|
|
|
|
{
|
2022-10-06 21:37:18 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-10-06 21:37:18 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:21:46 +00:00
|
|
|
// Check tags enabled for this user
|
|
|
|
if (!$this->tagsIsEnabled()) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Tags not enabled for user'], Http::STATUS_PRECONDITION_FAILED);
|
2022-10-07 20:21:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 21:37:18 +00:00
|
|
|
// If this isn't the timeline folder then things aren't going to work
|
|
|
|
$folder = $this->getRequestFolder();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $folder) {
|
2022-10-06 21:37:18 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
$list = $this->timelineQuery->getTags(
|
|
|
|
$folder,
|
|
|
|
);
|
2022-10-08 00:57:48 +00:00
|
|
|
|
|
|
|
// Preload all tag previews
|
2022-10-26 16:29:32 +00:00
|
|
|
$this->timelineQuery->getTagPreviews($list, $folder);
|
2022-10-08 00:57:48 +00:00
|
|
|
|
2022-10-06 21:37:18 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
2022-10-07 19:28:39 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get list of faces with counts of images
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function faces(): JSONResponse
|
|
|
|
{
|
2022-10-07 19:28:39 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-10-07 19:28:39 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:21:46 +00:00
|
|
|
// Check faces enabled for this user
|
|
|
|
if (!$this->recognizeIsEnabled()) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Recognize app not enabled or not v3+'], Http::STATUS_PRECONDITION_FAILED);
|
2022-10-07 20:21:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 19:28:39 +00:00
|
|
|
// If this isn't the timeline folder then things aren't going to work
|
|
|
|
$folder = $this->getRequestFolder();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $folder) {
|
2022-10-07 19:28:39 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
$list = $this->timelineQuery->getFaces(
|
|
|
|
$folder,
|
|
|
|
);
|
2022-10-08 00:57:48 +00:00
|
|
|
|
2022-10-17 17:41:58 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
2022-10-08 02:00:55 +00:00
|
|
|
|
2022-10-17 17:41:58 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-10-17 17:41:58 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Get face preview image cropped with imagick
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-10-17 17:41:58 +00:00
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function facePreview(string $id): Http\Response
|
|
|
|
{
|
2022-10-17 17:41:58 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-10-17 17:41:58 +00:00
|
|
|
return new DataResponse([], Http::STATUS_PRECONDITION_FAILED);
|
2022-10-07 20:21:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:41:58 +00:00
|
|
|
// Check faces enabled for this user
|
|
|
|
if (!$this->recognizeIsEnabled()) {
|
|
|
|
return new DataResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get folder to search for
|
|
|
|
$folder = $this->getRequestFolder();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $folder) {
|
2022-10-17 17:41:58 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
2022-10-19 17:10:36 +00:00
|
|
|
$detections = $this->timelineQuery->getFacePreviewDetection($folder, (int) $id);
|
|
|
|
if (null === $detections || 0 === \count($detections)) {
|
2022-10-17 17:41:58 +00:00
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first detection that has a preview
|
|
|
|
$preview = null;
|
|
|
|
foreach ($detections as &$detection) {
|
|
|
|
// Get the file (also checks permissions)
|
2022-10-19 17:10:36 +00:00
|
|
|
$files = $folder->getById($detection['file_id']);
|
|
|
|
if (0 === \count($files) || FileInfo::TYPE_FILE !== $files[0]->getType()) {
|
2022-10-17 17:41:58 +00:00
|
|
|
continue;
|
2022-10-08 02:00:55 +00:00
|
|
|
}
|
2022-10-17 17:41:58 +00:00
|
|
|
|
|
|
|
// Get (hopefully cached) preview image
|
|
|
|
try {
|
|
|
|
$preview = $this->previewManager->getPreview($files[0], 2048, 2048, false);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Got the preview
|
|
|
|
break;
|
2022-10-07 19:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:41:58 +00:00
|
|
|
// Make sure the preview is valid
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $preview) {
|
2022-10-17 17:41:58 +00:00
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Crop image
|
|
|
|
$image = new \Imagick();
|
|
|
|
$image->readImageBlob($preview->getContent());
|
|
|
|
$iw = $image->getImageWidth();
|
|
|
|
$ih = $image->getImageHeight();
|
2022-10-19 17:15:14 +00:00
|
|
|
$dw = (float) $detection['width'];
|
|
|
|
$dh = (float) $detection['height'];
|
|
|
|
$dcx = (float) $detection['x'] + (float) $detection['width'] / 2;
|
|
|
|
$dcy = (float) $detection['y'] + (float) $detection['height'] / 2;
|
2022-10-17 17:41:58 +00:00
|
|
|
$faceDim = max($dw * $iw, $dh * $ih) * 1.5;
|
|
|
|
$image->cropImage(
|
2022-10-19 17:10:36 +00:00
|
|
|
(int) $faceDim,
|
|
|
|
(int) $faceDim,
|
|
|
|
(int) ($dcx * $iw - $faceDim / 2),
|
|
|
|
(int) ($dcy * $ih - $faceDim / 2),
|
2022-10-17 17:41:58 +00:00
|
|
|
);
|
2022-10-18 14:54:44 +00:00
|
|
|
$image->scaleImage(256, 256, true);
|
2022-10-17 17:41:58 +00:00
|
|
|
$blob = $image->getImageBlob();
|
|
|
|
|
|
|
|
// Create and send response
|
|
|
|
$response = new DataDisplayResponse($blob, Http::STATUS_OK, [
|
|
|
|
'Content-Type' => $image->getImageMimeType(),
|
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-10-17 17:41:58 +00:00
|
|
|
return $response;
|
2022-10-07 19:28:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 13:21:40 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get image info for one file
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-09-25 13:21:40 +00:00
|
|
|
* @param string fileid
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function imageInfo(string $id): JSONResponse
|
|
|
|
{
|
2022-09-25 13:21:40 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-09-25 13:21:40 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
|
|
|
|
|
|
|
|
// Check for permissions and get numeric Id
|
2022-10-19 17:10:36 +00:00
|
|
|
$file = $userFolder->getById((int) $id);
|
|
|
|
if (0 === \count($file)) {
|
2022-09-25 13:21:40 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
$file = $file[0];
|
|
|
|
|
|
|
|
// Get the image info
|
|
|
|
$info = $this->timelineQuery->getInfoById($file->getId());
|
|
|
|
|
|
|
|
return new JSONResponse($info, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Change exif data for one file
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-09-25 13:21:40 +00:00
|
|
|
* @param string fileid
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function imageEdit(string $id): JSONResponse
|
|
|
|
{
|
2022-09-25 13:21:40 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-09-25 13:21:40 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
|
|
|
|
|
|
|
|
// Check for permissions and get numeric Id
|
2022-10-19 17:10:36 +00:00
|
|
|
$file = $userFolder->getById((int) $id);
|
|
|
|
if (0 === \count($file)) {
|
2022-09-25 13:21:40 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
$file = $file[0];
|
|
|
|
|
2022-09-25 15:09:04 +00:00
|
|
|
// Check if user has permissions
|
|
|
|
if (!$file->isUpdateable()) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
2022-09-25 13:21:40 +00:00
|
|
|
|
|
|
|
// Get new date from body
|
|
|
|
$body = $this->request->getParams();
|
|
|
|
if (!isset($body['date'])) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Missing date'], Http::STATUS_BAD_REQUEST);
|
2022-09-25 13:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the date is valid
|
|
|
|
try {
|
|
|
|
Exif::parseExifDate($body['date']);
|
|
|
|
} catch (\Exception $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
|
2022-09-25 13:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update date
|
|
|
|
try {
|
|
|
|
$res = Exif::updateExifDate($file, $body['date']);
|
2022-10-19 17:10:36 +00:00
|
|
|
if (false === $res) {
|
2022-09-25 13:21:40 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-09-25 13:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reprocess the file
|
|
|
|
$this->timelineWrite->processFile($file, true);
|
|
|
|
|
|
|
|
return $this->imageInfo($id);
|
|
|
|
}
|
|
|
|
|
2022-09-25 23:02:26 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Move one file to the archive folder
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-09-25 23:02:26 +00:00
|
|
|
* @param string fileid
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function archive(string $id): JSONResponse
|
|
|
|
{
|
2022-09-25 23:02:26 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
|
|
|
return new JSONResponse(['message' => 'Not logged in'], Http::STATUS_PRECONDITION_FAILED);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($uid);
|
|
|
|
|
|
|
|
// Check for permissions and get numeric Id
|
2022-10-19 17:10:36 +00:00
|
|
|
$file = $userFolder->getById((int) $id);
|
|
|
|
if (0 === \count($file)) {
|
|
|
|
return new JSONResponse(['message' => 'No such file'], Http::STATUS_NOT_FOUND);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
$file = $file[0];
|
|
|
|
|
|
|
|
// Check if user has permissions
|
|
|
|
if (!$file->isUpdateable()) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Cannot update this file'], Http::STATUS_FORBIDDEN);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create archive folder in the root of the user's configured timeline
|
|
|
|
$timelinePath = Exif::removeExtraSlash(Exif::getPhotosPath($this->config, $uid));
|
|
|
|
$timelineFolder = $userFolder->get($timelinePath);
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $timelineFolder || !$timelineFolder instanceof Folder) {
|
|
|
|
return new JSONResponse(['message' => 'Cannot get timeline'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
if (!$timelineFolder->isCreatable()) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Cannot create archive folder'], Http::STATUS_FORBIDDEN);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get path of current file relative to the timeline folder
|
|
|
|
// remove timelineFolder path from start of file path
|
|
|
|
$timelinePath = $timelineFolder->getPath(); // no trailing slash
|
2022-10-19 17:10:36 +00:00
|
|
|
if (substr($file->getPath(), 0, \strlen($timelinePath)) !== $timelinePath) {
|
|
|
|
return new JSONResponse(['message' => 'Files outside timeline cannot be archived'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
$relativePath = substr($file->getPath(), \strlen($timelinePath)); // has a leading slash
|
2022-09-25 23:02:26 +00:00
|
|
|
|
|
|
|
// Final path of the file including the file name
|
|
|
|
$destinationPath = '';
|
|
|
|
|
|
|
|
// Check if we want to archive or unarchive
|
|
|
|
$body = $this->request->getParams();
|
2022-10-19 17:10:36 +00:00
|
|
|
$unarchive = isset($body['archive']) && false === $body['archive'];
|
2022-09-25 23:02:26 +00:00
|
|
|
|
|
|
|
// Get if the file is already in the archive (relativePath starts with archive)
|
2022-10-19 17:10:36 +00:00
|
|
|
$archiveFolderWithLeadingSlash = '/'.\OCA\Memories\Util::$ARCHIVE_FOLDER;
|
|
|
|
if (substr($relativePath, 0, \strlen($archiveFolderWithLeadingSlash)) === $archiveFolderWithLeadingSlash) {
|
2022-09-25 23:02:26 +00:00
|
|
|
// file already in archive, remove it instead
|
2022-10-19 17:10:36 +00:00
|
|
|
$destinationPath = substr($relativePath, \strlen($archiveFolderWithLeadingSlash));
|
2022-09-25 23:02:26 +00:00
|
|
|
if (!$unarchive) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'File already archived'], Http::STATUS_BAD_REQUEST);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// file not in archive, put it in there
|
2022-10-19 17:10:36 +00:00
|
|
|
$destinationPath = Exif::removeExtraSlash(\OCA\Memories\Util::$ARCHIVE_FOLDER.$relativePath);
|
2022-09-25 23:02:26 +00:00
|
|
|
if ($unarchive) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'File not archived'], Http::STATUS_BAD_REQUEST);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the filename
|
|
|
|
$destinationFolders = explode('/', $destinationPath);
|
|
|
|
array_pop($destinationFolders);
|
|
|
|
|
|
|
|
// Create folder tree
|
|
|
|
$folder = $timelineFolder;
|
|
|
|
foreach ($destinationFolders as $folderName) {
|
2022-10-19 17:10:36 +00:00
|
|
|
if ('' === $folderName) {
|
2022-09-25 23:02:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-09-25 23:02:26 +00:00
|
|
|
try {
|
2022-10-19 17:10:36 +00:00
|
|
|
$existingFolder = $folder->get($folderName.'/');
|
2022-09-25 23:02:26 +00:00
|
|
|
if (!$existingFolder instanceof Folder) {
|
|
|
|
throw new \OCP\Files\NotFoundException('Not a folder');
|
|
|
|
}
|
|
|
|
$folder = $existingFolder;
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
try {
|
|
|
|
$folder = $folder->newFolder($folderName);
|
|
|
|
} catch (\OCP\Files\NotPermittedException $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Failed to create folder'], Http::STATUS_FORBIDDEN);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move file to archive folder
|
|
|
|
try {
|
2022-10-19 17:10:36 +00:00
|
|
|
$file->move($folder->getPath().'/'.$file->getName());
|
2022-09-25 23:02:26 +00:00
|
|
|
} catch (\OCP\Files\NotPermittedException $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Failed to move file'], Http::STATUS_FORBIDDEN);
|
2022-09-25 23:02:26 +00:00
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'File not found'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-09-25 23:02:26 +00:00
|
|
|
} catch (\OCP\Files\InvalidPathException $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'Invalid path'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-09-25 23:02:26 +00:00
|
|
|
} catch (\OCP\Lock\LockedException $e) {
|
2022-10-19 17:10:36 +00:00
|
|
|
return new JSONResponse(['message' => 'File is locked'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-09-25 23:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* update preferences (user setting)
|
|
|
|
*
|
|
|
|
* @param string key the identifier to change
|
|
|
|
* @param string value the value to set
|
|
|
|
*
|
|
|
|
* @return JSONResponse an empty JSONResponse with respective http status code
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function setUserConfig(string $key, string $value): JSONResponse
|
|
|
|
{
|
2022-09-09 07:31:42 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (null === $user) {
|
2022-09-09 07:31:42 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-10-22 17:41:34 +00:00
|
|
|
// Make sure not running in read-only mode
|
2022-10-22 17:45:20 +00:00
|
|
|
if ($this->config->getSystemValue('memories.readonly', false)) {
|
2022-10-22 17:59:54 +00:00
|
|
|
return new JSONResponse(['message' => 'Cannot change settings in readonly mode'], Http::STATUS_FORBIDDEN);
|
2022-10-22 17:41:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
$userId = $user->getUid();
|
|
|
|
$this->config->setUserValue($userId, Application::APPNAME, $key, $value);
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-09-09 07:31:42 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
}
|
2022-10-17 02:52:44 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function serviceWorker(): StreamResponse
|
|
|
|
{
|
|
|
|
$response = new StreamResponse(__DIR__.'/../../js/memories-service-worker.js');
|
|
|
|
$response->setHeaders([
|
|
|
|
'Content-Type' => 'application/javascript',
|
|
|
|
'Service-Worker-Allowed' => '/',
|
|
|
|
]);
|
|
|
|
$policy = new ContentSecurityPolicy();
|
|
|
|
$policy->addAllowedWorkerSrcDomain("'self'");
|
|
|
|
$policy->addAllowedScriptDomain("'self'");
|
|
|
|
$policy->addAllowedConnectDomain("'self'");
|
|
|
|
$response->setContentSecurityPolicy($policy);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get transformations depending on the request.
|
2022-10-24 16:21:38 +00:00
|
|
|
*
|
|
|
|
* @param bool $aggregateOnly Only apply transformations for aggregation (days call)
|
2022-10-19 17:10:36 +00:00
|
|
|
*/
|
2022-10-24 16:21:38 +00:00
|
|
|
private function getTransformations(bool $aggregateOnly)
|
2022-10-19 17:10:36 +00:00
|
|
|
{
|
|
|
|
$transforms = [];
|
|
|
|
|
|
|
|
// Filter only favorites
|
|
|
|
if ($this->request->getParam('fav')) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformFavoriteFilter'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter only videos
|
|
|
|
if ($this->request->getParam('vid')) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformVideoFilter'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter only for one face
|
|
|
|
if ($this->recognizeIsEnabled()) {
|
|
|
|
$face = $this->request->getParam('face');
|
|
|
|
if ($face) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformFaceFilter', $face];
|
|
|
|
}
|
|
|
|
|
|
|
|
$faceRect = $this->request->getParam('facerect');
|
2022-10-24 16:21:38 +00:00
|
|
|
if ($faceRect && !$aggregateOnly) {
|
2022-10-19 17:10:36 +00:00
|
|
|
$transforms[] = [$this->timelineQuery, 'transformFaceRect', $face];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter only for one tag
|
|
|
|
if ($this->tagsIsEnabled()) {
|
|
|
|
$tagName = $this->request->getParam('tag');
|
|
|
|
if ($tagName) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformTagFilter', $tagName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit number of responses for day query
|
|
|
|
$limit = $this->request->getParam('limit');
|
|
|
|
if ($limit) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformLimitDay', (int) $limit];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $transforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Preload a few "day" at the start of "days" response */
|
|
|
|
private function preloadDays(array &$days, Folder &$folder, bool $recursive, bool $archive)
|
|
|
|
{
|
|
|
|
$uid = $this->userSession->getUser()->getUID();
|
2022-10-24 16:21:38 +00:00
|
|
|
$transforms = $this->getTransformations(false);
|
2022-10-19 17:10:36 +00:00
|
|
|
$preloaded = 0;
|
2022-10-26 18:10:24 +00:00
|
|
|
$preloadDayIds = [];
|
|
|
|
$preloadDays = [];
|
2022-10-19 17:10:36 +00:00
|
|
|
foreach ($days as &$day) {
|
2022-10-26 18:10:24 +00:00
|
|
|
$preloaded += $day['count'];
|
|
|
|
$preloadDayIds[] = $day['dayid'];
|
|
|
|
$preloadDays[] = &$day;
|
|
|
|
|
|
|
|
if ($preloaded >= 50) { // should be enough
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\count($preloadDayIds) > 0) {
|
|
|
|
$allDetails = $this->timelineQuery->getDay(
|
2022-10-19 17:10:36 +00:00
|
|
|
$folder,
|
|
|
|
$uid,
|
2022-10-26 18:10:24 +00:00
|
|
|
$preloadDayIds,
|
2022-10-19 17:10:36 +00:00
|
|
|
$recursive,
|
|
|
|
$archive,
|
|
|
|
$transforms,
|
|
|
|
);
|
|
|
|
|
2022-10-26 18:10:24 +00:00
|
|
|
// Group into dayid
|
|
|
|
$detailMap = [];
|
|
|
|
foreach ($allDetails as &$detail) {
|
|
|
|
$detailMap[$detail['dayid']][] = &$detail;
|
|
|
|
}
|
|
|
|
foreach ($preloadDays as &$day) {
|
|
|
|
$m = $detailMap[$day['dayid']];
|
|
|
|
if (isset($m) && null !== $m && \count($m) > 0) {
|
|
|
|
$day['detail'] = $m;
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the Folder object relevant to the request */
|
|
|
|
private function getRequestFolder()
|
|
|
|
{
|
|
|
|
$uid = $this->userSession->getUser()->getUID();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$folder = null;
|
|
|
|
$folderPath = $this->request->getParam('folder');
|
2022-10-25 04:30:43 +00:00
|
|
|
$forcedTimelinePath = $this->request->getParam('timelinePath');
|
2022-10-19 17:10:36 +00:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($uid);
|
|
|
|
|
|
|
|
if (null !== $folderPath) {
|
|
|
|
$folder = $userFolder->get($folderPath);
|
2022-10-25 04:30:43 +00:00
|
|
|
} elseif (null !== $forcedTimelinePath) {
|
|
|
|
$folder = $userFolder->get($forcedTimelinePath);
|
2022-10-19 17:10:36 +00:00
|
|
|
} else {
|
|
|
|
$configPath = Exif::removeExtraSlash(Exif::getPhotosPath($this->config, $uid));
|
|
|
|
$folder = $userFolder->get($configPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$folder instanceof Folder) {
|
|
|
|
throw new \Exception('Folder not found');
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if tags is enabled for this user.
|
|
|
|
*/
|
|
|
|
private function tagsIsEnabled(): bool
|
|
|
|
{
|
|
|
|
return $this->appManager->isEnabledForUser('systemtags');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if recognize is enabled for this user.
|
|
|
|
*/
|
|
|
|
private function recognizeIsEnabled(): bool
|
|
|
|
{
|
|
|
|
if (!$this->appManager->isEnabledForUser('recognize')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$v = $this->appManager->getAppInfo('recognize')['version'];
|
|
|
|
|
|
|
|
return version_compare($v, '3.0.0-alpha', '>=');
|
|
|
|
}
|
|
|
|
}
|