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-04-14 23:02:13 +00:00
|
|
|
use OCA\Memories\Service\BinExt;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Util;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\AppFramework\Http\StreamResponse;
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return JSONResponse an empty JSONResponse with respective http status code
|
|
|
|
*/
|
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-04-10 06:24:59 +00:00
|
|
|
/**
|
|
|
|
* @AdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function getSystemConfig(): Http\Response
|
|
|
|
{
|
|
|
|
return Util::guardEx(function () {
|
|
|
|
$config = [];
|
|
|
|
foreach (Util::systemConfigDefaults() as $key => $default) {
|
|
|
|
$config[$key] = $this->config->getSystemValue($key, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse($config, Http::STATUS_OK);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @AdminRequired
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setSystemConfig(string $key, $value): Http\Response
|
|
|
|
{
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:37:56 +00:00
|
|
|
// Assign config with type checking
|
2023-04-10 21:01:29 +00:00
|
|
|
Util::setSystemConfig($key, $value);
|
2023-04-10 06:24:59 +00:00
|
|
|
|
2023-04-10 21:37:56 +00:00
|
|
|
// If changing vod settings, kill any running go-vod instances
|
|
|
|
if (0 === strpos($key, 'memories.vod.')) {
|
2023-04-10 23:45:04 +00:00
|
|
|
try {
|
|
|
|
BinExt::startGoVod();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
error_log('Failed to start go-vod: '.$e->getMessage());
|
|
|
|
}
|
2023-04-10 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 06:24:59 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-10 07:20:17 +00:00
|
|
|
/**
|
|
|
|
* @AdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
2023-04-17 03:05:29 +00:00
|
|
|
*
|
|
|
|
* @UseSession
|
2023-04-10 07:20:17 +00:00
|
|
|
*/
|
|
|
|
public function getSystemStatus(): Http\Response
|
|
|
|
{
|
|
|
|
return Util::guardEx(function () {
|
2023-04-17 01:47:26 +00:00
|
|
|
$config = \OC::$server->get(\OCP\IConfig::class);
|
|
|
|
$index = \OC::$server->get(\OCA\Memories\Service\Index::class);
|
|
|
|
|
|
|
|
// Build status array
|
2023-04-10 07:20:17 +00:00
|
|
|
$status = [];
|
|
|
|
|
2023-04-10 21:01:29 +00:00
|
|
|
// Check exiftool version
|
2023-04-11 00:51:16 +00:00
|
|
|
try {
|
|
|
|
$s = $this->getExecutableStatus(BinExt::getExiftoolPBin());
|
|
|
|
if ('ok' === $s || Util::getSystemConfig('memories.exiftool_no_local')) {
|
2023-04-10 21:01:29 +00:00
|
|
|
BinExt::testExiftool();
|
2023-04-11 00:51:16 +00:00
|
|
|
$s = 'test_ok';
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
2023-04-11 00:51:16 +00:00
|
|
|
$status['exiftool'] = $s;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status['exiftool'] = 'test_fail:'.$e->getMessage();
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
2023-04-10 07:20:17 +00:00
|
|
|
|
|
|
|
// Check for system perl
|
|
|
|
$status['perl'] = $this->getExecutableStatus(exec('which perl'));
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Check number of indexed files
|
|
|
|
$status['indexed_count'] = $index->getIndexedCount();
|
|
|
|
|
2023-04-17 01:47:26 +00:00
|
|
|
// Automatic indexing stats
|
|
|
|
$jobStart = $config->getAppValue(Application::APPNAME, 'last_index_job_start', 0);
|
|
|
|
$status['last_index_job_start'] = $jobStart ? time() - $jobStart : 0; // Seconds ago
|
|
|
|
$status['last_index_job_duration'] = $config->getAppValue(Application::APPNAME, 'last_index_job_duration', 0);
|
|
|
|
$status['last_index_job_status'] = $config->getAppValue(Application::APPNAME, 'last_index_job_status', 'Indexing has not been run yet');
|
|
|
|
$status['last_index_job_status_type'] = $config->getAppValue(Application::APPNAME, 'last_index_job_status_type', 'warning');
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Check supported preview mimes
|
|
|
|
$status['mimes'] = $index->getPreviewMimes($index->getAllMimes());
|
|
|
|
|
|
|
|
// Check for bad encryption module
|
|
|
|
$status['bad_encryption'] = \OCA\Memories\Util::isEncryptionEnabled();
|
|
|
|
|
2023-04-11 02:17:16 +00:00
|
|
|
// Get GIS status
|
|
|
|
$places = \OC::$server->get(\OCA\Memories\Service\Places::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$status['gis_type'] = $places->detectGisType();
|
|
|
|
$status['gis_count'] = $places->geomCount();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status['gis_type'] = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
2023-04-10 07:20:17 +00:00
|
|
|
// Check ffmpeg and ffprobe binaries
|
|
|
|
$status['ffmpeg'] = $this->getExecutableStatus(Util::getSystemConfig('memories.vod.ffmpeg'));
|
|
|
|
$status['ffprobe'] = $this->getExecutableStatus(Util::getSystemConfig('memories.vod.ffprobe'));
|
|
|
|
|
|
|
|
// Check go-vod binary
|
2023-04-11 00:57:57 +00:00
|
|
|
try {
|
|
|
|
$s = $this->getExecutableStatus(BinExt::getGoVodBin());
|
|
|
|
if ('ok' === $s || Util::getSystemConfig('memories.vod.external')) {
|
2023-04-10 23:45:04 +00:00
|
|
|
BinExt::testStartGoVod();
|
2023-04-11 00:57:57 +00:00
|
|
|
$s = 'test_ok';
|
2023-04-10 23:45:04 +00:00
|
|
|
}
|
2023-04-11 00:57:57 +00:00
|
|
|
$status['govod'] = $s;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status['govod'] = 'test_fail:'.$e->getMessage();
|
2023-04-10 23:45:04 +00:00
|
|
|
}
|
2023-04-10 07:20:17 +00:00
|
|
|
|
|
|
|
// Check for VA-API device
|
|
|
|
$devPath = '/dev/dri/renderD128';
|
2023-04-17 16:31:54 +00:00
|
|
|
if (!file_exists($devPath)) {
|
2023-04-10 07:20:17 +00:00
|
|
|
$status['vaapi_dev'] = 'not_found';
|
|
|
|
} elseif (!is_readable($devPath)) {
|
|
|
|
$status['vaapi_dev'] = 'not_readable';
|
|
|
|
} else {
|
|
|
|
$status['vaapi_dev'] = 'ok';
|
|
|
|
}
|
|
|
|
|
2023-04-17 03:05:29 +00:00
|
|
|
// Action token
|
|
|
|
$status['action_token'] = $this->actionToken(true);
|
|
|
|
|
2023-04-10 07:20:17 +00:00
|
|
|
return new JSONResponse($status, Http::STATUS_OK);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-11 02:17:16 +00:00
|
|
|
/**
|
|
|
|
* @AdminRequired
|
2023-04-17 03:05:29 +00:00
|
|
|
*
|
|
|
|
* @UseSession
|
2023-04-11 02:17:16 +00:00
|
|
|
*/
|
2023-04-17 03:05:29 +00:00
|
|
|
public function placesSetup(?string $actiontoken): Http\Response
|
2023-04-11 02:17:16 +00:00
|
|
|
{
|
2023-04-17 03:05:29 +00:00
|
|
|
if (!$actiontoken || $this->actionToken() !== $actiontoken) {
|
|
|
|
return new JSONResponse(['error' => 'Invalid action token. Refresh the memories admin page.'], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset action token
|
|
|
|
$this->actionToken(true);
|
|
|
|
|
2023-04-11 02:17:16 +00:00
|
|
|
try {
|
|
|
|
// Set PHP timeout to infinite
|
|
|
|
set_time_limit(0);
|
|
|
|
|
|
|
|
// Send headers for long-running request
|
|
|
|
header('Content-Type: text/plain');
|
|
|
|
header('X-Accel-Buffering: no');
|
|
|
|
header('Cache-Control: no-cache');
|
|
|
|
header('Connection: keep-alive');
|
|
|
|
header('Content-Length: 0');
|
|
|
|
|
|
|
|
$places = \OC::$server->get(\OCA\Memories\Service\Places::class);
|
|
|
|
$datafile = $places->downloadPlanet();
|
|
|
|
$places->importPlanet($datafile);
|
2023-04-17 02:53:06 +00:00
|
|
|
$places->recalculateAll();
|
2023-04-11 02:17:16 +00:00
|
|
|
|
|
|
|
echo "Done.\n";
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
echo 'Failed: '.$e->getMessage()."\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function serviceWorker(): StreamResponse
|
|
|
|
{
|
|
|
|
$response = new StreamResponse(__DIR__.'/../../js/memories-service-worker.js');
|
|
|
|
$response->setHeaders([
|
|
|
|
'Content-Type' => 'application/javascript',
|
|
|
|
'Service-Worker-Allowed' => '/',
|
|
|
|
]);
|
2023-02-09 20:02:11 +00:00
|
|
|
$response->setContentSecurityPolicy(PageController::getCSP());
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2023-04-10 07:20:17 +00:00
|
|
|
|
|
|
|
private function getExecutableStatus(string $path): string
|
|
|
|
{
|
|
|
|
if (!is_file($path)) {
|
|
|
|
return 'not_found';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_executable($path)) {
|
|
|
|
return 'not_executable';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'ok';
|
|
|
|
}
|
2023-04-17 03:05:29 +00:00
|
|
|
|
|
|
|
private function actionToken(bool $set = false): string
|
|
|
|
{
|
|
|
|
$session = \OC::$server->get(\OCP\ISession::class);
|
|
|
|
if (!$set) {
|
|
|
|
return $session->get('memories_action_token');
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = bin2hex(random_bytes(32));
|
|
|
|
$session->set('memories_action_token', $token);
|
|
|
|
|
|
|
|
return $token ?? '';
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|