support remote file fetching

This commit is contained in:
Kieran 2017-04-25 19:17:50 +08:00
parent 5690962bb8
commit dd245f40de
2 changed files with 34 additions and 3 deletions

View File

@ -166,6 +166,15 @@ function changeUI()
*/
function uploadFile(f, id)
{
if(typeof f === "string"){
var fx = new File([], 'remote');
fx.type = "text/plain";
fx.size = 0;
fx.url = f;
f = fx;
}
if(f instanceof Blob || f instanceof File)
{
if($('#' + id) === null){
@ -225,7 +234,7 @@ function uploadFile(f, id)
xhr.upload.addEventListener('abort', function(evt) { uploadProgress(evt, id); });
xhr.addEventListener('readystatechange', function(evt) { uploadProgress(evt, id); });
xhr.open("POST", "upload.php?filename=" + f.name);
xhr.open("POST", "upload.php?filename=" + f.name + (f.url !== undefined ? "&remote=" + encodeURIComponent(f.url) : ""));
xhr.send(f);
}
}
@ -285,6 +294,12 @@ function handleFilePaste(evt)
var file_t = file.getAsFile();
file_t.name = "clipboard.png";
uploadFile(file_t, fid);
}else if(file.kind === 'string' && file.type === 'text/plain'){
var file_t = file.getAsString(function(url){
if(url.indexOf('http://') === 0 || url.indexOf('https://') === 0) {
uploadFile(url);
}
});
}
}
}

View File

@ -24,8 +24,24 @@
}
else
{
$rawf = fopen('php://input', 'rb');
$tmpf = fopen('php://temp', 'rb+');
$source = isset($_GET["remote"]) ? $_GET["remote"] : "php://input";
$rawf = fopen($source, 'rb');
if(isset($_GET["remote"])){
$meta_data = stream_get_meta_data($rawf);
foreach($meta_data["wrapper_data"] as $hd){
if(strpos($hd, "Content-Type") === 0){
$nt = explode(": ", $hd);
$mime = $nt[1];
}else if(strpos($hd, "Content-Disposition") === 0){
$nn = explode("filename=", $hd);
$fname = str_replace("\"", "", $nn[1]);
}
}
}
$tmpf = fopen("php://temp", 'rb+');
stream_copy_to_stream($rawf, $tmpf);
rewind($tmpf);