Save uploaded files to S3 and generate a URL
This commit is contained in:
parent
bd57ce56d4
commit
2c5c6e7753
4 changed files with 23 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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' => [
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue