From ce0ff3b3ac83a05c3b1993c5dc483d88f30a8bbb Mon Sep 17 00:00:00 2001 From: Moahmed-Ismail MEJRI Date: Wed, 23 Mar 2022 20:50:50 +0100 Subject: [PATCH] Update filename in the database according to the download extension file --- lib/Controller/YoutubeController.php | 15 +++++++++++++-- lib/Tools/Youtube.php | 9 +++++++-- lib/Tools/YoutubeHelper.php | 12 +++++++++--- src/App.vue | 6 +----- src/components/mainForm.vue | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/Controller/YoutubeController.php b/lib/Controller/YoutubeController.php index 549041a..a023525 100644 --- a/lib/Controller/YoutubeController.php +++ b/lib/Controller/YoutubeController.php @@ -18,6 +18,7 @@ class YoutubeController extends Controller private $settings = null; //@config OC\AppConfig private $l10n; + private $audio_extensions = array("mp3", "m4a", "vorbis"); public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Aria2 $aria2, Youtube $youtube) { @@ -79,10 +80,11 @@ class YoutubeController extends Controller $params = array(); $url = trim($this->request->getParam('text-input-value')); $yt = $this->youtube; - $yt->audioOnly = (bool) $this->request->getParam('audio-only'); - if ($yt->audioOnly) { + if (in_array($this->request->getParam('extension'), $this->audio_extensions)) { + $yt->audioOnly = TRUE; $yt->audioFormat = $this->request->getParam('extension'); } else { + $yt->audioOnly = FALSE; $yt->videoFormat = $this->request->getParam('extension'); } if (!$yt->isInstalled()) { @@ -154,6 +156,15 @@ class YoutubeController extends Controller $row = $this->dbconn->getByGid($gid); $data = $this->dbconn->getExtra($row["data"]); if (!empty($data['link'])) { + if (isset($data['ext'])) { + if (in_array($data['ext'], $this->audio_extensions)) { + $this->youtube->audioOnly = TRUE; + $this->youtube->audioFormat = $data['ext']; + } else { + $this->youtube->audioOnly = FALSE; + $this->youtube->videoFormat = $data['ext']; + } + } //$this->dbconn->deleteByGid($gid); $resp = $this->youtube->forceIPV4()->download($data['link']); folderScan::sync(); diff --git a/lib/Tools/Youtube.php b/lib/Tools/Youtube.php index 0623d58..912df4e 100644 --- a/lib/Tools/Youtube.php +++ b/lib/Tools/Youtube.php @@ -74,9 +74,9 @@ class Youtube } else { $this->audioFormat = "m4a"; } - $pos = strrpos($this->outTpl, '.'); + /*$pos = strrpos($this->outTpl, '.'); $this->outTpl = substr($this->outTpl, 0, $pos) . "." . $this->audioFormat; - //$this->outTpl = "/%(id)s-%(title)s.m4a"; + $this->outTpl = "/%(id)s-%(title)s.m4a";*/ $this->setAudioFormat($this->audioFormat); return $this; } @@ -145,6 +145,11 @@ class Youtube $process = new Process($this->options, null, $this->env); $process->setTimeout($this->timeout); $data = ['link' => $url]; + if ($this->audioOnly) { + $data['ext'] = $this->audioFormat; + } else { + $data['ext'] = $this->videoFormat; + } $process->run(function ($type, $buffer) use ($data, $process) { if (Process::ERR === $type) { $this->onError($buffer); diff --git a/lib/Tools/YoutubeHelper.php b/lib/Tools/YoutubeHelper.php index b8d66da..375cffc 100644 --- a/lib/Tools/YoutubeHelper.php +++ b/lib/Tools/YoutubeHelper.php @@ -30,7 +30,8 @@ class YoutubeHelper } public function getFilePath($output) { - $rules = '#\[download\]\s+Destination:\s+(?.*\.(?(mp4|mp3|aac|webm|m4a|ogg|3gp|mkv|wav|flv)))#i'; + $rules = '#\[(download|ExtractAudio|VideoConvertor|Merger)\]((\s+|\s+Converting.*;\s+)Destination:\s+|\s+Merging formats into\s+\")' . + '(?.*\.(?(mp4|mp3|aac|webm|m4a|ogg|3gp|mkv|wav|flv)))#i'; preg_match($rules, $output, $matches); @@ -70,9 +71,14 @@ class YoutubeHelper 'timestamp' => time(), 'data' => $extra, ]; + if (isset($this->file)) { + $sql = sprintf("UPDATE %s set filename = ? WHERE gid = ?", $this->tablename); + $this->dbconn->executeUpdate($sql, [basename($file), $this->gid]); + } else { + $this->dbconn->insert($data); + } //save the filename as this runs only once - $this->file = $file; - $this->dbconn->insert($data); + $this->file = basename($file); //$this->dbconn->save($data,[],['gid' => $this->gid]); } if (preg_match_all(self::PROGRESS_PATTERN, $buffer, $matches, PREG_SET_ORDER) !== false) { diff --git a/src/App.vue b/src/App.vue index 6f3a35c..2e5080e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -59,13 +59,9 @@ export default { let inputValue = formData["text-input-value"]; let message; if (formData.type === "youtube-dl") { - formData["audio-only"] = ""; formData["extension"] = ""; - if (formData["select-value-extension"] !== "Default") { + if (formData["select-value-extension"] !== "defaultext") { formData["extension"] = formData["select-value-extension"]; - if ((formData["select-value-extension"] === "mp3" ) || (formData["select-value-extension"] === "m4a" ) || (formData["select-value-extension"] === "vorbis" ) ) { - formData["audio-only"] = "true"; - } } message = helper.t("Download task started!"); } diff --git a/src/components/mainForm.vue b/src/components/mainForm.vue index 0e205df..6452054 100644 --- a/src/components/mainForm.vue +++ b/src/components/mainForm.vue @@ -28,7 +28,7 @@