Host images locally
We don’t need the complexity of S3. Sepcifically the complexity of managing my own AWS account, flysystem made the Laravel side easy. A command is added to copy the the S3 files over to local storage.
This commit is contained in:
parent
d80e8164c8
commit
d7da42b626
47 changed files with 295 additions and 214 deletions
|
@ -10,6 +10,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
|
|||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
use Tests\TestToken;
|
||||
|
||||
|
@ -22,9 +23,7 @@ class MicropubMediaTest extends TestCase
|
|||
public function emptyResponseForLastUploadWhenNoneFound(): void
|
||||
{
|
||||
// Make sure there’s no media
|
||||
Media::all()->each(function ($media) {
|
||||
$media->delete();
|
||||
});
|
||||
$this->assertCount(0, Media::all());
|
||||
|
||||
$response = $this->get(
|
||||
'/api/media?q=last',
|
||||
|
@ -82,10 +81,8 @@ class MicropubMediaTest extends TestCase
|
|||
public function clientCanListLastUpload(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
$file = __DIR__ . '/../aaron.png';
|
||||
$token = $this->getToken();
|
||||
config(['filesystems.disks.s3.url' => 'https://s3.example.com']);
|
||||
|
||||
$response = $this->post(
|
||||
'/api/media',
|
||||
|
@ -95,12 +92,8 @@ class MicropubMediaTest extends TestCase
|
|||
['HTTP_Authorization' => 'Bearer ' . $token]
|
||||
);
|
||||
|
||||
$location = $response->headers->get('Location');
|
||||
|
||||
$this->assertStringStartsWith('https://s3.example.com/', $location);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
|
||||
$lastUploadResponse = $this->get(
|
||||
'/api/media?q=last',
|
||||
|
@ -109,14 +102,14 @@ class MicropubMediaTest extends TestCase
|
|||
$lastUploadResponse->assertJson(['url' => $response->headers->get('Location')]);
|
||||
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function clientCanSourceUploads(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
$file = __DIR__ . '/../aaron.png';
|
||||
$token = $this->getToken();
|
||||
|
||||
|
@ -129,7 +122,7 @@ class MicropubMediaTest extends TestCase
|
|||
);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
|
||||
$sourceUploadResponse = $this->get(
|
||||
'/api/media?q=source',
|
||||
|
@ -141,14 +134,14 @@ class MicropubMediaTest extends TestCase
|
|||
]]]);
|
||||
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function clientCanSourceUploadsWithLimit(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
$file = __DIR__ . '/../aaron.png';
|
||||
$token = $this->getToken();
|
||||
|
||||
|
@ -161,7 +154,7 @@ class MicropubMediaTest extends TestCase
|
|||
);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
|
||||
$sourceUploadResponse = $this->get(
|
||||
'/api/media?q=source&limit=1',
|
||||
|
@ -175,7 +168,8 @@ class MicropubMediaTest extends TestCase
|
|||
$this->assertCount(1, json_decode($sourceUploadResponse->getContent(), true)['items']);
|
||||
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -256,7 +250,6 @@ class MicropubMediaTest extends TestCase
|
|||
public function mediaEndpointUploadFile(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
$file = __DIR__ . '/../aaron.png';
|
||||
|
||||
$response = $this->post(
|
||||
|
@ -268,18 +261,18 @@ class MicropubMediaTest extends TestCase
|
|||
);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
Queue::assertPushed(ProcessMedia::class);
|
||||
Storage::disk('local')->assertExists($filename);
|
||||
Storage::disk('local')->assertExists($path);
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function mediaEndpointUploadAudioFile(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
$file = __DIR__ . '/../audio.mp3';
|
||||
|
||||
$response = $this->post(
|
||||
|
@ -291,18 +284,18 @@ class MicropubMediaTest extends TestCase
|
|||
);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
Queue::assertPushed(ProcessMedia::class);
|
||||
Storage::disk('local')->assertExists($filename);
|
||||
Storage::disk('local')->assertExists($path);
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function mediaEndpointUploadVideoFile(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
$file = __DIR__ . '/../video.ogv';
|
||||
|
||||
$response = $this->post(
|
||||
|
@ -314,18 +307,18 @@ class MicropubMediaTest extends TestCase
|
|||
);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
Queue::assertPushed(ProcessMedia::class);
|
||||
Storage::disk('local')->assertExists($filename);
|
||||
Storage::disk('local')->assertExists($path);
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function mediaEndpointUploadDocumentFile(): void
|
||||
{
|
||||
Queue::fake();
|
||||
Storage::fake('s3');
|
||||
|
||||
$response = $this->post(
|
||||
'/api/media',
|
||||
|
@ -336,11 +329,12 @@ class MicropubMediaTest extends TestCase
|
|||
);
|
||||
|
||||
$path = parse_url($response->headers->get('Location'), PHP_URL_PATH);
|
||||
$filename = substr($path, 7);
|
||||
$filename = Str::chopStart($path, '/media/');
|
||||
Queue::assertPushed(ProcessMedia::class);
|
||||
Storage::disk('local')->assertExists($filename);
|
||||
Storage::disk('local')->assertExists($path);
|
||||
// now remove file
|
||||
unlink(storage_path('app/') . $filename);
|
||||
unlink(storage_path('app/media/') . $filename);
|
||||
$this->removeDirIfEmpty(storage_path('app/media'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue