Repository navigation
fix: serve commit images straight from cloud storage #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
murrant
wants to merge
6
commits into
dev-null-sh:production
Choose a base branch
from
murrant:images
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d149c98
fix: serve commit images straight from cloud storage
murrant 1d88500
Signed S3 URLs now go only to public images, last 20 minutes, and are…
murrant a0021dd
restore download functionality lost in merge
murrant 89e9cf2
Fix test
murrant 11a2d93
docblocks
murrant 39751f5
return null for any signed URL that isn't HTTPS
murrant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Models\CommitImage; | ||
| use Aws\Credentials\CredentialsInterface; | ||
| use Illuminate\Filesystem\AwsS3V3Adapter; | ||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| final readonly class CreateCommitImageSignedUrl | ||
| { | ||
| /** | ||
| * How long the URL lets anyone holding it fetch the image, which bounds how long an image stays reachable after its commit is deleted or rejected. | ||
| */ | ||
| public const int LIFETIME = 1200; | ||
|
|
||
| /** | ||
| * How long the browser reuses the URL, and so the image behind it, kept well inside its lifetime. | ||
| */ | ||
| public const int MAX_AGE = 600; | ||
|
|
||
| /** | ||
| * Only cloud storage serves files itself, and only while the credentials signing the URL outlive it: role credentials can expire first, which would leave the browser holding a dead URL. | ||
| */ | ||
| public function handle(CommitImage $commitImage, bool $download = false): ?string | ||
| { | ||
| $disk = Storage::disk(); | ||
|
|
||
| if (! $disk instanceof AwsS3V3Adapter) { | ||
| return null; | ||
| } | ||
|
|
||
| $expires = now()->addSeconds(self::LIFETIME); | ||
| $credentials = $disk->getClient()->getCredentials()->wait(); | ||
|
|
||
| if (! $credentials instanceof CredentialsInterface || ($credentials->getExpiration() !== null && $credentials->getExpiration() < $expires->getTimestamp())) { | ||
| return null; | ||
| } | ||
|
|
||
| return $disk->temporaryUrl($commitImage->path, $expires, [ | ||
| 'ResponseContentType' => 'image/webp', | ||
| 'ResponseContentDisposition' => $download | ||
| ? 'attachment; filename="'.$commitImage->id.'.webp"' | ||
| : 'inline', | ||
| 'ResponseCacheControl' => 'private, max-age='.self::MAX_AGE, | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.