fixed #47;some cleaning up and bugfixes;

This commit is contained in:
huangjx
2022-04-22 16:47:31 +08:00
parent aa1bf42ccc
commit 73d01e558f
8 changed files with 138 additions and 86 deletions

View File

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

View File

@@ -116,7 +116,7 @@ class Youtube
public function getDownloadDir()
{
return $this->getDownloadDir;
return $this->downloadDir;
}
public function prependOption(string $option)

View File

@@ -10,19 +10,18 @@ class folderScan
{
private $user;
private $path;
private $realDir;
public function __construct($path = null, $user = null)
{
$this->user = $user ?? \OC::$server->getUserSession()->getUser()->getUID();
$this->user = $user ?? Helper::getUID();
$this->path = $path ?? $this->getDefaultPath();
$this->realDir = \OC::$server->getSystemConfig()->getValue('datadirectory') . "/" . $this->path;
$this->realDir = $realDir ?? Helper::getLocalFolder($this->path);
}
public function getDefaultPath()
{
$settings = new Settings($this->user);
$rootFolder = Helper::getUserFolder($this->user);
$downloadDir = $settings->get('ncd_downloader_dir') ?? "/Downloads";
return $rootFolder . "/" . ltrim($downloadDir, '/\\');
return $settings->get('ncd_downloader_dir') ?? "/Downloads";
}
public static function create($path = null, $user = null)
{
@@ -42,7 +41,7 @@ class folderScan
private function update()
{
if (!(self::folderUpdated($this->realDir))) {
if (!(Helper::folderUpdated($this->realDir))) {
return ['message' => "no change"];
}
$this->scan();
@@ -65,25 +64,7 @@ class folderScan
return ['status' => $e->getMessage(), 'path' => $this->path];
}
public static function folderUpdated($dir)
{
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;
}
//update only folder is modified
public static function sync($path = null, $user = null)