add youtube download, and look if I can fix the download progress ....

This commit is contained in:
Joe Tretter
2022-08-13 13:54:57 -05:00
parent 740cc1d908
commit f1e2ae93b5
2 changed files with 56 additions and 53 deletions

103
index.php
View File

@@ -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'])) {
</head>
<body>
<span style='font-size:40px'><span style='color:#777777'>Down</span>-<span style='color:#FF0000'>loader</span></span><span style='font-size:14px'> by <a href='mailto:joe@netsquat.com'>joe@netsquat.com</a></span>
<span style='font-size:40px'><span style='color:#777777'>Down</span>-<span style='color:#FF0000'>loader</span></span><span style='font-size:14px'> by <a href='mailto:joe@kawomi.com'>joe@kawomi.com</a></span>
<br>
<br>
<fieldset class='upload'><legend class='upload'>Upload local file</legend>
@@ -194,10 +167,14 @@ if (isset($_GET['kill'])) {
<br>
<fieldset class='upload'><legend class='upload'>Download remote url</legend>
<input name="url" id="url" size="100">
<button id='btnDownload' onclick="downloader.downloadURL(document.getElementById('url').value)" >Download</button>
<button id='btnDownload' onclick="downloader.downloadURL(document.getElementById('url').value,'url')" >Download</button>
</fieldset>
<fieldset class='upload'><legend class='upload'>Download youtube</legend>
<input name="ytdl" id="ytdl" size="100">
<button id='btnDownload' onclick="downloader.downloadURL(document.getElementById('ytdl').value,'ytdl')" >Download</button>
</fieldset>
<div id="DownloadProgressBars">
</div>
</fieldset>
<hr>
<table id="fileList" class='fileList' style="font-family:monospace">
<tbody>
@@ -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 <br />');
}
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 <br />');
}
/* 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){