new helper function to retrieve settings

This commit is contained in:
huangjx
2022-04-22 18:29:21 +08:00
parent 49477881a9
commit 5ae1685044
7 changed files with 29 additions and 24 deletions

View File

@@ -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;

View File

@@ -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!");
}
/**

View File

@@ -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));

View File

@@ -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;

View File

@@ -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"),

View File

@@ -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");
}
}

View File

@@ -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)
{