From a67a84a8ba52eae557793fd786ee9526e59c6e17 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 5 Jun 2016 11:53:08 -0700 Subject: [PATCH] use `file` instead of `photo` for media endpoint --- controllers/controllers.php | 12 +++++++++++- lib/helpers.php | 9 +++++---- views/new-post.php | 8 ++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/controllers/controllers.php b/controllers/controllers.php index 4a62a5b..654bd9f 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -291,7 +291,7 @@ $app->post('/post', function() use($app) { $app->post('/micropub/media', function() use($app) { if($user=require_login($app)) { - $file = isset($_FILES['photo']) ? $_FILES['photo'] : null; + $file = isset($_FILES['file']) ? $_FILES['file'] : null; $error = validate_photo($file); unset($_POST['null']); @@ -309,16 +309,26 @@ $app->post('/micropub/media', function() use($app) { $url = trim($match[1]); } else { $r['error'] = "No 'Location' header in response."; + $r['debug'] = $response; } $app->response()['Content-type'] = 'application/json'; $app->response()->body(json_encode(array( 'location' => $url, 'error' => (isset($r['error']) ? $r['error'] : null), + 'debug' => (isset($r['debug']) ? $r['debug'] : null), ))); } }); +$app->get('/micropub/config', function() use($app) { + if($user=require_login($app)) { + $config = get_micropub_config($user); + $app->response()['Content-type'] = 'application/json'; + $app->response()->body(json_encode($config)); + } +}); + $app->get('/options.json', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); diff --git a/lib/helpers.php b/lib/helpers.php index 97a90d7..786a937 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -132,13 +132,14 @@ function micropub_media_post($endpoint, $access_token, $file) { curl_setopt($ch, CURLOPT_POST, true); $post = [ - 'photo' => new CURLFile($file) + 'file' => new CURLFile($file) ]; curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 20); $response = curl_exec($ch); $error = curl_error($ch); $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT); @@ -185,8 +186,8 @@ function get_micropub_config(&$user) { $r = micropub_get($user->micropub_endpoint, [], $user->access_token); - if(array_key_exists('media_endpoint', $r['data'])) { - $user->micropub_media_endpoint = $r['data']['media_endpoint']; + if(array_key_exists('media-endpoint', $r['data'])) { + $user->micropub_media_endpoint = $r['data']['media-endpoint']; $user->save(); } @@ -200,7 +201,7 @@ function build_static_map_url($latitude, $longitude, $height, $width, $zoom) { } function static_map_service($query) { - return 'https://atlas.p3k.io/map/img?' . $query; + return 'http://atlas.dev/map/img?' . $query; } function relative_time($date) { diff --git a/views/new-post.php b/views/new-post.php index e5ad04b..dbf4c44 100644 --- a/views/new-post.php +++ b/views/new-post.php @@ -240,21 +240,25 @@ $(function(){ var formData = new FormData(); formData.append("null","null"); - formData.append("photo", e.target.files[0]); + formData.append("file", e.target.files[0]); var request = new XMLHttpRequest(); request.open("POST", "/micropub/media"); request.onreadystatechange = function() { if(request.readyState == XMLHttpRequest.DONE) { try { var response = JSON.parse(request.responseText); + console.log(response); if(response.location) { // Replace the file upload form with the URL replacePhotoWithPhotoURL(response.location); - saveNoteState(); } else { console.log("Endpoint did not return a location header", response); + $("#remove_photo").click(); + $("#note_photo_loading").addClass("hidden"); } } catch(e) { + $("#remove_photo").click(); + $("#note_photo_loading").addClass("hidden"); console.log(e); } }