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;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
2023-10-22 19:58:33 +00:00
|
|
|
use OCA\Memories\Settings\SystemConfig;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Util;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2023-11-25 20:41:22 +00:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\AppFramework\Http\StreamResponse;
|
2023-10-20 21:28:08 +00:00
|
|
|
use OCP\IRequest;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class OtherController extends GenericApiController
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* update preferences (user setting)
|
|
|
|
*
|
|
|
|
* @param string key the identifier to change
|
|
|
|
* @param string value the value to set
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @return Http\Response empty JSONResponse with respective http status code
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function setUserConfig(string $key, string $value): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($key, $value) {
|
|
|
|
// Make sure not running in read-only mode
|
|
|
|
if ($this->config->getSystemValue('memories.readonly', false)) {
|
|
|
|
throw Exceptions::Forbidden('Cannot change settings in readonly mode');
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-24 00:56:41 +00:00
|
|
|
$this->config->setUserValue(Util::getUID(), Application::APPNAME, $key, $value);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 05:52:00 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
|
|
|
public function getUserConfig(): Http\Response
|
|
|
|
{
|
|
|
|
return Util::guardEx(function () {
|
2023-08-25 19:53:15 +00:00
|
|
|
// get memories version
|
|
|
|
$version = \OC::$server->get(\OCP\App\IAppManager::class)
|
2023-08-25 20:49:40 +00:00
|
|
|
->getAppInfo('memories')['version']
|
|
|
|
;
|
2023-05-03 05:52:00 +00:00
|
|
|
|
2023-08-25 19:53:15 +00:00
|
|
|
// get user if logged in
|
2023-05-03 05:52:00 +00:00
|
|
|
try {
|
|
|
|
$uid = Util::getUID();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$uid = null;
|
|
|
|
}
|
|
|
|
|
2023-08-25 19:53:15 +00:00
|
|
|
// helper function to get user config values
|
2023-10-15 19:46:35 +00:00
|
|
|
$getAppConfig = function (string $key, mixed $default) use ($uid): mixed {
|
2023-05-03 05:52:00 +00:00
|
|
|
return $this->config->getUserValue($uid, Application::APPNAME, $key, $default);
|
|
|
|
};
|
|
|
|
|
|
|
|
return new JSONResponse([
|
2023-08-25 19:53:15 +00:00
|
|
|
// general stuff
|
|
|
|
'version' => $version,
|
2023-10-22 19:58:33 +00:00
|
|
|
'vod_disable' => SystemConfig::get('memories.vod.disable'),
|
|
|
|
'video_default_quality' => SystemConfig::get('memories.video_default_quality'),
|
|
|
|
'places_gis' => SystemConfig::get('memories.gis_type'),
|
2023-05-03 05:52:00 +00:00
|
|
|
|
2023-08-25 19:53:15 +00:00
|
|
|
// enabled apps
|
2023-05-03 06:38:45 +00:00
|
|
|
'systemtags_enabled' => Util::tagsIsEnabled(),
|
|
|
|
'albums_enabled' => Util::albumsIsEnabled(),
|
2023-08-25 04:14:41 +00:00
|
|
|
'recognize_installed' => Util::recognizeIsInstalled(),
|
|
|
|
'recognize_enabled' => Util::recognizeIsEnabled(),
|
2023-05-03 06:38:45 +00:00
|
|
|
'facerecognition_installed' => Util::facerecognitionIsInstalled(),
|
|
|
|
'facerecognition_enabled' => Util::facerecognitionIsEnabled(),
|
2023-08-03 17:11:39 +00:00
|
|
|
'preview_generator_enabled' => Util::previewGeneratorIsEnabled(),
|
2023-05-03 05:52:00 +00:00
|
|
|
|
2023-08-25 19:53:15 +00:00
|
|
|
// general settings
|
2023-10-22 19:58:33 +00:00
|
|
|
'timeline_path' => $getAppConfig('timelinePath', SystemConfig::get('memories.timeline.default_path')),
|
2023-08-25 19:53:15 +00:00
|
|
|
'enable_top_memories' => 'true' === $getAppConfig('enableTopMemories', 'true'),
|
2023-10-30 01:12:14 +00:00
|
|
|
'stack_raw_files' => 'true' === $getAppConfig('stackRawFiles', 'true'),
|
2023-08-25 19:53:15 +00:00
|
|
|
|
|
|
|
// viewer settings
|
2023-10-22 19:58:33 +00:00
|
|
|
'high_res_cond_default' => SystemConfig::get('memories.viewer.high_res_cond_default'),
|
2023-08-25 19:53:15 +00:00
|
|
|
'livephoto_autoplay' => 'true' === $getAppConfig('livephotoAutoplay', 'true'),
|
|
|
|
'sidebar_filepath' => 'true' === $getAppConfig('sidebarFilepath', false),
|
|
|
|
|
|
|
|
// folder settings
|
2023-05-03 06:38:45 +00:00
|
|
|
'folders_path' => $getAppConfig('foldersPath', '/'),
|
|
|
|
'show_hidden_folders' => 'true' === $getAppConfig('showHidden', false),
|
|
|
|
'sort_folder_month' => 'true' === $getAppConfig('sortFolderMonth', false),
|
2023-08-25 19:53:15 +00:00
|
|
|
|
|
|
|
// album settings
|
2023-05-03 06:38:45 +00:00
|
|
|
'sort_album_month' => 'true' === $getAppConfig('sortAlbumMonth', 'true'),
|
2023-11-14 08:52:07 +00:00
|
|
|
'show_hidden_albums' => 'true' === $getAppConfig('showHiddenAlbums', false),
|
2023-05-03 05:52:00 +00:00
|
|
|
], Http::STATUS_OK);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-16 07:25:59 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
2023-10-15 19:46:35 +00:00
|
|
|
public function describeApi(): Http\Response
|
2023-05-16 07:25:59 +00:00
|
|
|
{
|
2023-08-30 18:10:08 +00:00
|
|
|
return Util::guardEx(static function () {
|
2023-05-16 07:25:59 +00:00
|
|
|
$appManager = \OC::$server->get(\OCP\App\IAppManager::class);
|
|
|
|
$urlGenerator = \OC::$server->get(\OCP\IURLGenerator::class);
|
|
|
|
|
2023-05-18 05:41:53 +00:00
|
|
|
$info = [
|
2023-05-16 07:25:59 +00:00
|
|
|
'version' => $appManager->getAppInfo('memories')['version'],
|
|
|
|
'baseUrl' => $urlGenerator->linkToRouteAbsolute('memories.Page.main'),
|
2023-05-16 10:18:04 +00:00
|
|
|
'loginFlowUrl' => $urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.init'),
|
2023-05-18 05:41:53 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$info['uid'] = Util::getUID();
|
2023-10-15 19:46:35 +00:00
|
|
|
} catch (\Exception) {
|
2023-05-18 05:41:53 +00:00
|
|
|
$info['uid'] = null;
|
|
|
|
}
|
2023-05-16 07:25:59 +00:00
|
|
|
|
|
|
|
// This is public information
|
2023-05-18 05:41:53 +00:00
|
|
|
$res = new JSONResponse($info);
|
2023-05-16 07:25:59 +00:00
|
|
|
$res->addHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
2023-10-20 19:21:41 +00:00
|
|
|
public function static(string $name): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-10-20 19:21:41 +00:00
|
|
|
return Util::guardEx(static function () use ($name) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'service-worker.js':
|
2023-11-14 09:10:29 +00:00
|
|
|
// Disable service worker if server is in debug mode
|
2023-11-25 18:57:03 +00:00
|
|
|
if (!\OC::$server->get(\OCP\IConfig::class)->getSystemValue('memories.sw.enabled', true)) {
|
2023-11-25 19:54:31 +00:00
|
|
|
throw Exceptions::NotFound('Service worker is disabled in global configuration');
|
2023-11-14 09:10:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 20:41:22 +00:00
|
|
|
// Get relative URL to JS web root of the app
|
|
|
|
$prefix = \OC::$server->get(\OCP\IURLGenerator::class)->linkTo('memories', 'js/memories-main.js');
|
|
|
|
$prefix = preg_replace('/memories-main\.js.*$/', '', $prefix);
|
|
|
|
|
|
|
|
// Make sure prefix starts and ends with a slash
|
|
|
|
$prefix = '/'.ltrim($prefix, '/');
|
|
|
|
$prefix = rtrim($prefix, '/').'/';
|
|
|
|
|
|
|
|
// Replace relative URLs to have correct prefix
|
|
|
|
$sw = file_get_contents(__DIR__.'/../../js/memories-service-worker.js');
|
|
|
|
$sw = str_replace('/apps/memories/js/', $prefix, $sw);
|
|
|
|
|
|
|
|
// Return processed service worker
|
|
|
|
$response = (new DataDisplayResponse($sw))->setHeaders([
|
2023-10-20 19:21:41 +00:00
|
|
|
'Content-Type' => 'application/javascript',
|
|
|
|
'Service-Worker-Allowed' => '/',
|
|
|
|
]);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2023-10-20 21:28:08 +00:00
|
|
|
case 'go-vod':
|
|
|
|
switch (\OC::$server->get(IRequest::class)->getParam('arch')) {
|
|
|
|
case 'x86_64':
|
|
|
|
case 'amd64':
|
|
|
|
return new StreamResponse(__DIR__.'/../../bin-ext/go-vod-amd64');
|
|
|
|
|
|
|
|
case 'aarch64':
|
|
|
|
case 'arm64':
|
|
|
|
return new StreamResponse(__DIR__.'/../../bin-ext/go-vod-aarch64');
|
|
|
|
}
|
|
|
|
|
|
|
|
// no break
|
2023-10-20 19:21:41 +00:00
|
|
|
default:
|
2023-10-20 21:28:08 +00:00
|
|
|
throw Exceptions::NotFound("File not found: {$name}");
|
2023-10-20 19:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Http\Response $response */
|
|
|
|
$response->setContentSecurityPolicy(PageController::getCSP());
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
}
|