diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 5c4503a7..5b88dd43 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Media; use App\Place; use Ramsey\Uuid\Uuid; use Illuminate\Http\Request; @@ -203,10 +204,10 @@ class MicropubController extends Controller ]); } //nope, how about a config query? - //this should have a media endpoint as well at some point if ($request->input('q') == 'config') { return response()->json([ 'syndicate-to' => config('syndication.targets'), + 'media-endpoint' => route('media-endpoint'), ]); } @@ -258,28 +259,53 @@ class MicropubController extends Controller if ($request->file('file')->isValid()) { //save media try { - $filename = Uuid::uuid4() . $request->file->extension(); + $filename = Uuid::uuid4() . '.' . $request->file('file')->extension(); } catch (UnsatisfiedDependencyException $e) { return response()->json([ 'response' => 'error', 'error' => 'internal_server_error', 'error_description' => 'A problem occured handling your request' - ], 500) + ], 500); } try { - $path = $request->file->storeAs('media', $filename, 's3'); - } catch(Excetion $e) { // which exception? + $path = $request->file('file')->storeAs('media', $filename, 's3'); + } catch (Exception $e) { // which exception? return response()->json([ 'response' => 'error', 'error' => 'service_unavailable', 'error_description' => 'Unable to save media to S3' - ], 503) + ], 503); } + $media = new Media(); + $media->token = $token; + $media->path = $path; + $media->save(); - return $path; + return response()->json([ + 'response' => 'created', + 'location' => $media->url, + ], 201)->header('Location', $media->url); } - //return URL for media + return response()->json([ + 'response' => 'error', + 'error' => 'invalid_request', + 'error_description' => 'The uploaded file failed validation', + ], 400); + } + + return response()->json([ + 'response' => 'error', + 'error' => 'insufficient_scope', + 'error_description' => 'The provided token has insufficient scopes', + ], 401); + } + + return response()->json([ + 'response' => 'error', + 'error' => 'unauthorized', + 'error_description' => 'No token provided with request', + ], 401); } return response()->json([ diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php index 45915796..06a68e4c 100644 --- a/app/Http/Controllers/NotesController.php +++ b/app/Http/Controllers/NotesController.php @@ -58,12 +58,6 @@ class NotesController extends Controller $note->place->icon ); } - $photoURLs = []; - $photos = $note->getMedia(); - foreach ($photos as $photo) { - $photoURLs[] = $photo->getUrl(); - } - $note->photoURLs = $photoURLs; } $homepage = ($request->path() == '/'); @@ -164,13 +158,6 @@ class NotesController extends Controller ); } - $photoURLs = []; - $photos = $note->getMedia(); - foreach ($photos as $photo) { - $photoURLs[] = $photo->getUrl(); - } - $note->photoURLs = $photoURLs; - return view('notes.show', compact('note', 'replies', 'reposts', 'likes')); } diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index c8545185..948eda56 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -12,9 +12,10 @@ class VerifyCsrfToken extends BaseVerifier * @var array */ protected $except = [ - 'api/token', + 'api/media', 'api/post', - 'webmention', + 'api/token', 'places/new', + 'webmention', ]; } diff --git a/app/Media.php b/app/Media.php index 5c7370e0..f0234c6e 100644 --- a/app/Media.php +++ b/app/Media.php @@ -20,4 +20,14 @@ class Media extends Model { return $this->belongsTo('App\Note'); } + + /** + * Get the URL for an S3 media file. + * + * @return string + */ + public function getUrlAttribute() + { + return config('filesystems.disks.s3.url') . '/' . $this->path; + } } diff --git a/config/filesystems.php b/config/filesystems.php index 66120369..38b34b97 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -61,6 +61,7 @@ return [ 'secret' => env('AWS_S3_SECRET'), 'region' => env('AWS_S3_REGION'), 'bucket' => env('AWS_S3_BUCKET'), + 'url' => env('AWS_S3_URL'), ], 'media' => [ diff --git a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php index 92789dc0..a2388065 100644 --- a/database/migrations/2017_03_09_155908_create_media_endpoint_table.php +++ b/database/migrations/2017_03_09_155908_create_media_endpoint_table.php @@ -14,13 +14,13 @@ class CreateMediaEndpointTable extends Migration public function up() { Schema::create('media_endpoint', function (Blueprint $table) { - $table->uuid('id'); - $table->varchar('client_id')->nullable(); - $table->varchar('filetype'); + $table->increments('id'); + $table->text('token')->nullable(); + $table->string('path'); $table->unsignedInteger('note_id')->nullable(); $table->timestamps(); - $table->primary('id'); + $table->index('token'); $table->foreign('note_id')->references('id')->on('notes'); }); } diff --git a/resources/views/templates/note.blade.php b/resources/views/templates/note.blade.php index d5ff1ae7..625af985 100644 --- a/resources/views/templates/note.blade.php +++ b/resources/views/templates/note.blade.php @@ -8,11 +8,6 @@
{!! $note->note !!} - @if(count($note->photoURLs) > 0) - @foreach($note->photoURLs as $photoURL) - - @endforeach - @endif
diff --git a/routes/web.php b/routes/web.php index a8268e27..6d0dc09a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -115,7 +115,7 @@ Route::group(['domain' => config('url.longurl')], function () { // Micropub Endpoint Route::get('api/post', 'MicropubController@get'); Route::post('api/post', 'MicropubController@post'); - Route::post('api/media', 'MicropubController@media'); + Route::post('api/media', 'MicropubController@media')->name('media-endpoint'); //webmention Route::get('webmention', 'WebMentionsController@get');