Starting work on adding a media endpoint

This commit is contained in:
Jonny Barnes 2017-03-09 22:00:29 +00:00
parent 602e09d4b8
commit bd57ce56d4
6 changed files with 125 additions and 19 deletions

View file

@ -3,11 +3,13 @@
namespace App\Http\Controllers;
use App\Place;
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 Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
class MicropubController extends Controller
{
@ -238,9 +240,9 @@ class MicropubController extends Controller
$httpAuth = $request->header('Authorization');
if (preg_match('/Bearer (.+)/', $httpAuth, $match)) {
$token = $match[1];
$valid = $this->tokenService->validateToken($token);
$tokenData = $this->tokenService->validateToken($token);
if ($valid === null) {
if ($tokenData === null) {
return response()->json([
'response' => 'error',
'error' => 'invalid_token',
@ -249,10 +251,33 @@ class MicropubController extends Controller
}
//check post scope
if ($tokenData->hasClaim('scope')) {
$scopes = explode(' ', $tokenData->getClaim('scope'));
if (array_search('post', $scopes) !== false) {
//check media valid
if ($request->file('file')->isValid()) {
//save media
try {
$filename = Uuid::uuid4() . $request->file->extension();
} catch (UnsatisfiedDependencyException $e) {
return response()->json([
'response' => 'error',
'error' => 'internal_server_error',
'error_description' => 'A problem occured handling your request'
], 500)
}
try {
$path = $request->file->storeAs('media', $filename, 's3');
} catch(Excetion $e) { // which exception?
return response()->json([
'response' => 'error',
'error' => 'service_unavailable',
'error_description' => 'Unable to save media to S3'
], 503)
}
//check media valid
//save media
return $path;
}
//return URL for media
}

30
app/Media.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Media extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
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.
*/
public function note()
{
return $this->belongsTo('App\Note');
}
}

View file

@ -53,6 +53,16 @@ class Note extends Model
return $this->belongsTo('App\Place');
}
/**
* Define the relationship with media.
*
* @return void
*/
public function media()
{
return $this->hasMany('App\Media');
}
/**
* We shall set a blacklist of non-modifiable model attributes.
*