Skip to content

Commit

Permalink
refactor: move SystemConfig to file
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <[email protected]>
  • Loading branch information
pulsejet committed Oct 22, 2023
1 parent 8b2df7c commit e38da20
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 217 deletions.
15 changes: 8 additions & 7 deletions lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCA\Memories\AppInfo\Application;
use OCA\Memories\Exceptions;
use OCA\Memories\Service\BinExt;
use OCA\Memories\Settings\SystemConfig;
use OCA\Memories\Util;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -39,7 +40,7 @@ public function getSystemConfig(): Http\Response
{
return Util::guardEx(function () {
$config = [];
foreach (Util::systemConfigDefaults() as $key => $default) {
foreach (SystemConfig::DEFAULTS as $key => $default) {
$config[$key] = $this->config->getSystemValue($key, $default);
}

Expand All @@ -59,7 +60,7 @@ public function setSystemConfig(string $key, mixed $value): Http\Response
}

// Assign config with type checking
Util::setSystemConfig($key, $value);
SystemConfig::set($key, $value);

// If changing vod settings, kill any running go-vod instances
if (str_starts_with($key, 'memories.vod.')) {
Expand Down Expand Up @@ -89,7 +90,7 @@ public function getSystemStatus(): Http\Response
$status = [];

// Check exiftool version
$exiftoolNoLocal = Util::getSystemConfig('memories.exiftool_no_local');
$exiftoolNoLocal = SystemConfig::get('memories.exiftool_no_local');
$status['exiftool'] = $this->getExecutableStatus(
static fn () => BinExt::getExiftoolPBin(),
static fn () => BinExt::testExiftool(),
Expand Down Expand Up @@ -136,23 +137,23 @@ public function getSystemStatus(): Http\Response
// Check for FFmpeg for preview generation
/** @psalm-suppress ForbiddenCode */
$status['ffmpeg_preview'] = $this->getExecutableStatus(
Util::getSystemConfig('preview_ffmpeg_path')
SystemConfig::get('preview_ffmpeg_path')
?: trim(shell_exec('which ffmpeg') ?: ''),
static fn ($p) => BinExt::testFFmpeg($p, 'ffmpeg'),
);

// Check ffmpeg and ffprobe binaries for transcoding
$status['ffmpeg'] = $this->getExecutableStatus(
Util::getSystemConfig('memories.vod.ffmpeg'),
SystemConfig::get('memories.vod.ffmpeg'),
static fn ($p) => BinExt::testFFmpeg($p, 'ffmpeg'),
);
$status['ffprobe'] = $this->getExecutableStatus(
Util::getSystemConfig('memories.vod.ffprobe'),
SystemConfig::get('memories.vod.ffprobe'),
static fn ($p) => BinExt::testFFmpeg($p, 'ffprobe'),
);

// Check go-vod binary
$extGoVod = Util::getSystemConfig('memories.vod.external');
$extGoVod = SystemConfig::get('memories.vod.external');
$status['govod'] = $this->getExecutableStatus(
static fn () => BinExt::getGoVodBin(),
static fn () => BinExt::testStartGoVod(),
Expand Down
11 changes: 6 additions & 5 deletions lib/Controller/OtherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Memories\AppInfo\Application;
use OCA\Memories\Exceptions;
use OCA\Memories\Settings\SystemConfig;
use OCA\Memories\Util;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -85,9 +86,9 @@ public function getUserConfig(): Http\Response
return new JSONResponse([
// general stuff
'version' => $version,
'vod_disable' => Util::getSystemConfig('memories.vod.disable'),
'video_default_quality' => Util::getSystemConfig('memories.video_default_quality'),
'places_gis' => Util::getSystemConfig('memories.gis_type'),
'vod_disable' => SystemConfig::get('memories.vod.disable'),
'video_default_quality' => SystemConfig::get('memories.video_default_quality'),
'places_gis' => SystemConfig::get('memories.gis_type'),

// enabled apps
'systemtags_enabled' => Util::tagsIsEnabled(),
Expand All @@ -99,11 +100,11 @@ public function getUserConfig(): Http\Response
'preview_generator_enabled' => Util::previewGeneratorIsEnabled(),

// general settings
'timeline_path' => $getAppConfig('timelinePath', Util::getSystemConfig('memories.timeline.default_path')),
'timeline_path' => $getAppConfig('timelinePath', SystemConfig::get('memories.timeline.default_path')),
'enable_top_memories' => 'true' === $getAppConfig('enableTopMemories', 'true'),

// viewer settings
'high_res_cond_default' => Util::getSystemConfig('memories.viewer.high_res_cond_default'),
'high_res_cond_default' => SystemConfig::get('memories.viewer.high_res_cond_default'),
'livephoto_autoplay' => 'true' === $getAppConfig('livephotoAutoplay', 'true'),
'sidebar_filepath' => 'true' === $getAppConfig('sidebarFilepath', false),

Expand Down
4 changes: 2 additions & 2 deletions lib/Cron/IndexJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use OCA\Memories\AppInfo\Application;
use OCA\Memories\Service;
use OCA\Memories\Util;
use OCA\Memories\Settings\SystemConfig;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
Expand Down Expand Up @@ -38,7 +38,7 @@ public function __construct(
protected function run(mixed $argument): void
{
// Check if indexing is enabled
if ('0' === Util::getSystemConfig('memories.index.mode')) {
if ('0' === SystemConfig::get('memories.index.mode')) {
return;
}

Expand Down
66 changes: 33 additions & 33 deletions lib/Service/BinExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OCA\Memories\Service;

use OCA\Memories\Util;
use OCA\Memories\Settings\SystemConfig;

class BinExt
{
Expand All @@ -15,7 +15,7 @@ class BinExt
/** Get the path to the temp directory */
public static function getTmpPath(): string
{
return Util::getSystemConfig('memories.exiftool.tmp') ?: sys_get_temp_dir();
return SystemConfig::get('memories.exiftool.tmp') ?: sys_get_temp_dir();
}

/** Copy a binary to temp dir for execution */
Expand Down Expand Up @@ -56,7 +56,7 @@ public static function getTempBin(string $path, string $name, bool $copy = true)
/** Get the name for a binary */
public static function getName(string $name, string $version = ''): string
{
$id = Util::getInstanceId();
$id = SystemConfig::get('instanceid');

return empty($version) ? "{$name}-{$id}" : "{$name}-{$id}-{$version}";
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public static function testExiftool(): string
/** Get path to exiftool binary */
public static function getExiftoolPBin(): string
{
$path = Util::getSystemConfig('memories.exiftool');
$path = SystemConfig::get('memories.exiftool');

return self::getTempBin($path, self::getName('exiftool', self::EXIFTOOL_VER));
}
Expand All @@ -117,7 +117,7 @@ public static function getExiftoolPBin(): string
*/
public static function getExiftool(): array
{
if (Util::getSystemConfig('memories.exiftool_no_local')) {
if (SystemConfig::get('memories.exiftool_no_local')) {
return ['perl', realpath(__DIR__.'/../../bin-ext/exiftool/exiftool')];
}

Expand All @@ -129,11 +129,11 @@ public static function getExiftool(): array
*/
public static function detectExiftool(): false|string
{
if (!empty($path = Util::getSystemConfig('memories.exiftool')) && file_exists($path)) {
if (!empty($path = SystemConfig::get('memories.exiftool')) && file_exists($path)) {
return $path;
}

if (Util::getSystemConfig('memories.exiftool_no_local')) {
if (SystemConfig::get('memories.exiftool_no_local')) {
return implode(' ', self::getExiftool());
}

Expand All @@ -148,13 +148,13 @@ public static function detectExiftool(): false|string

// make sure it exists
if ($path && file_exists($path)) {
Util::setSystemConfig('memories.exiftool', $path);
SystemConfig::set('memories.exiftool', $path);

return $path;
}
}

Util::setSystemConfig('memories.exiftool_no_local', true);
SystemConfig::set('memories.exiftool_no_local', true);

return false;
}
Expand All @@ -166,8 +166,8 @@ public static function getGoVodUrl(string $client, string $path, string $profile
{
$path = rawurlencode($path);

$bind = Util::getSystemConfig('memories.vod.bind');
$connect = Util::getSystemConfig('memories.vod.connect', $bind);
$bind = SystemConfig::get('memories.vod.bind');
$connect = SystemConfig::get('memories.vod.connect', $bind);

return "http://{$connect}/{$client}{$path}/{$profile}";
}
Expand All @@ -176,36 +176,36 @@ public static function getGoVodConfig(bool $local = false): array
{
// Get config from system values
$env = [
'vaapi' => Util::getSystemConfig('memories.vod.vaapi'),
'vaapiLowPower' => Util::getSystemConfig('memories.vod.vaapi.low_power'),
'vaapi' => SystemConfig::get('memories.vod.vaapi'),
'vaapiLowPower' => SystemConfig::get('memories.vod.vaapi.low_power'),

'nvenc' => Util::getSystemConfig('memories.vod.nvenc'),
'nvencTemporalAQ' => Util::getSystemConfig('memories.vod.nvenc.temporal_aq'),
'nvencScale' => Util::getSystemConfig('memories.vod.nvenc.scale'),
'nvenc' => SystemConfig::get('memories.vod.nvenc'),
'nvencTemporalAQ' => SystemConfig::get('memories.vod.nvenc.temporal_aq'),
'nvencScale' => SystemConfig::get('memories.vod.nvenc.scale'),

'useTranspose' => Util::getSystemConfig('memories.vod.use_transpose'),
'useGopSize' => Util::getSystemConfig('memories.vod.use_gop_size'),
'useTranspose' => SystemConfig::get('memories.vod.use_transpose'),
'useGopSize' => SystemConfig::get('memories.vod.use_gop_size'),
];

if (!$local) {
return $env;
}

// Get temp directory
$tmpPath = Util::getSystemConfig('memories.vod.tempdir', sys_get_temp_dir().'/go-vod/');
$tmpPath = SystemConfig::get('memories.vod.tempdir', sys_get_temp_dir().'/go-vod/');

// Make sure path ends with slash
if ('/' !== substr($tmpPath, -1)) {
$tmpPath .= '/';
}

// Add instance ID to path
$tmpPath .= Util::getInstanceId();
$tmpPath .= SystemConfig::get('instanceid');

return array_merge($env, [
'bind' => Util::getSystemConfig('memories.vod.bind'),
'ffmpeg' => Util::getSystemConfig('memories.vod.ffmpeg'),
'ffprobe' => Util::getSystemConfig('memories.vod.ffprobe'),
'bind' => SystemConfig::get('memories.vod.bind'),
'ffmpeg' => SystemConfig::get('memories.vod.ffmpeg'),
'ffprobe' => SystemConfig::get('memories.vod.ffprobe'),
'tempdir' => $tmpPath,
]);
}
Expand All @@ -215,7 +215,7 @@ public static function getGoVodConfig(bool $local = false): array
*/
public static function getGoVodBin(): string
{
$path = Util::getSystemConfig('memories.vod.path');
$path = SystemConfig::get('memories.vod.path');

return self::getTempBin($path, self::getName('go-vod', self::GOVOD_VER));
}
Expand All @@ -227,15 +227,15 @@ public static function getGoVodBin(): string
public static function startGoVod(): ?string
{
// Check if disabled
if (Util::getSystemConfig('memories.vod.disable')) {
if (SystemConfig::get('memories.vod.disable')) {
// Make sure it's dead, in case the user just disabled it
self::pkill(self::getName('go-vod'));

return null;
}

// Check if external
if (Util::getSystemConfig('memories.vod.external')) {
if (SystemConfig::get('memories.vod.external')) {
self::configureGoVod();

return null;
Expand Down Expand Up @@ -306,7 +306,7 @@ public static function testStartGoVod(): string
public static function testGoVod(): string
{
// Check if disabled
if (Util::getSystemConfig('memories.vod.disable')) {
if (SystemConfig::get('memories.vod.disable')) {
throw new \Exception('Transcoding is disabled');
}

Expand Down Expand Up @@ -372,7 +372,7 @@ public static function configureGoVod(): bool
*/
public static function detectGoVod(): false|string
{
$goVodPath = Util::getSystemConfig('memories.vod.path');
$goVodPath = SystemConfig::get('memories.vod.path');

if (empty($goVodPath) || !file_exists($goVodPath)) {
// Detect architecture
Expand All @@ -385,7 +385,7 @@ public static function detectGoVod(): false|string
}

// Set config
Util::setSystemConfig('memories.vod.path', $goVodPath);
SystemConfig::set('memories.vod.path', $goVodPath);

// Make executable
if (!is_executable($goVodPath)) {
Expand All @@ -398,8 +398,8 @@ public static function detectGoVod(): false|string

public static function detectFFmpeg(): ?string
{
$ffmpegPath = Util::getSystemConfig('memories.vod.ffmpeg');
$ffprobePath = Util::getSystemConfig('memories.vod.ffprobe');
$ffmpegPath = SystemConfig::get('memories.vod.ffmpeg');
$ffprobePath = SystemConfig::get('memories.vod.ffprobe');

if (empty($ffmpegPath) || !file_exists($ffmpegPath) || empty($ffprobePath) || !file_exists($ffprobePath)) {
// Use PATH environment variable to find ffmpeg
Expand All @@ -418,8 +418,8 @@ public static function detectFFmpeg(): ?string
$ffprobePath = trim($ffprobePath);

// Set config
Util::setSystemConfig('memories.vod.ffmpeg', $ffmpegPath);
Util::setSystemConfig('memories.vod.ffprobe', $ffprobePath);
SystemConfig::set('memories.vod.ffmpeg', $ffmpegPath);
SystemConfig::set('memories.vod.ffprobe', $ffprobePath);
}

// Check if executable
Expand Down
5 changes: 3 additions & 2 deletions lib/Service/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Memories\AppInfo\Application;
use OCA\Memories\Db\TimelineWrite;
use OCA\Memories\Settings\SystemConfig;
use OCA\Memories\Util;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\File;
Expand Down Expand Up @@ -76,15 +77,15 @@ public function indexUser(string $uid, ?string $folder = null): void
$root = $this->rootFolder->getUserFolder($uid);

// Get paths of folders to index
$mode = Util::getSystemConfig('memories.index.mode');
$mode = SystemConfig::get('memories.index.mode');
if (null !== $folder) {
$paths = [$folder];
} elseif ('1' === $mode || '0' === $mode) { // everything (or nothing)
$paths = ['/'];
} elseif ('2' === $mode) { // timeline
$paths = Util::getTimelinePaths($uid);
} elseif ('3' === $mode) { // custom
$paths = [Util::getSystemConfig('memories.index.path')];
$paths = [SystemConfig::get('memories.index.path')];
} else {
throw new \Exception('Invalid index mode');
}
Expand Down
Loading

0 comments on commit e38da20

Please sign in to comment.