Images can now be uploaded via the media endpoint in a basic way

This commit is contained in:
Jonny Barnes 2017-03-18 20:09:11 +00:00
parent 16015d3f4b
commit 764557b96b
10 changed files with 125 additions and 75 deletions

View file

@ -2,14 +2,12 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Services\IndieAuthService;
use Illuminate\Support\Facades\Log;
use IndieAuth\Client as IndieClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Http\{Request, Response};
use GuzzleHttp\Exception\{ClientException, ServerException};
class MicropubClientController extends Controller
{
@ -70,11 +68,14 @@ class MicropubClientController extends Controller
foreach ($request->file('file') as $file) {
try {
$response = $this->guzzleClient->request('POST', $mediaEndpoint, [
'headers' => ['Authorization' => 'Bearer ' . $token],
'mulitpart' => [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
'multipart' => [
[
'name' => $file->getClientOriginalName(),
'file' => fopen($file->path(), 'r'),
'name' => 'file',
'contents' => fopen($file->path(), 'r'),
'filename' => $file->getClientOriginalName(),
],
],
]);
@ -82,7 +83,7 @@ class MicropubClientController extends Controller
continue;
}
$mediaURLs[] = $response->header('Location');
$mediaURLs[] = $response->getHeader('Location')[0];
}
$request->session()->put('media-links', $mediaURLs);
@ -121,6 +122,7 @@ class MicropubClientController extends Controller
$response = $this->postNoteRequest($request, $micropubEndpoint, $token);
if ($response->getStatusCode() == 201) {
$request->session()->forget('media-links');
$location = $response->getHeader('Location');
if (is_array($location)) {
return redirect($location[0]);
@ -273,6 +275,14 @@ class MicropubClientController extends Controller
];
}
}
if ($request->input('media')) {
foreach ($request->input('media') as $media) {
$multipart[] = [
'name' => 'photo[]',
'contents' => $media,
];
}
}
$headers = [
'Authorization' => 'Bearer ' . $token,
];

View file

@ -2,15 +2,13 @@
namespace App\Http\Controllers;
use App\Media;
use App\Place;
use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid;
use Illuminate\Http\Request;
use App\Services\NoteService;
use Illuminate\Http\Response;
use App\Services\PlaceService;
use App\Services\TokenService;
use App\{Media, Place};
use Illuminate\Http\{Request, Response};
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use App\Services\{NoteService, PlaceService, TokenService};
class MicropubController extends Controller
{
@ -98,6 +96,18 @@ class MicropubController extends Controller
}
}
}
$data['photo'] = [];
Log::info('Checking request for photos');
Log::info($request->all());
if (is_array($request->input('photo'))) {
foreach ($request->input('photo') as $photo) {
if (is_string($photo)) {
Log::info($photo);
//only supporting media URLs for now
$data['photo'][] = $photo;
}
}
}
try {
$note = $this->noteService->createNote($data);
} catch (Exception $exception) {
@ -185,6 +195,15 @@ class MicropubController extends Controller
'syndicate-to' => config('syndication.targets'),
]);
}
//nope, how about a config query?
if ($request->input('q') == 'config') {
return response()->json([
'syndicate-to' => config('syndication.targets'),
'media-endpoint' => route('media-endpoint'),
]);
}
//nope, how about a geo URL?
if (substr($request->input('q'), 0, 4) === 'geo:') {
preg_match_all(
@ -203,13 +222,6 @@ class MicropubController extends Controller
'places' => $places,
]);
}
//nope, how about a config query?
if ($request->input('q') == 'config') {
return response()->json([
'syndicate-to' => config('syndication.targets'),
'media-endpoint' => route('media-endpoint'),
]);
}
//nope, just return the token
return response()->json([
@ -256,7 +268,7 @@ class MicropubController extends Controller
$scopes = explode(' ', $tokenData->getClaim('scope'));
if (array_search('post', $scopes) !== false) {
//check media valid
if ($request->file('file')->isValid()) {
if ($request->hasFile('file') && $request->file('file')->isValid()) {
//save media
try {
$filename = Uuid::uuid4() . '.' . $request->file('file')->extension();

View file

@ -2,9 +2,8 @@
namespace App\Http\Controllers;
use App\Tag;
use App\{Note, Tag};
use Twitter;
use App\Note;
use HTMLPurifier;
use GuzzleHttp\Client;
use HTMLPurifier_Config;
@ -58,6 +57,10 @@ class NotesController extends Controller
$note->place->icon
);
}
/*$mediaLinks = [];
foreach ($note->media()->get() as $media) {
$mediaLinks[] = $media->url;
}*/
}
$homepage = ($request->path() == '/');

View file

@ -4,11 +4,10 @@ declare(strict_types=1);
namespace App\Services;
use App\Note;
use App\Place;
use App\Jobs\SendWebMentions;
use App\Jobs\SyndicateToTwitter;
use App\Jobs\SyndicateToFacebook;
use Illuminate\Support\Facades\Log;
use App\{Media, Note, Place};
use App\Jobs\{SendWebMentions, SyndicateToFacebook, SyndicateToTwitter};
class NoteService
{
@ -55,6 +54,15 @@ class NoteService
}
}
*/
//add support for media uploaded as URLs
foreach ($data['photo'] as $photo) {
// check the media was uploaded to my endpoint
if (starts_with($photo, config('filesystems.disks.s3.url'))) {
$path = substr($photo, strlen(config('filesystems.disks.s3.url')));
$media = Media::where('path', ltrim($path, '/'))->firstOrFail();
$note->media()->save($media);
}
}
$note->save();