Add media endpoint support for the source parameter

This commit is contained in:
Jonny Barnes 2020-08-15 09:56:33 +01:00
parent 8e4e09c38f
commit ae2c45ff3d
2 changed files with 97 additions and 0 deletions

View file

@ -63,6 +63,30 @@ class MicropubMediaController extends Controller
return response()->json(['url' => $media->url]);
}
if (request()->input('q') === 'source') {
$limit = request()->input('limit', 10);
$offset = request()->input('offset', 0);
$media = Media::latest()->offset($offset)->limit($limit)->get();
$media->transform(function ($mediaItem) {
return [
'url' => $mediaItem->url,
];
});
return response()->json(['items' => $media]);
}
if (request()->has('q')) {
return response()->json([
'error' => 'invalid_request',
'error_description' => 'This server does not know how to handle this q parameter (' . request()->input('q') . ')',
], 400);
}
return response()->json(['status' => 'OK']);
}
/**

View file

@ -60,6 +60,79 @@ class MicropubMediaTest extends TestCase
unlink(storage_path('app/') . $filename);
}
/** @test */
public function clientCanSourceUploads()
{
Queue::fake();
Storage::fake('s3');
$file = __DIR__ . '/../aaron.png';
$response = $this->post(
'/api/media',
[
'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true),
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$path = parse_url($response->getData()->location, PHP_URL_PATH);
$filename = substr($path, 7);
$sourceUploadResponse = $this->get(
'/api/media?q=source',
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$sourceUploadResponse->assertJson(['items' => [[
'url' => $response->getData()->location,
]]]);
// now remove file
unlink(storage_path('app/') . $filename);
}
/** @test */
public function clientCanSourceUploadsWithLimit()
{
Queue::fake();
Storage::fake('s3');
$file = __DIR__ . '/../aaron.png';
$response = $this->post(
'/api/media',
[
'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true),
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$path = parse_url($response->getData()->location, PHP_URL_PATH);
$filename = substr($path, 7);
$sourceUploadResponse = $this->get(
'/api/media?q=source&limit=1',
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$sourceUploadResponse->assertJson(['items' => [[
'url' => $response->getData()->location,
]]]);
// And given our limit of 1 there should only be one result
$this->assertCount(1, json_decode($sourceUploadResponse->getContent(), true)['items']);
// now remove file
unlink(storage_path('app/') . $filename);
}
/** @test */
public function errorResponseForUnknownQValue()
{
$response = $this->get(
'/api/media?q=unknown',
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertStatus(400);
$response->assertJson(['error' => 'invalid_request']);
}
/** @test */
public function optionsRequestReturnsCorsResponse()
{