diff --git a/lib/Controller/Aria2Controller.php b/lib/Controller/Aria2Controller.php index 1951872..e5a8675 100644 --- a/lib/Controller/Aria2Controller.php +++ b/lib/Controller/Aria2Controller.php @@ -32,8 +32,7 @@ class Aria2Controller extends Controller $this->l10n = $IL10N; $this->rootFolder = $rootFolder; $this->urlGenerator = \OC::$server->getURLGenerator(); - $this->settings = new Settings($UserId); - $this->downloadDir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads"; + $this->downloadDir = Helper::getDownloadDir(); OC_Util::setupFS(); //$this->config = \OC::$server->getAppConfig(); $this->aria2 = $aria2; diff --git a/lib/Controller/MainController.php b/lib/Controller/MainController.php index b0463ed..dbbb991 100644 --- a/lib/Controller/MainController.php +++ b/lib/Controller/MainController.php @@ -38,10 +38,9 @@ class MainController extends Controller $this->dbconn = new DbHelper(); $this->counters = new Counters($aria2, $this->dbconn, $UserId); $this->youtube = $youtube; - $this->settings = new Settings($this->uid); $this->isAdmin = \OC_User::isAdminUser($this->uid); - $this->hideError = $this->settings->get("ncd_hide_errors", false); - $this->disable_bt_nonadmin = $this->settings->setType($this->settings::TYPE['SYSTEM'])->get("ncd_disable_bt", false); + $this->hideError = Helper::getSettings("ncd_hide_errors", false); + $this->disable_bt_nonadmin = Helper::getSettings("ncd_disable_bt", false, Settings::TYPE["SYSTEM"]); $this->accessDenied = $this->l10n->t("Sorry,only admin users can download files via BT!"); } /** diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 91f4a89..06c8af4 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -111,7 +111,7 @@ class SettingsController extends Controller { $saved = json_decode($this->settings->get("custom_youtube_dl_settings"), 1); $params = $this->request->getParams(); - foreach ($data as $key => $value) { + foreach ($params as $key => $value) { unset($saved[$key]); } $resp = $this->settings->save("custom_youtube_dl_settings", json_encode($saved)); diff --git a/lib/Controller/YoutubeController.php b/lib/Controller/YoutubeController.php index 3df4d8c..d87bab3 100644 --- a/lib/Controller/YoutubeController.php +++ b/lib/Controller/YoutubeController.php @@ -5,7 +5,6 @@ use OCA\NCDownloader\Tools\Aria2; use OCA\NCDownloader\Tools\DbHelper; use OCA\NCDownloader\Tools\folderScan; use OCA\NCDownloader\Tools\Helper; -use OCA\NCDownloader\Tools\Settings; use OCA\NCDownloader\Tools\Youtube; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; @@ -26,8 +25,7 @@ class YoutubeController extends Controller $this->uid = $UserId; $this->urlGenerator = \OC::$server->getURLGenerator(); $this->l10n = $IL10N; - $this->settings = new Settings($UserId); - $this->downloadDir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads"; + $this->downloadDir = Helper::getDownloadDir(); $this->dbconn = new DbHelper(); $this->youtube = $youtube; $this->aria2 = $aria2; diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index c289834..3293be5 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -34,7 +34,7 @@ class Personal implements ISettings { */ public function getForm() { $parameters = [ - "ncd_downloader_dir" => $this->settings->get("ncd_downloader_dir"), + "ncd_downloader_dir" => Helper::getDownloadDir(), "ncd_torrents_dir" => $this->settings->get("ncd_torrents_dir"), "ncd_seed_ratio" => $this->settings->get("ncd_seed_ratio"), 'ncd_seed_time_unit' => $this->settings->get("ncd_seed_time_unit"), diff --git a/lib/Tools/Helper.php b/lib/Tools/Helper.php index 225d688..6941290 100644 --- a/lib/Tools/Helper.php +++ b/lib/Tools/Helper.php @@ -399,16 +399,12 @@ class Helper public static function getRealDownloadDir($uid = null): string { - $uid = $uid ?? self::getUID(); - $settings = new Settings($uid); - $dlDir = $settings->get('ncd_downloader_dir') ?? "/Downloads"; + $dlDir = self::getDownloadDir();; return self::getLocalFolder($dlDir); } public static function getRealTorrentsDir($uid = null): string { - $uid = $uid ?? self::getUID(); - $settings = new Settings($uid); - $dir = $settings->get('ncd_torrents_dir') ?? "/Torrents"; + $dir = self::getSettings('ncd_torrents_dir', "/Torrents"); return self::getLocalFolder($dir); } @@ -422,14 +418,24 @@ class Helper return self::getUser()->getUID(); } - public static function getYoutubeConfig($uid = null): array + public static function getSettings($key, $default = null, int $type = Settings::TYPE['USER']) + { + $settings = self::newSettings(); + return $settings->setType($type)->get($key, $default); + } + + public static function newSettings($uid = null) { $uid = $uid ?? self::getUID(); - $settings = new Settings($uid); + return Settings::create($uid); + } + + public static function getYoutubeConfig($uid = null): array + { $config = [ - 'binary' => $settings->setType(Settings::TYPE['SYSTEM'])->get("ncd_yt_binary"), + 'binary' => self::getSettings("ncd_yt_binary", null, Settings::TYPE['SYSTEM']), 'downloadDir' => Helper::getRealDownloadDir(), - 'settings' => $settings->setType(Settings::TYPE['USER'])->getYoutube(), + 'settings' => self::newSettings()->getYoutube(), ]; return $config; } @@ -438,7 +444,7 @@ class Helper { $options = []; $uid = $uid ?? self::getUID(); - $settings = new Settings($uid); + $settings = self::newSettings($uid); $realDownloadDir = Helper::getRealDownloadDir($uid); $torrentsDir = Helper::getRealTorrentsDir($uid); $appPath = self::getAppPath(); @@ -467,7 +473,7 @@ class Helper { return \OC::$server->getAppManager()->getAppPath('ncdownloader'); } - public static function folderUpdated(string $dir):bool + public static function folderUpdated(string $dir): bool { if (!file_exists($dir)) { return false; @@ -486,4 +492,8 @@ class Helper } return false; } + public static function getDownloadDir(): string + { + return self::getSettings('ncd_downloader_dir', "/Downloads"); + } } diff --git a/lib/Tools/folderScan.php b/lib/Tools/folderScan.php index 24096d6..ba3e9d3 100644 --- a/lib/Tools/folderScan.php +++ b/lib/Tools/folderScan.php @@ -20,8 +20,7 @@ class folderScan public function getDefaultPath() { - $settings = new Settings($this->user); - return $settings->get('ncd_downloader_dir') ?? "/Downloads"; + return Helper::getDownloadDir(); } public static function create($path = null, $user = null) {