Browse Source

use `file` instead of `photo` for media endpoint

pull/10/head
Aaron Parecki 8 years ago
parent
commit
a67a84a8ba
3 changed files with 22 additions and 7 deletions
  1. +11
    -1
      controllers/controllers.php
  2. +5
    -4
      lib/helpers.php
  3. +6
    -2
      views/new-post.php

+ 11
- 1
controllers/controllers.php View File

@ -291,7 +291,7 @@ $app->post('/post', function() use($app) {
$app->post('/micropub/media', function() use($app) { $app->post('/micropub/media', function() use($app) {
if($user=require_login($app)) { if($user=require_login($app)) {
$file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
$file = isset($_FILES['file']) ? $_FILES['file'] : null;
$error = validate_photo($file); $error = validate_photo($file);
unset($_POST['null']); unset($_POST['null']);
@ -309,16 +309,26 @@ $app->post('/micropub/media', function() use($app) {
$url = trim($match[1]); $url = trim($match[1]);
} else { } else {
$r['error'] = "No 'Location' header in response."; $r['error'] = "No 'Location' header in response.";
$r['debug'] = $response;
} }
$app->response()['Content-type'] = 'application/json'; $app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array( $app->response()->body(json_encode(array(
'location' => $url, 'location' => $url,
'error' => (isset($r['error']) ? $r['error'] : null), '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) { $app->get('/options.json', function() use($app) {
if($user=require_login($app)) { if($user=require_login($app)) {
$params = $app->request()->params(); $params = $app->request()->params();

+ 5
- 4
lib/helpers.php View File

@ -132,13 +132,14 @@ function micropub_media_post($endpoint, $access_token, $file) {
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
$post = [ $post = [
'photo' => new CURLFile($file)
'file' => new CURLFile($file)
]; ];
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch); $response = curl_exec($ch);
$error = curl_error($ch); $error = curl_error($ch);
$sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT); $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); $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(); $user->save();
} }
@ -200,7 +201,7 @@ function build_static_map_url($latitude, $longitude, $height, $width, $zoom) {
} }
function static_map_service($query) { function static_map_service($query) {
return 'https://atlas.p3k.io/map/img?' . $query;
return 'http://atlas.dev/map/img?' . $query;
} }
function relative_time($date) { function relative_time($date) {

+ 6
- 2
views/new-post.php View File

@ -240,21 +240,25 @@ $(function(){
var formData = new FormData(); var formData = new FormData();
formData.append("null","null"); formData.append("null","null");
formData.append("photo", e.target.files[0]);
formData.append("file", e.target.files[0]);
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open("POST", "/micropub/media"); request.open("POST", "/micropub/media");
request.onreadystatechange = function() { request.onreadystatechange = function() {
if(request.readyState == XMLHttpRequest.DONE) { if(request.readyState == XMLHttpRequest.DONE) {
try { try {
var response = JSON.parse(request.responseText); var response = JSON.parse(request.responseText);
console.log(response);
if(response.location) { if(response.location) {
// Replace the file upload form with the URL // Replace the file upload form with the URL
replacePhotoWithPhotoURL(response.location); replacePhotoWithPhotoURL(response.location);
saveNoteState();
} else { } else {
console.log("Endpoint did not return a location header", response); console.log("Endpoint did not return a location header", response);
$("#remove_photo").click();
$("#note_photo_loading").addClass("hidden");
} }
} catch(e) { } catch(e) {
$("#remove_photo").click();
$("#note_photo_loading").addClass("hidden");
console.log(e); console.log(e);
} }
} }

Loading…
Cancel
Save