added delete option for youtube-dl download;added options for copying links;

This commit is contained in:
huangjx
2022-02-19 00:44:45 +08:00
parent d25e0c1ec1
commit 06e1588d06
13 changed files with 219 additions and 23 deletions

View File

@@ -50,18 +50,18 @@ class YoutubeController extends Controller
$folderLink = $this->urlGenerator->linkToRoute('files.view.index', $params);
foreach ($data as $value) {
$tmp = [];
$extra = unserialize($value['data']);
$filename = sprintf('<a class="download-file-folder" href="%s">%s</a>', $folderLink, $value['filename']);
$fileInfo = sprintf("%s | %s", $value['filesize'], date("Y-m-d H:i:s", $value['timestamp']));
$fileInfo = sprintf('<div class="ncd-file-info"><button id="icon-clipboard" class="icon-clipboard" data-text="%s"></button> %s | % s</div>', $extra['link'], $value['filesize'], date("Y-m-d H:i:s", $value['timestamp']));
$tmp['filename'] = array($filename, $fileInfo);
$tmp['speed'] = explode("|", $value['speed']);
$tmp['progress'] = $value['progress'];
if ((int) $value['status'] == Helper::STATUS['COMPLETE']) {
$path = $this->urlGenerator->linkToRoute('ncdownloader.Youtube.Delete');
$tmp['actions'][] = ['name' => 'delete', 'path' => $path];
} else {
$path = $this->urlGenerator->linkToRoute('ncdownloader.Youtube.Redownload');
$tmp['actions'][] = ['name' => 'refresh', 'path' => $path];
}
$path = $this->urlGenerator->linkToRoute('ncdownloader.Youtube.Delete');
$tmp['actions'][] = ['name' => 'delete', 'path' => $path];
$path = $this->urlGenerator->linkToRoute('ncdownloader.Youtube.Redownload');
$tmp['actions'][] = ['name' => 'refresh', 'path' => $path];
$tmp['data_gid'] = $value['gid'] ?? 0;
array_push($resp['row'], $tmp);
}
@@ -111,10 +111,30 @@ class YoutubeController extends Controller
return new JSONResponse(['error' => "no gid value is received!"]);
}
if ($this->dbconn->deleteByGid($gid)) {
return new JSONResponse(['message' => $gid . " Deleted!"]);
$row = $this->dbconn->getByGid($gid);
$data = unserialize($row['data']);
if (!isset($data['pid'])) {
if ($this->dbconn->deleteByGid($gid)) {
$msg = sprintf("%s is deleted from database!", $gid);
}
return new JSONResponse(['message' => $msg]);
}
$pid = $data['pid'];
if (!Helper::isRunning($pid)) {
if ($this->dbconn->deleteByGid($gid)) {
$msg = sprintf("%s is deleted from database!", $gid);
} else {
$msg = sprintf("process %d is not running!", $pid);
}
} else {
if (Helper::stop($pid)) {
$msg = sprintf("process %d has been terminated!", $pid);
} else {
$msg = sprintf("failed to terminate process %d!", $pid);
}
$this->dbconn->deleteByGid($gid);
}
return new JSONResponse(['message' => $msg]);
}
/**
* @NoAdminRequired
@@ -129,6 +149,7 @@ class YoutubeController extends Controller
$row = $this->dbconn->getByGid($gid);
$data = unserialize($row['data']);
if (!empty($data['link'])) {
//$this->dbconn->deleteByGid($gid);
$resp = $this->youtube->forceIPV4()->download($data['link']);
folderScan::sync();
return new JSONResponse($resp);

View File

@@ -46,7 +46,7 @@ class torrentSearch
if (!$value) {
continue;
}
$value['actions'][] = array("name" => 'download', 'path' => '/index.php/apps/ncdownloader/new');
$value['actions'] = [["name" => 'download', 'path' => '/index.php/apps/ncdownloader/new'], ['name' => 'clipboard']];
}
}

View File

@@ -43,6 +43,7 @@ class Helper
{
$host = parse_url($url, PHP_URL_HOST);
//$sites = ['twitter.com', 'www.twitter.com'];
$sites = [];
return (bool) (in_array($host, $sites));
}
public static function parseUrl($url)
@@ -134,7 +135,7 @@ class Helper
public static function debug($msg)
{
$logger = \OC::$server->getLogger();
$logger->debug($msg, ['app' => 'ncdownloader']);
$logger->error($msg, ['app' => 'ncdownloader']);
}
public static function log($msg, $file = "/tmp/nc.log")
@@ -298,4 +299,28 @@ class Helper
return filter_var($string, FILTER_SANITIZE_STRING);
}
public static function doSignal($pid, $signal): bool
{
if (\function_exists('posix_kill')) {
$ok = @posix_kill($pid, $signal);
} elseif ($ok = proc_open(sprintf('kill -%d %d', $signal, $pid), [2 => ['pipe', 'w']], $pipes)) {
$ok = false === fgets($pipes[2]);
}
if (!$ok) {
return false;
}
return true;
}
public static function isRunning($pid)
{
return self::doSignal($pid, 0);
}
public static function stop($pid)
{
return self::doSignal($pid, 9);
}
}

View File

@@ -154,11 +154,13 @@ class Youtube
//\OC::$server->getLogger()->error($process->getCommandLine(), ['app' => 'PHP']);
$process = new Process($this->options, null, $this->env);
$process->setTimeout($this->timeout);
$process->run(function ($type, $buffer) use ($url) {
$data = ['link' => $url];
$process->run(function ($type, $buffer) use ($data, $process) {
if (Process::ERR === $type) {
$this->onError($buffer);
// $this->onError($buffer);
} else {
$this->onOutput($buffer, $url);
$data['pid'] = $process->getPid();
$this->onOutput($buffer, $data);
}
});
if ($process->isSuccessful()) {
@@ -173,9 +175,9 @@ class Youtube
$this->helper->log($buffer);
}
public function onOutput($buffer, $url)
public function onOutput($buffer, $extra)
{
$this->helper->run($buffer, $url);
$this->helper->run($buffer, $extra);
}
public function getDownloadUrl($url)
{

View File

@@ -16,6 +16,7 @@ class YoutubeHelper
'(\s+in\s+(?<totalTime>[\d:]{2,8}))?#i';
public $file = null;
public $filesize = null;
protected $pid = 0;
public function __construct()
{
$this->dbconn = new DbHelper();
@@ -47,9 +48,13 @@ class YoutubeHelper
//$sql = sprintf("UPDATE %s set status = ? WHERE gid = ?", $this->tablename);
$this->dbconn->updateStatus($this->gid, $this->status);
}
public function run($buffer, $url)
public function setPid($pid)
{
$this->gid = Helper::generateGID($url);
$this->pid = $pid;
}
public function run($buffer, $extra)
{
$this->gid = Helper::generateGID($extra['link']);
$file = $this->getFilePath($buffer);
if ($file) {
$data = [
@@ -59,7 +64,7 @@ class YoutubeHelper
'filename' => basename($file),
'status' => Helper::STATUS['ACTIVE'],
'timestamp' => time(),
'data' => serialize(['link' => $url]),
'data' => serialize($extra),
];
//save the filename as this runs only once
$this->file = $file;