diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 5c4503a7..8b1847e4 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -258,7 +258,7 @@ 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', @@ -267,16 +267,20 @@ class MicropubController extends Controller ], 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) } + $media = new Media(); + $media->token = $token; + $media->path = $path; + $media->save(); - return $path; + return $media->url; } //return URL for media diff --git a/app/Media.php b/app/Media.php index 61f94160..31ea3a1b 100644 --- a/app/Media.php +++ b/app/Media.php @@ -13,13 +13,6 @@ class Media extends Model */ protected $table = 'media_endpoint'; - /** - * Our primaru key is a UUID value, therefore not incrementing. - * - * @var boolean - */ - protected $incrementing = false; - /** * Get the note that owns this media. */ @@ -27,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.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..e885881f 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,14 @@ 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->varchar('path'); $table->unsignedInteger('note_id')->nullable(); $table->timestamps(); $table->primary('id'); + $table->index('token'); $table->foreign('note_id')->references('id')->on('notes'); }); }