Skip to content

Commit

Permalink
fix: Explicitely capture thrown exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
geokrety-bot committed Aug 5, 2023
1 parent 8ef2415 commit 699e07d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
32 changes: 12 additions & 20 deletions downloader/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require __DIR__.'/vendor/autoload.php';

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client as AWSS3Client;

define('GK_WEBSITE_SERVER_URL', getenv('GK_WEBSITE_SERVER_URL') ?: 'http://nginx');
Expand Down Expand Up @@ -60,27 +59,20 @@ function fileUploaded(Base $f3) {
file_put_contents('/tmp/headers', print_r($f3->get('HEADERS'), true));
file_put_contents('/tmp/body', $f3->get('BODY'));

try {
$s3 = new AWSS3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => GK_MINIO_SERVER_URL,
'use_path_style_endpoint' => true,
'credentials' => [
'key' => GK_MINIO_PICTURES_PROCESSOR_MINIO_ACCESS_KEY,
'secret' => GK_MINIO_PICTURES_PROCESSOR_MINIO_SECRET_KEY,
],
]);
} catch (S3Exception $exception) {
http_response_code(401);
echo 'Failed to connect to S3!';
$f3->abort();
throw $exception;
}
$s3 = new AWSS3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => GK_MINIO_SERVER_URL,
'use_path_style_endpoint' => true,
'credentials' => [
'key' => GK_MINIO_PICTURES_PROCESSOR_MINIO_ACCESS_KEY,
'secret' => GK_MINIO_PICTURES_PROCESSOR_MINIO_SECRET_KEY,
],
]);

$body = json_decode($f3->get('BODY'), true);
if (!in_array($body['EventName'], ['s3:ObjectCreated:Put', 's3:ObjectCreated:Post'])) {
return;
throw new Exception('Invalid EventName');
}

list($bucket, $key) = explode('/', $body['Key'], 2);
Expand All @@ -93,7 +85,7 @@ function fileUploaded(Base $f3) {
'Key' => $key,
'SaveAs' => $imgPath,
]);
} catch (S3Exception $exception) {
} catch (Exception $exception) {
http_response_code(404);
echo 'File not found!';
$f3->abort();
Expand Down
32 changes: 12 additions & 20 deletions uploader/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require __DIR__.'/vendor/autoload.php';

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client as AWSS3Client;

define('GK_MINIO_SERVER_URL', getenv('GK_MINIO_SERVER_URL') ?: 'http://minio:9000');
Expand Down Expand Up @@ -34,27 +33,20 @@ function fileUploaded(Base $f3) {
throw new Exception('Missing or wrong authorization header');
}

try {
$s3 = new AWSS3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => GK_MINIO_SERVER_URL,
'use_path_style_endpoint' => true,
'credentials' => [
'key' => MINIO_ACCESS_KEY,
'secret' => MINIO_SECRET_KEY,
],
]);
} catch (S3Exception $exception) {
http_response_code(401);
echo 'Failed to connect to S3!';
$f3->abort();
throw $exception;
}
$s3 = new AWSS3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => GK_MINIO_SERVER_URL,
'use_path_style_endpoint' => true,
'credentials' => [
'key' => MINIO_ACCESS_KEY,
'secret' => MINIO_SECRET_KEY,
],
]);

$body = json_decode($f3->get('BODY'), true);
if ($body['EventName'] !== 's3:ObjectCreated:Put') {
return;
throw new Exception('Invalid EventName');
}

list($bucket, $key) = explode('/', $body['Key'], 2);
Expand All @@ -66,7 +58,7 @@ function fileUploaded(Base $f3) {
'Key' => $key,
'SaveAs' => $imgPath,
]);
} catch (S3Exception $exception) {
} catch (Exception $exception) {
http_response_code(404);
echo 'File not found!';
$f3->abort();
Expand Down

0 comments on commit 699e07d

Please sign in to comment.