fixed #47;some cleaning up and bugfixes;
This commit is contained in:
@@ -2,9 +2,13 @@
|
||||
|
||||
namespace OCA\NCDownloader\Tools;
|
||||
|
||||
use Exception;
|
||||
use OCA\NCDownloader\Search\Sites\searchInterface;
|
||||
use OCA\NCDownloader\Tools\aria2Options;
|
||||
use OCA\NCDownloader\Tools\Settings;
|
||||
use OCP\IUser;
|
||||
use OC\Files\Filesystem;
|
||||
use OC_Util;
|
||||
|
||||
class Helper
|
||||
{
|
||||
@@ -135,6 +139,9 @@ class Helper
|
||||
|
||||
public static function debug($msg)
|
||||
{
|
||||
if (is_array($msg)) {
|
||||
$msg = implode(",", $msg);
|
||||
}
|
||||
$logger = \OC::$server->getLogger();
|
||||
$logger->error($msg, ['app' => 'ncdownloader']);
|
||||
}
|
||||
@@ -279,14 +286,13 @@ class Helper
|
||||
];
|
||||
return $titles[$type];
|
||||
}
|
||||
// the relative home folder of a nextcloud user
|
||||
public static function getUserFolder($uid = null)
|
||||
// the relative home folder of a nextcloud user,e.g. /admin/files
|
||||
public static function getUserFolder($uid = null): string
|
||||
{
|
||||
if (!empty($rootFolder = Filesystem::getRoot())) {
|
||||
return $rootFolder;
|
||||
} else if (isset($uid)) {
|
||||
return "/" . $uid . "/files";
|
||||
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -374,4 +380,110 @@ class Helper
|
||||
return $sites;
|
||||
}
|
||||
|
||||
public static function getMountPoints(): ?array
|
||||
{
|
||||
return Filesystem::getMountPoints("/");
|
||||
}
|
||||
|
||||
public static function getDataDir(): string
|
||||
{
|
||||
return \OC::$server->getSystemConfig()->getValue('datadirectory');
|
||||
}
|
||||
|
||||
public static function getLocalFolder(string $path): string
|
||||
{
|
||||
//without calling this, filesystem::getLocalFolder doesn't work
|
||||
OC_Util::setupFS();
|
||||
return Filesystem::getLocalFolder($path);
|
||||
}
|
||||
|
||||
public static function getRealDownloadDir($uid = null): string
|
||||
{
|
||||
$uid = $uid ?? self::getUID();
|
||||
$settings = new Settings($uid);
|
||||
$dlDir = $settings->get('ncd_downloader_dir') ?? "/Downloads";
|
||||
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";
|
||||
return self::getLocalFolder($dir);
|
||||
}
|
||||
|
||||
public static function getUser(): ?IUser
|
||||
{
|
||||
return \OC::$server->getUserSession()->getUser();
|
||||
}
|
||||
|
||||
public static function getUID(): string
|
||||
{
|
||||
return self::getUser()->getUID();
|
||||
}
|
||||
|
||||
public static function getYoutubeConfig($uid = null): array
|
||||
{
|
||||
$uid = $uid ?? self::getUID();
|
||||
$settings = new Settings($uid);
|
||||
$config = [
|
||||
'binary' => $settings->setType(Settings::TYPE['SYSTEM'])->get("ncd_yt_binary"),
|
||||
'downloadDir' => Helper::getRealDownloadDir(),
|
||||
'settings' => $settings->setType(Settings::TYPE['USER'])->getYoutube(),
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
public static function getAria2Config($uid = null): array
|
||||
{
|
||||
$options = [];
|
||||
$uid = $uid ?? self::getUID();
|
||||
$settings = new Settings($uid);
|
||||
$realDownloadDir = Helper::getRealDownloadDir($uid);
|
||||
$torrentsDir = Helper::getRealTorrentsDir($uid);
|
||||
$appPath = self::getAppPath();
|
||||
$dataDir = self::getDataDir();
|
||||
$aria2_dir = $dataDir . "/aria2";
|
||||
$options['seed_time'] = $settings->get("ncd_seed_time");
|
||||
$options['seed_ratio'] = $settings->get("ncd_seed_ratio");
|
||||
if (is_array($customSettings = $settings->getAria2())) {
|
||||
$options = array_merge($customSettings, $options);
|
||||
}
|
||||
$token = $settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_rpctoken');
|
||||
$config = [
|
||||
'dir' => $realDownloadDir,
|
||||
'torrents_dir' => $torrentsDir,
|
||||
'conf_dir' => $aria2_dir,
|
||||
'token' => $token,
|
||||
'settings' => $options,
|
||||
'binary' => $settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_aria2_binary'),
|
||||
'startHook' => $appPath . "/hooks/startHook.sh",
|
||||
'completeHook' => $appPath . "/hooks/completeHook.sh",
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
public static function getAppPath(): string
|
||||
{
|
||||
return \OC::$server->getAppManager()->getAppPath('ncdownloader');
|
||||
}
|
||||
public static function folderUpdated(string $dir):bool
|
||||
{
|
||||
if (!file_exists($dir)) {
|
||||
return false;
|
||||
}
|
||||
$checkFile = $dir . "/.lastmodified";
|
||||
if (!file_exists($checkFile)) {
|
||||
$time = \filemtime($dir);
|
||||
file_put_contents($checkFile, $time);
|
||||
return false;
|
||||
}
|
||||
$lastModified = (int) file_get_contents($checkFile);
|
||||
$time = \filemtime($dir);
|
||||
if ($time > $lastModified) {
|
||||
file_put_contents($checkFile, $time);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user