Save uploaded files to S3 and generate a URL

This commit is contained in:
Jonny Barnes 2017-03-10 09:58:35 +00:00
parent bd57ce56d4
commit 2c5c6e7753
4 changed files with 23 additions and 14 deletions

View file

@ -258,7 +258,7 @@ class MicropubController extends Controller
if ($request->file('file')->isValid()) { if ($request->file('file')->isValid()) {
//save media //save media
try { try {
$filename = Uuid::uuid4() . $request->file->extension(); $filename = Uuid::uuid4() . $request->file('file')->extension();
} catch (UnsatisfiedDependencyException $e) { } catch (UnsatisfiedDependencyException $e) {
return response()->json([ return response()->json([
'response' => 'error', 'response' => 'error',
@ -267,16 +267,20 @@ class MicropubController extends Controller
], 500) ], 500)
} }
try { try {
$path = $request->file->storeAs('media', $filename, 's3'); $path = $request->file('file')->storeAs('media', $filename, 's3');
} catch(Excetion $e) { // which exception? } catch (Exception $e) { // which exception?
return response()->json([ return response()->json([
'response' => 'error', 'response' => 'error',
'error' => 'service_unavailable', 'error' => 'service_unavailable',
'error_description' => 'Unable to save media to S3' 'error_description' => 'Unable to save media to S3'
], 503) ], 503)
} }
$media = new Media();
$media->token = $token;
$media->path = $path;
$media->save();
return $path; return $media->url;
} }
//return URL for media //return URL for media

View file

@ -13,13 +13,6 @@ class Media extends Model
*/ */
protected $table = 'media_endpoint'; 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. * Get the note that owns this media.
*/ */
@ -27,4 +20,14 @@ class Media extends Model
{ {
return $this->belongsTo('App\Note'); 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;
}
} }

View file

@ -61,6 +61,7 @@ return [
'secret' => env('AWS_S3_SECRET'), 'secret' => env('AWS_S3_SECRET'),
'region' => env('AWS_S3_REGION'), 'region' => env('AWS_S3_REGION'),
'bucket' => env('AWS_S3_BUCKET'), 'bucket' => env('AWS_S3_BUCKET'),
'url' => env('AWS_S3_URL'),
], ],
'media' => [ 'media' => [

View file

@ -14,13 +14,14 @@ class CreateMediaEndpointTable extends Migration
public function up() public function up()
{ {
Schema::create('media_endpoint', function (Blueprint $table) { Schema::create('media_endpoint', function (Blueprint $table) {
$table->uuid('id'); $table->increments('id');
$table->varchar('client_id')->nullable(); $table->text('token')->nullable();
$table->varchar('filetype'); $table->varchar('path');
$table->unsignedInteger('note_id')->nullable(); $table->unsignedInteger('note_id')->nullable();
$table->timestamps(); $table->timestamps();
$table->primary('id'); $table->primary('id');
$table->index('token');
$table->foreign('note_id')->references('id')->on('notes'); $table->foreign('note_id')->references('id')->on('notes');
}); });
} }