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

@@ -19,11 +19,9 @@ class Application extends App
public function __construct(array $urlParams = array()) public function __construct(array $urlParams = array())
{ {
parent::__construct('ncdownloader', $urlParams); parent::__construct('ncdownloader', $urlParams);
$user = \OC::$server->getUserSession()->getUser(); $user = Helper::getUser();
$this->uid = ($user) ? $user->getUID() : ''; $this->uid = ($user) ? $user->getUID() : '';
$this->settings = new Settings($this->uid); $this->settings = new Settings($this->uid);
$this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory');
$this->appPath = \OC::$server->getAppManager()->getAppPath('ncdownloader');
$this->userFolder = Helper::getUserFolder($this->uid); $this->userFolder = Helper::getUserFolder($this->uid);
$container = $this->getContainer(); $container = $this->getContainer();
$container->registerService('UserId', function (IContainer $container) { $container->registerService('UserId', function (IContainer $container) {
@@ -31,15 +29,11 @@ class Application extends App
}); });
$container->registerService('Aria2', function (IContainer $container) { $container->registerService('Aria2', function (IContainer $container) {
return new Aria2($this->getConfig()); return new Aria2(Helper::getAria2Config($this->uid));
}); });
$container->registerService('Youtube', function (IContainer $container) { $container->registerService('Youtube', function (IContainer $container) {
$config = [ $config = Helper::getYoutubeConfig($this->uid);
'binary' => $this->settings->setType(Settings::TYPE['SYSTEM'])->get("ncd_yt_binary"),
'downloadDir' => $this->getRealDownloadDir(),
'settings' => $this->settings->setType(Settings::TYPE['USER'])->getYoutube(),
];
return new Youtube($config); return new Youtube($config);
}); });
@@ -100,42 +94,4 @@ class Application extends App
} }
} }
private function getRealDownloadDir()
{
//relative nextcloud user path
$dir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads";
return $this->dataDir . $this->userFolder . $dir;
}
private function getRealTorrentsDir()
{
$dir = $this->settings->get('ncd_torrents_dir') ?? "/Torrents";
return $this->dataDir . $this->userFolder . $dir;
}
private function getConfig()
{
//$this->config = \OC::$server->getAppConfig();
$realDownloadDir = $this->getRealDownloadDir();
$torrentsDir = $this->getRealTorrentsDir();
$aria2_dir = $this->dataDir . "/aria2";
$settings['seed_time'] = $this->settings->get("ncd_seed_time");
$settings['seed_ratio'] = $this->settings->get("ncd_seed_ratio");
if (is_array($customSettings = $this->settings->getAria2())) {
$settings = array_merge($customSettings, $settings);
}
$token = $this->settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_rpctoken');
$config = [
'dir' => $realDownloadDir,
'torrents_dir' => $torrentsDir,
'conf_dir' => $aria2_dir,
'token' => $token,
'settings' => $settings,
'binary' => $this->settings->setType(Settings::TYPE['SYSTEM'])->get('ncd_aria2_binary'),
'startHook' => $this->appPath . "/hooks/startHook.sh",
'completeHook' => $this->appPath . "/hooks/completeHook.sh",
];
return $config;
}
} }

View File

