From dd245f40de4ba5168b0147c0589813b6f69a7b1a Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 25 Apr 2017 19:17:50 +0800 Subject: [PATCH] support remote file fetching --- public/main.js | 17 ++++++++++++++++- upload.php | 20 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/public/main.js b/public/main.js index c803bd0..98fc6cd 100644 --- a/public/main.js +++ b/public/main.js @@ -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); + } + }); } } } diff --git a/upload.php b/upload.php index b4992ed..5ee9962 100644 --- a/upload.php +++ b/upload.php @@ -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);