-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
356 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use MrEduar\LaravelS3Multipart\Http\Controllers\S3MultipartController; | ||
|
||
Route::get('s3m/create-multipart-upload', [S3MultipartController::class, 'createMultipartUpload'])->name('s3m.create-multipart'); |
17 changes: 17 additions & 0 deletions
17
src/Contracts/StorageMultipartUploadControllerContract.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace MrEduar\LaravelS3Multipart\Contracts; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
use MrEduar\LaravelS3Multipart\Http\Requests\CompleteMultipartUploadRequest; | ||
use MrEduar\LaravelS3Multipart\Http\Requests\CreateMultipartUploadRequest; | ||
use MrEduar\LaravelS3Multipart\Http\Requests\SignPartRequest; | ||
|
||
interface StorageMultipartUploadControllerContract | ||
{ | ||
public function createMultipartUpload(CreateMultipartUploadRequest $request): JsonResponse; | ||
|
||
public function signPartUpload(SignPartRequest $request): JsonResponse; | ||
|
||
public function completeMultipartUpload(CompleteMultipartUploadRequest $request): JsonResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
namespace MrEduar\LaravelS3Multipart\Http\Controllers; | ||
|
||
use Aws\S3\S3Client; | ||
use Exception; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Controller; | ||
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
use MrEduar\LaravelS3Multipart\Contracts\StorageMultipartUploadControllerContract; | ||
use MrEduar\LaravelS3Multipart\Http\Requests\CreateMultipartUploadRequest; | ||
use MrEduar\LaravelS3Multipart\Http\Requests\SignPartRequest; | ||
|
||
class S3MultipartController extends Controller implements StorageMultipartUploadControllerContract | ||
{ | ||
/** | ||
* Create a new multipart upload. | ||
*/ | ||
public function createMultipartUpload(CreateMultipartUploadRequest $request): JsonResponse | ||
{ | ||
$this->ensureEnvironmentVariablesAreAvailable($request); | ||
|
||
$client = $this->storageClient(); | ||
|
||
$bucket = $request->input('bucket') ?: $_ENV['AWS_BUCKET']; | ||
|
||
$uuid = (string) Str::uuid(); | ||
|
||
$key = $this->getKey($uuid); | ||
|
||
try { | ||
$uploader = $client->createMultipartUpload([ | ||
'Bucket' => $bucket, | ||
'Key' => $key, | ||
'ACL' => $request->input('visibility') ?: $this->defaultVisibility(), | ||
'ContentType' => $request->input('content_type') ?: 'application/octet-stream', | ||
]); | ||
|
||
return response()->json([ | ||
'uuid' => $uuid, | ||
'bucket' => $bucket, | ||
'key' => $key, | ||
'uploadId' => $uploader['UploadId'], | ||
]); | ||
} catch (Exception $e) { | ||
return response()->json([ | ||
'error' => $e->getMessage(), | ||
], 500); | ||
} | ||
} | ||
|
||
/** | ||
* Sign a part upload. | ||
*/ | ||
public function signPartUpload(SignPartRequest $request): JsonResponse | ||
{ | ||
return new JsonResponse([]); | ||
} | ||
|
||
/** | ||
* Complete a multipart upload. | ||
*/ | ||
public function completeMultipartUpload(Request $request): JsonResponse | ||
{ | ||
return new JsonResponse([]); | ||
} | ||
|
||
/** | ||
* Ensure the required environment variables are available. | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function ensureEnvironmentVariablesAreAvailable(Request $request): void | ||
{ | ||
$missing = array_diff_key(array_flip(array_filter([ | ||
$request->input('bucket') ? null : 'AWS_BUCKET', | ||
'AWS_DEFAULT_REGION', | ||
'AWS_ACCESS_KEY_ID', | ||
'AWS_SECRET_ACCESS_KEY', | ||
])), $_ENV); | ||
|
||
if (empty($missing)) { | ||
return; | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
'Unable to issue signed URL. Missing environment variables: '.implode(', ', array_keys($missing)) | ||
); | ||
} | ||
|
||
/** | ||
* Get the S3 storage client instance. | ||
*/ | ||
protected function storageClient(): S3Client | ||
{ | ||
$config = [ | ||
'region' => config('filesystems.disks.s3.region', $_ENV['AWS_DEFAULT_REGION']), | ||
'version' => 'latest', | ||
'signature_version' => 'v4', | ||
'use_path_style_endpoint' => config('filesystems.disks.s3.use_path_style_endpoint', false), | ||
]; | ||
|
||
$config['credentials'] = array_filter([ | ||
'key' => $_ENV['AWS_ACCESS_KEY_ID'] ?? null, | ||
'secret' => $_ENV['AWS_SECRET_ACCESS_KEY'] ?? null, | ||
'token' => $_ENV['AWS_SESSION_TOKEN'] ?? null, | ||
'url' => $_ENV['AWS_URL'] ?? null, | ||
'endpoint' => $_ENV['AWS_URL'] ?? null, | ||
]); | ||
|
||
if (array_key_exists('AWS_URL', $_ENV) && ! is_null($_ENV['AWS_URL'])) { | ||
$config['url'] = $_ENV['AWS_URL']; | ||
$config['endpoint'] = $_ENV['AWS_URL']; | ||
} | ||
|
||
return new S3Client($config); | ||
} | ||
|
||
/** | ||
* Get key for the given UUID. | ||
*/ | ||
protected function getKey(string $uuid): string | ||
{ | ||
return 'tmp/'.$uuid; | ||
} | ||
|
||
/** | ||
* Get the default visibility for uploads. | ||
*/ | ||
protected function defaultVisibility(): string | ||
{ | ||
return 'private'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace MrEduar\LaravelS3Multipart\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class CompleteMultipartUploadRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace MrEduar\LaravelS3Multipart\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Facades\Gate; | ||
|
||
class CreateMultipartUploadRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return Gate::allows('uploadFiles', [$this->user(), $this->input('bucket')]); | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'bucket' => ['nullable', 'string'], | ||
'visibility' => ['nullable', 'string'], | ||
'content_type' => ['nullable', 'string'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace MrEduar\LaravelS3Multipart\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class SignPartRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.