@@ -11,11 +11,10 @@ use OCA\NCDownloader\Tools\Youtube;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
//use OCP\Files\IRootFolder;
use OCP\IL10N; use OCP\IL10N;
//use OCP\Files\IRootFolder;
use OCP\IRequest; use OCP\IRequest;
use OC_Util; use OC_Util;
use OC\Files\Filesystem;
class MainController extends Controller class MainController extends Controller
{ {
@@ -57,7 +56,6 @@ class MainController extends Controller
// OC_Util::addStyle($this->appName, 'table'); // OC_Util::addStyle($this->appName, 'table');
$params = $this->buildParams(); $params = $this->buildParams();
$response = new TemplateResponse($this->appName, 'Index', $params); $response = new TemplateResponse($this->appName, 'Index', $params);
return $response; return $response;
} }
@@ -125,6 +123,10 @@ class MainController extends Controller
*/ */
public function Download() public function Download()
{ {
$dlDir = $this->aria2->getDownloadDir();
if (!is_writable($dlDir)) {
return new JSONResponse(['error' => sprintf("%s is not writable", $dlDir)]);
}
$url = trim($this->request->getParam('text-input-value')); $url = trim($this->request->getParam('text-input-value'));
if (Helper::isMagnet($url)) { if (Helper::isMagnet($url)) {
if ($this->disable_bt_nonadmin && !($this->isAdmin)) { if ($this->disable_bt_nonadmin && !($this->isAdmin)) {

View File

@@ -76,7 +76,10 @@ class YoutubeController extends Controller
*/ */
public function Download() public function Download()
{ {
$params = array(); $dlDir = $this->youtube->getDownloadDir();
if (!is_writable($dlDir)) {
return new JSONResponse(['error' => sprintf("%s is not writable", $dlDir)]);
}
$url = trim($this->request->getParam('text-input-value')); $url = trim($this->request->getParam('text-input-value'));
$yt = $this->youtube; $yt = $this->youtube;
$yt->audioOnly = (bool) $this->request->getParam('audio-only'); $yt->audioOnly = (bool) $this->request->getParam('audio-only');
@@ -117,7 +120,7 @@ class YoutubeController extends Controller
} }
$row = $this->dbconn->getByGid($gid); $row = $this->dbconn->getByGid($gid);
$data = $this->dbconn->getExtra($value["data"]);; $data = $this->dbconn->getExtra($row["data"]);
if (!isset($data['pid'])) { if (!isset($data['pid'])) {
if ($this->dbconn->deleteByGid($gid)) { if ($this->dbconn->deleteByGid($gid)) {
$msg = sprintf("%s is deleted from database!", $gid); $msg = sprintf("%s is deleted from database!", $gid);
@@ -193,9 +196,9 @@ class YoutubeController extends Controller
private function installYTD() private function installYTD()
{ {
try { try {
$filename = Helper::getFileName($yt->installUrl()); $filename = Helper::getFileName($this->installUrl());
$yt->setDownloadDir($this->dataDir . "/bin"); $this->setDownloadDir($this->dataDir . "/bin");
$resp = $this->Save($yt->installUrl(), $filename); $resp = $this->Save($this->installUrl(), $filename);
return $resp; return $resp;
} catch (\Exception $e) { } catch (\Exception $e) {
return ['error' => $e->getMessage()]; return ['error' => $e->getMessage()];

View File

@@ -33,7 +33,6 @@ class TPB extends searchBase implements searchInterface
if ($this->content) { if ($this->content) {
return $this->content; return $this->content;
} }
$content;
try { try {
$response = $this->client->request('GET', $this->searchUrl); $response = $this->client->request('GET', $this->searchUrl);
$content = $response->getContent(); $content = $response->getContent();

View File

@@ -37,7 +37,6 @@ class bitSearch extends searchBase implements searchInterface
if ($this->content) { if ($this->content) {
return $this->content; return $this->content;
} }
$content;
try { try {
$response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]); $response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]);
$content = $response->getContent(); $content = $response->getContent();

View File

@@ -2,9 +2,13 @@
namespace OCA\NCDownloader\Tools; namespace OCA\NCDownloader\Tools;
use Exception;
use OCA\NCDownloader\Search\Sites\searchInterface; use OCA\NCDownloader\Search\Sites\searchInterface;
use OCA\NCDownloader\Tools\aria2Options; use OCA\NCDownloader\Tools\aria2Options;
use OCA\NCDownloader\Tools\Settings;
use OCP\IUser;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC_Util;
class Helper class Helper
{ {
@@ -135,6 +139,9 @@ class Helper
public static function debug($msg) public static function debug($msg)
{ {
if (is_array($msg)) {
$msg = implode(",", $msg);
}
$logger = \OC::$server->getLogger(); $logger = \OC::$server->getLogger();
$logger->error($msg, ['app' => 'ncdownloader']); $logger->error($msg, ['app' => 'ncdownloader']);
} }
@@ -279,14 +286,13 @@ class Helper
]; ];
return $titles[$type]; return $titles[$type];
} }
// the relative home folder of a nextcloud user // the relative home folder of a nextcloud user,e.g. /admin/files
public static function getUserFolder($uid = null) public static function getUserFolder($uid = null): string
{ {
if (!empty($rootFolder = Filesystem::getRoot())) { if (!empty($rootFolder = Filesystem::getRoot())) {
return $rootFolder; return $rootFolder;
} else if (isset($uid)) { } else if (isset($uid)) {
return "/" . $uid . "/files"; return "/" . $uid . "/files";
} }
return ''; return '';
} }
@@ -374,4 +380,110 @@ class Helper
return $sites; 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() public function getDownloadDir()
{ {
return $this->getDownloadDir; return $this->downloadDir;
} }
public function prependOption(string $option) public function prependOption(string $option)

View File

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