mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-25 02:10:39 +00:00
test s3 connection before set up
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace Domain\Settings\Actions;
|
||||
|
||||
use Storage;
|
||||
use Domain\Settings\DTO\S3CredentialsData;
|
||||
|
||||
class TestS3ConnectionAction
|
||||
{
|
||||
public function __invoke(S3CredentialsData $credentials): void
|
||||
{
|
||||
// Set temporary s3 connection
|
||||
config([
|
||||
'filesystems.disks.s3' => [
|
||||
'driver' => 's3',
|
||||
'key' => $credentials->key,
|
||||
'secret' => $credentials->secret,
|
||||
'region' => $credentials->region,
|
||||
'bucket' => $credentials->bucket,
|
||||
'endpoint' => $credentials->endpoint,
|
||||
],
|
||||
]);
|
||||
|
||||
// Try to get files
|
||||
Storage::disk('s3')->allFiles();
|
||||
|
||||
// Try to create folder
|
||||
Storage::disk('s3')->makeDirectory('s3-test');
|
||||
|
||||
// Delete test folder
|
||||
Storage::disk('s3')->deleteDirectory('s3-test');
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,19 @@ namespace Domain\Settings\Controllers;
|
||||
|
||||
use Artisan;
|
||||
use Illuminate\Http\Response;
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use League\Flysystem\UnableToWriteFile;
|
||||
use Domain\Settings\DTO\S3CredentialsData;
|
||||
use Domain\Settings\Actions\TestS3ConnectionAction;
|
||||
use Domain\Settings\Requests\StoreStorageCredentialsRequest;
|
||||
|
||||
class StoreStorageCredentialsController
|
||||
{
|
||||
public function __construct(
|
||||
private TestS3ConnectionAction $testS3Connection,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set new email credentials to .env file
|
||||
*/
|
||||
@@ -15,6 +24,20 @@ class StoreStorageCredentialsController
|
||||
// Abort in demo mode
|
||||
abort_if(is_demo(), 204, 'Done.');
|
||||
|
||||
// Test s3 credentials
|
||||
if ($request->input('storage.driver') !== 'local') {
|
||||
try {
|
||||
// connect to the s3
|
||||
($this->testS3Connection)(S3CredentialsData::fromRequest($request));
|
||||
} catch (S3Exception | UnableToWriteFile $error) {
|
||||
return response([
|
||||
'type' => 's3-connection-error',
|
||||
'title' => 'S3 Connection Error',
|
||||
'message' => $error->getMessage(),
|
||||
], 401);
|
||||
}
|
||||
}
|
||||
|
||||
if (! app()->runningUnitTests()) {
|
||||
$drivers = [
|
||||
'local' => [
|
||||
@@ -34,10 +57,9 @@ class StoreStorageCredentialsController
|
||||
$driver = 'local' === $request->input('storage.driver') ? 'local' : 's3';
|
||||
|
||||
// Storage credentials for storage
|
||||
setEnvironmentValue(
|
||||
$drivers[$driver]
|
||||
);
|
||||
setEnvironmentValue($drivers[$driver]);
|
||||
|
||||
// Cache the config
|
||||
Artisan::call('config:cache');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Domain\Settings\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
|
||||
class S3CredentialsData extends DataTransferObject
|
||||
{
|
||||
public string $key;
|
||||
public string $secret;
|
||||
public string $region;
|
||||
public string $bucket;
|
||||
public string $endpoint;
|
||||
|
||||
public static function fromRequest($request): self
|
||||
{
|
||||
return new self([
|
||||
'key' => $request->input('storage.key'),
|
||||
'secret' => $request->input('storage.secret'),
|
||||
'region' => $request->input('storage.region'),
|
||||
'bucket' => $request->input('storage.bucket'),
|
||||
'endpoint' => $request->input('storage.endpoint'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user