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

@@ -11,11 +11,10 @@ use OCA\NCDownloader\Tools\Youtube;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
//use OCP\Files\IRootFolder;
use OCP\IL10N;
//use OCP\Files\IRootFolder;
use OCP\IRequest;
use OC_Util;
use OC\Files\Filesystem;
class MainController extends Controller
{
@@ -57,7 +56,6 @@ class MainController extends Controller
// OC_Util::addStyle($this->appName, 'table');
$params = $this->buildParams();
$response = new TemplateResponse($this->appName, 'Index', $params);
return $response;
}
@@ -125,6 +123,10 @@ class MainController extends Controller
*/
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'));
if (Helper::isMagnet($url)) {
if ($this->disable_bt_nonadmin && !($this->isAdmin)) {

View File

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

View File

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

View File

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

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)