From f1e2ae93b57337379bd2ca2f9dd7c8665693d711 Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Sat, 13 Aug 2022 13:54:57 -0500 Subject: [PATCH] add youtube download, and look if I can fix the download progress .... --- index.php | 103 ++++++++++++++++++++++++++---------------------------- ytdl.sh | 6 ++++ 2 files changed, 56 insertions(+), 53 deletions(-) create mode 100644 ytdl.sh diff --git a/index.php b/index.php index ba06fc7..dcd192e 100644 --- a/index.php +++ b/index.php @@ -29,37 +29,6 @@ function readfile_chunked($filename, $retbytes = TRUE) { return $status; } -// write stdin to a file chunk by chunk -function writefile_chunked($filename, $retbytes = TRUE) { - $buffer = ''; - $cnt =0; - // $handle = fopen($filename, 'rb'); - $handleIn = fopen("php://input", 'rb'); - if ($handleIn === false) { - return false; - } - //$handleOut = fopen($filename, 'wb'); - $handleOut = fopen($filename, 'wb'); - if ($handleOut === false) { - return false; - } - while (!feof($handleIn)) { - $buffer = fread($handleIn, CHUNK_SIZE); - $retbytes=fwrite($handleOut,$buffer); - if ($retbytes) { - $cnt += strlen($buffer); - } - } - $statusOut = fclose($handleOut); - $statusIn = fclose($handleIn); - $status = $statusIn; - if ($retbytes && $statusOut) { - return $cnt; // return num. bytes written. - } - - return $status; -} - if (isset($_GET['dlFile'])){ # doesn't work -- always gets killed (out of memory?) header('Content-disposition: attachment; filename='. $_GET["dlFile"]); @@ -77,6 +46,11 @@ if (isset($_GET['url'])) { exit(0); } +if (isset($_GET['ytdl'])) { + system("cd '" . $FP . "';/var/www/dl/ytdl.sh '" . $_GET["ytdl"] . "'"); + exit(0); +} + if (isset($_GET['getFileList'])) { header('Content-type: application/json'); header("Pragma: no-cache"); @@ -96,8 +70,7 @@ if (isset($_SERVER['HTTP_X_FILENAME'])) { $fileName=$origFileName . "." . ++$i; } - //file_put_contents( $FP . $fileName, file_get_contents('php://input')); - writefile_chunked($FP . $fileName); + file_put_contents( $FP . $fileName, file_get_contents('php://input')); echo $fileName . " uploaded"; system("cd '" . $FP . "';/usr/bin/md5sum '" . $fileName . "' | /usr/bin/cut -c1-32 > '." . $fileName . ".md5'"); exit(0); @@ -179,7 +152,7 @@ if (isset($_GET['kill'])) { - Down-loader by joe@netsquat.com + Down-loader by joe@kawomi.com

Upload local file @@ -194,10 +167,14 @@ if (isset($_GET['kill'])) {
Download remote url - + +
+
Download youtube + + +
-

@@ -308,11 +285,11 @@ var downloader = (function () { fillFileList(); } - function downloadURL(url) { + function downloadURL(url,type) { var progressBarId=Math.random()*100000000000000000; $id("DownloadProgressBars").add(progressBarId,url) $id("DownloadProgressBars").setMax(progressBarId,100) - $id("url").value = ""; + $id(type).value = ""; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { @@ -321,23 +298,41 @@ var downloader = (function () { fillFileList(); } } - xhr.open("GET", "?url=" + encodeURIComponent(url), true); + // TODO: The progress update only fires at the end, used to work before.. have to look sure if it's server or client... + xhr.onprogress = function(event) { + // report progress + console.log(`Loaded ${event.loaded} of ${event.total}`); + var tmpResponse=event.currentTarget.response; + var lastIndex=tmpResponse.lastIndexOf("%"); + console.log(new Date(),tmpResponse,lastIndex); + if (lastIndex>0) { + var pctFinish=tmpResponse.substring(lastIndex-3,lastIndex); + //console.log(pctFinish,tmpResponse); + $id("DownloadProgressBars").setProgress(progressBarId,pctFinish); + } + }; + + xhr.addEventListener("progress",xhr.onprogress,false); + xhr.open("GET", "?" + type + "=" + encodeURIComponent(url), true); xhr.send(); + var timer; timer = window.setInterval(function() { - if (xhr.readyState === 4) { - window.clearTimeout(timer); - Output('UrlDownload done
'); - } - var tmpResponse=xhr.responseText; - var lastIndex=tmpResponse.lastIndexOf("%"); - if (lastIndex>0) { - var pctFinish=tmpResponse.substring(lastIndex-3,lastIndex); - $id("DownloadProgressBars").setProgress(progressBarId,pctFinish); - } - // console.log(xhr.responseText); - }, 300); + if (xhr.readyState === 4) { + window.clearTimeout(timer); + Output('UrlDownload done
'); + } + /* var tmpResponse=xhr.responseText; + var lastIndex=tmpResponse.lastIndexOf("%"); + console.log(tmpResponse,lastIndex); + if (lastIndex>0) { + var pctFinish=tmpResponse.substring(lastIndex-3,lastIndex); + //console.log(pctFinish,tmpResponse); + $id("DownloadProgressBars").setProgress(progressBarId,pctFinish); + } + console.log(xhr.responseText); */ + }, 300); } function UploadFile(file) { @@ -423,8 +418,10 @@ function setPct(id){ function setProgress(id,value){ current=value; - $id("progressUL" + id).value = value; - setPct(id); + if ($id("progressUL" + id) !== null) { + $id("progressUL" + id).value = value; + setPct(id); + } } function remove(id){ diff --git a/ytdl.sh b/ytdl.sh new file mode 100644 index 0000000..0d71241 --- /dev/null +++ b/ytdl.sh @@ -0,0 +1,6 @@ +#!/bin/bash +echo $1 >> /tmp/dlX +#/usr/bin/stdbuf -i0 -o0 -e0 /usr/local/bin/youtube-dl --newline -f 160 $1 2>&1 | cut -c 11-13 | while read x; do echo ${x}% $(seq 1 2094 | xargs /usr/bin/stdbuf -i0 -o0 -e0 echo); done +/usr/bin/stdbuf -i0 -o0 -e0 /usr/local/bin/youtube-dl --newline -f 160 $1 2>&1 | /usr/bin/stdbuf -i0 -o0 -e0 cut -c 11-14 | while read x +do /usr/bin/stdbuf -i0 -o0 -e0 printf "%3d%% .................................................................................................... \r\n" ${x} + done