diff options
author | Aaron Parecki <aaron@parecki.com> | 2016-05-11 17:47:17 +0200 |
---|---|---|
committer | Aaron Parecki <aaron@parecki.com> | 2016-05-11 17:47:17 +0200 |
commit | 542aa812f8606dad16ab456c3e5da438cc501644 (patch) | |
tree | 1a5b071eda488c11d6958a8c6cde83a5e09c8d4c /controllers | |
parent | 29f0c9b0543cdbf7780ce6e45204bd62a4ba4f52 (diff) |
support media endpoint, autosave notes in local storage
* looks for a media endpoint in the micropub config
* if media endpoint is available, both the note interface and the editor will upload files to it instead of posting the photo directly
* the note interface autosaves in-progress notes in localstorage
Diffstat (limited to 'controllers')
-rw-r--r-- | controllers/auth.php | 4 | ||||
-rw-r--r-- | controllers/controllers.php | 28 | ||||
-rw-r--r-- | controllers/editor.php | 33 |
3 files changed, 51 insertions, 14 deletions
diff --git a/controllers/auth.php b/controllers/auth.php index 748f7ad..c6c4ad8 100644 --- a/controllers/auth.php +++ b/controllers/auth.php @@ -212,9 +212,9 @@ $app->get('/auth/callback', function() use($app) { $user->save(); $_SESSION['user_id'] = $user->id(); - // Make a request to the micropub endpoint to discover the syndication targets if any. + // Make a request to the micropub endpoint to discover the syndication targets and media endpoint if any. // Errors are silently ignored here. The user will be able to retry from the new post interface and get feedback. - get_syndication_targets($user); + get_micropub_config($user); } unset($_SESSION['auth_state']); diff --git a/controllers/controllers.php b/controllers/controllers.php index 4373fcf..2a90892 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -76,6 +76,7 @@ $app->get('/new', function() use($app) { 'title' => 'New Post', 'in_reply_to' => $in_reply_to, 'micropub_endpoint' => $user->micropub_endpoint, + 'media_endpoint' => $user->micropub_media_endpoint, 'micropub_scope' => $user->micropub_scope, 'micropub_access_token' => $user->micropub_access_token, 'response_date' => $user->last_micropub_response_date, @@ -452,7 +453,7 @@ $app->post('/repost', function() use($app) { $app->get('/micropub/syndications', function() use($app) { if($user=require_login($app)) { - $data = get_syndication_targets($user); + $data = get_micropub_config($user, ['q'=>'syndicate-to']); $app->response()->body(json_encode(array( 'targets' => $data['targets'], 'response' => $data['response'] @@ -522,6 +523,31 @@ $app->post('/micropub/multipart', function() use($app) { } }); +$app->post('/micropub/media', function() use($app) { + if($user=require_login($app)) { + $file = isset($_FILES['photo']) ? $_FILES['photo'] : null; + $error = validate_photo($file); + unset($_POST['null']); + + if(!$error) { + $file_path = $file['tmp_name']; + correct_photo_rotation($file_path); + $r = micropub_media_post_for_user($user, $file_path); + } else { + $r = array('error' => $error); + } + + if(empty($r['location']) && empty($r['error'])) { + $r['error'] = "No 'Location' header in response."; + } + + $app->response()->body(json_encode(array( + 'location' => (isset($r['location']) ? $r['location'] : null), + 'error' => (isset($r['error']) ? $r['error'] : null), + ))); + } +}); + $app->post('/micropub/postjson', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); diff --git a/controllers/editor.php b/controllers/editor.php index a3c0496..fbb72cf 100644 --- a/controllers/editor.php +++ b/controllers/editor.php @@ -35,19 +35,30 @@ $app->post('/editor/publish', function() use($app) { }); $app->post('/editor/upload', function() use($app) { - // Fake a file uploader by echo'ing back the data URI - $fn = $_FILES['files']['tmp_name'][0]; - $imageData = base64_encode(file_get_contents($fn)); - $src = 'data:'.mime_content_type($fn).';base64,'.$imageData; + if($user=require_login($app)) { + $fn = $_FILES['files']['tmp_name'][0]; + $imageURL = false; - $app->response()['Content-type'] = 'application/json'; - $app->response()->body(json_encode([ - 'files' => [ - [ - 'url'=>$src + if($user->micropub_media_endpoint) { + // If the user has a media endpoint, upload to that and return that URL + correct_photo_rotation($fn); + $r = micropub_media_post_for_user($user, $fn); + if(!empty($r['location'])) { + $imageURL = $r['location']; + } + } + if(!$imageURL) { + // Otherwise, fake a file uploader by echo'ing back the data URI + $imageData = base64_encode(file_get_contents($fn)); + $imageURL = 'data:'.mime_content_type($fn).';base64,'.$imageData; + } + $app->response()['Content-type'] = 'application/json'; + $app->response()->body(json_encode([ + 'files' => [ + ['url'=>$imageURL] ] - ] - ])); + ])); + } }); $app->post('/editor/delete-file', function() use($app) { |