added support for updating yt-dlp binary;moved version info display to admin section

This commit is contained in:
benson
2023-05-05 21:56:16 +08:00
parent 967f05061a
commit 1059d8a4bc
15 changed files with 264 additions and 61 deletions

View File

@@ -412,8 +412,14 @@ class Aria2
{
return $this->bin;
}
public function version(){
public function version()
{
$resp = $this->getVersion();
return $resp['result']['version'] ?? null;
}
public function install()
{
$url = "https://github.com/shiningw/ncdownloader-bin/raw/master/aria2c";
Helper::Download($url, $this->bin);
}
}

View File

@@ -226,4 +226,14 @@ class MainController extends Controller
$counter = $this->counters->getCounters();
return new JSONResponse(['counter' => $counter]);
}
public function ytdlCheck()
{
$resp = $this->ytdl->check();
return new JSONResponse($resp);
}
public function ytdlUPdate(){
$resp = $this->ytdl->update();
return new JSONResponse($resp);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace OCA\NCDownloader\http;
namespace OCA\NCDownloader\Http;
//require __DIR__ . "/../../vendor/autoload.php";
use Symfony\Component\HttpClient\HttpClient;
@@ -8,12 +8,12 @@ use Symfony\Component\HttpClient\HttpClient;
final class Client
{
private $client;
public function __construct(?array $options = null)
public function __construct(?array $options = [])
{
$this->client = HttpClient::create($this->configure($options));
}
public static function create(?array $options = null)
public static function create(?array $options =[])
{
return new self($options);
}
@@ -26,8 +26,7 @@ final class Client
private function defaultOptions(): array
{
$settings = [
'headers' => [
],
'headers' => [],
'extra' => ['curl' => null],
];
return $settings;

View File

@@ -39,7 +39,8 @@ class Admin implements ISettings
$settings = Helper::getAllAdminSettings();
$settings += [
"path" => "/apps/ncdownloader/admin/save",
"aria2_version" => Helper::getAria2Version(),
"ytdl_version" => Helper::getYtdlVersion(),
];
$parameters = [
'settings' => $settings,

View File

@@ -50,8 +50,6 @@ class Personal implements ISettings
"path" => $path,
"disallow_aria2_settings" => Helper::getAdminSettings("disallow_aria2_settings"),
"is_admin" => \OC_User::isAdminUser($this->uid),
"aria2_version" => Helper::getAria2Version(),
"ytdl_version" => Helper::getYtdlVersion(),
],
"options" => [
[

View File

@@ -12,6 +12,9 @@ use OC_Util;
use Psr\Log\LoggerInterface;
use OCA\NCDownloader\Aria2\Aria2;
use OCA\NCDownloader\Ytdl\Ytdl;
use OCA\NCDownloader\Http\Client;
require __DIR__ . "/../../vendor/autoload.php";
class Helper
{
@@ -592,4 +595,52 @@ class Helper
];
return $options;
}
public static function getLatestRelease($owner, $repo)
{
$client = Client::create();
$response = $client->request('GET', "https://api.github.com/repos/$owner/$repo/releases/latest", [
'headers' => [
'User-Agent' => 'PHP'
]
]);
$data = json_decode($response->getContent(), true);
if (isset($data['tag_name'])) {
return $data['tag_name'];
}
return null;
}
public static function downloadLatestRelease($owner, $repo, $file)
{
$client = Client::create(['max_redirects' => 10]);
$response = $client->request('GET', "https://api.github.com/repos/$owner/$repo/releases/latest", [
'headers' => [
'User-Agent' => 'PHP'
]
]);
$data = json_decode($response->getContent(), true);
$downloadUrl = null;
foreach ($data['assets'] as $asset) {
if ($asset['name'] == $repo) {
$downloadUrl = $asset['browser_download_url'];
break;
}
}
if ($downloadUrl) {
if (!is_writable(dirname($file))) {
throw new \Exception(dirname($file) . " is not writable");
}
$response = $client->request('GET', $downloadUrl);
if ($byte = file_put_contents($file, $response->getContent())) {
return $byte;
}else {
throw new \Exception("Failed to download $downloadUrl");
}
}
return false;
}
public static function removeLetters($str)
{
return preg_replace('/[^0-9.]+/', '', $str);
}
}

View File

@@ -271,8 +271,31 @@ class Ytdl
$process = new Process([$this->bin, '--version']);
$process->run();
if ($process->isSuccessful()) {
return $process->getOutput();
//remove any new line
return trim($process->getOutput());
}
return false;
}
public function check()
{
if ($tagName = Helper::getLatestRelease('yt-dlp', 'yt-dlp')) {
$tagName = Helper::removeLetters($tagName);
$version = $this->version();
if ($version && version_compare($version, $tagName, '<')) {
return ['status' => true, 'message' => $tagName];
}
}
return ['status' => false, 'message' => 'No update available'];
}
public function update()
{
$file = __DIR__ . "/../../bin/yt-dlp";
try {
Helper::downloadLatestRelease('yt-dlp', 'yt-dlp', $file);
chmod($file, 0744);
} catch (\Exception $e) {
return ['status' => false,'message' => $e->getMessage()];
}
return ['status' => true, 'message' => 'Updated to latest version','data' => $this->version()];
}
}