2022-08-13 03:34:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @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-17 22:03:29 +00:00
|
|
|
use OC\Files\Search\SearchComparison;
|
|
|
|
use OC\Files\Search\SearchQuery;
|
2022-08-18 18:27:25 +00:00
|
|
|
use OCA\Memories\AppInfo\Application;
|
2022-08-13 03:34:05 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\AppFramework\Http\StreamResponse;
|
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
2022-08-17 20:39:48 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
2022-08-13 03:34:05 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2022-08-17 22:03:29 +00:00
|
|
|
use OCP\Files\FileInfo;
|
|
|
|
use OCP\Files\Search\ISearchComparison;
|
2022-08-13 03:34:05 +00:00
|
|
|
|
|
|
|
class ApiController extends Controller {
|
|
|
|
private IConfig $config;
|
|
|
|
private IUserSession $userSession;
|
|
|
|
private IDBConnection $connection;
|
2022-08-18 18:27:25 +00:00
|
|
|
private \OCA\Memories\Db\Util $util;
|
2022-08-17 20:39:48 +00:00
|
|
|
private IRootFolder $rootFolder;
|
2022-08-13 03:34:05 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
IConfig $config,
|
|
|
|
IUserSession $userSession,
|
2022-08-17 20:39:48 +00:00
|
|
|
IDBConnection $connection,
|
|
|
|
IRootFolder $rootFolder,
|
2022-08-13 03:34:05 +00:00
|
|
|
) {
|
|
|
|
parent::__construct(Application::APPNAME, $request);
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->connection = $connection;
|
2022-08-18 18:27:25 +00:00
|
|
|
$this->util = new \OCA\Memories\Db\Util($this->connection);
|
2022-08-17 20:39:48 +00:00
|
|
|
$this->rootFolder = $rootFolder;
|
2022-08-13 03:34:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 23:31:47 +00:00
|
|
|
/**
|
2022-08-13 03:34:05 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2022-08-14 23:31:47 +00:00
|
|
|
public function days(): JSONResponse {
|
2022-08-13 03:34:05 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (is_null($user)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-08-20 00:18:04 +00:00
|
|
|
$list = $this->util->getDays($this->config, $user->getUID());
|
2022-08-14 23:31:47 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function day(string $id): JSONResponse {
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (is_null($user) || !is_numeric($id)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-08-20 00:18:04 +00:00
|
|
|
$list = $this->util->getDay($this->config, $user->getUID(), intval($id));
|
2022-08-13 03:34:05 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:39:48 +00:00
|
|
|
/**
|
|
|
|
* Check if folder is allowed and get it if yes
|
|
|
|
*/
|
|
|
|
private function getAllowedFolder(int $folder, $user) {
|
|
|
|
// Get root if folder not specified
|
|
|
|
$root = $this->rootFolder->getUserFolder($user->getUID());
|
|
|
|
if ($folder === 0) {
|
|
|
|
$folder = $root->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check access to folder
|
|
|
|
$nodes = $root->getById($folder);
|
|
|
|
if (empty($nodes)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check it is a folder
|
|
|
|
$node = $nodes[0];
|
|
|
|
if (!$node instanceof \OCP\Files\Folder) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2022-08-16 03:58:55 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2022-08-16 04:02:19 +00:00
|
|
|
public function folder(string $folder): JSONResponse {
|
2022-08-16 03:58:55 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (is_null($user) || !is_numeric($folder)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:39:48 +00:00
|
|
|
// Check permissions
|
|
|
|
$node = $this->getAllowedFolder(intval($folder), $user);
|
|
|
|
if (is_null($node)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get response from db
|
|
|
|
$list = $this->util->getDaysFolder($node->getId());
|
|
|
|
|
|
|
|
// Get subdirectories
|
2022-08-17 22:03:29 +00:00
|
|
|
$sub = $node->search(new SearchQuery(
|
|
|
|
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', FileInfo::MIMETYPE_FOLDER),
|
|
|
|
0, 0, [], $user));
|
|
|
|
$sub = array_filter($sub, function ($item) use ($node) {
|
|
|
|
return $item->getParent()->getId() === $node->getId();
|
2022-08-17 20:39:48 +00:00
|
|
|
});
|
2022-08-17 22:03:29 +00:00
|
|
|
|
|
|
|
// Map sub to JSON array
|
|
|
|
$subdirArray = [
|
2022-08-17 20:39:48 +00:00
|
|
|
"day_id" => -0.1,
|
|
|
|
"detail" => array_map(function ($item) {
|
|
|
|
return [
|
|
|
|
"file_id" => $item->getId(),
|
|
|
|
"name" => $item->getName(),
|
|
|
|
"is_folder" => 1,
|
|
|
|
];
|
|
|
|
}, $sub, []),
|
|
|
|
];
|
2022-08-17 22:03:29 +00:00
|
|
|
$subdirArray["count"] = count($subdirArray["detail"]);
|
|
|
|
array_unshift($list, $subdirArray);
|
2022-08-17 20:39:48 +00:00
|
|
|
|
2022-08-16 03:58:55 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2022-08-16 04:02:19 +00:00
|
|
|
public function folderDay(string $folder, string $dayId): JSONResponse {
|
2022-08-16 03:58:55 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (is_null($user) || !is_numeric($folder) || !is_numeric($dayId)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:39:48 +00:00
|
|
|
$node = $this->getAllowedFolder(intval($folder), $user);
|
|
|
|
if ($node === NULL) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $this->util->getDayFolder($node->getId(), intval($dayId));
|
2022-08-16 03:58:55 +00:00
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
2022-08-13 03:34:05 +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
|
|
|
|
*/
|
|
|
|
public function setUserConfig(string $key, string $value): JSONResponse {
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (is_null($user)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $user->getUid();
|
|
|
|
$this->config->setUserValue($userId, Application::APPNAME, $key, $value);
|
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function serviceWorker(): StreamResponse {
|
|
|
|
$response = new StreamResponse(__DIR__.'/../../js/photos-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;
|
|
|
|
}
|
|
|
|
}
|