From d71390c1170a518a7ed6daae57cba4fd53419f11 Mon Sep 17 00:00:00 2001 From: Christian Haschek Date: Mon, 2 Nov 2015 19:11:50 +0100 Subject: [PATCH] added support for POST upload via API --- README.md | 14 ++++++++++++-- backend.php | 6 +++++- models/pictsharemodel.php | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) mode change 100755 => 100644 README.md diff --git a/README.md b/README.md old mode 100755 new mode 100644 index 5739f1c..6707f9c --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ You can also combine as many options as you want. Even multiple times! Want your ## How does the external-upload-API work? -### Upload from URL +### Upload from external URL PictShare has a simple REST API to upload remote pictures. The API can be accessed via the backend.php file like this: ```https://pictshare.net/backend.php?getimage=```. @@ -70,8 +70,18 @@ Request: ```https://pictshare.net/backend.php?getimage=https://www.0xf.at/css/im The server will answer with the file name and the server path in JSON: +```json +{"status":"OK","type":"png","hash":"10ba188162.png","url":"https:\/\/pictshare.net\/10ba188162.png"} ``` -{"status":"OK","type":"png","hash":"10ba188162.png","url":"http:\/\/pictshare.net\/10ba188162.png"} + +### Upload via POST + +Send a POST request to ```https://pictshare.net/backend.php``` and send the image in the variable ```postimage```. + +Server will return JSON of uploaded data like this: + +```json +{"status":"OK","type":"png","hash":"2f18a052c4.png","url":"https:\/\/pictshare.net\/2f18a052c4.png","domain":"https:\/\/pictshare.net\/"} ``` ### Upload from base64 string diff --git a/backend.php b/backend.php index 40b5426..119fece 100644 --- a/backend.php +++ b/backend.php @@ -29,6 +29,11 @@ if($_REQUEST['getimage']) echo json_encode($pm->uploadImageFromURL($url)); } +if($_FILES['postimage']) +{ + $image = $_FILES['postimage']; + echo json_encode($pm->processSingleUpload($file,'postimage')); +} else if($_REQUEST['base64']) { $data = $_REQUEST['base64']; @@ -37,6 +42,5 @@ else if($_REQUEST['base64']) } else if($_REQUEST['getsize']) echo json_encode($pm->getSizeOfURL($_REQUEST['getsize'])); - else echo json_encode(array('status'=>'ERR')); diff --git a/models/pictsharemodel.php b/models/pictsharemodel.php index 8bf6d58..20bb1f3 100644 --- a/models/pictsharemodel.php +++ b/models/pictsharemodel.php @@ -315,6 +315,27 @@ class PictshareModel extends Model return false; } + + function processSingleUpload($file,$name) + { + if(UPLOAD_CODE!=false && !$pm->uploadCodeExists($_REQUEST['upload_code'])) + exit(json_encode(array('status'=>'ERR','reason'=>'Wrong upload code provided'))); + + $im = new Image(); + $i = 0; + if ($_FILES[$name]["error"] == UPLOAD_ERR_OK) + { + $data = $this->uploadImageFromURL($_FILES[$name]["tmp_name"]); + if($data['status']=='OK') + { + $hash = $data['hash']; + return array('status'=>'OK','type'=>$type,'hash'=>$hash,'url'=>DOMAINPATH.$hash,'domain'=>DOMAINPATH); + } + } + + + return $o; + } function ProcessUploads() {