Repository navigation
feat: support Giphy and external GIF embeds #1199
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
devjerry0
wants to merge
2
commits into
dev-null-sh:production
Choose a base branch
from
devjerry0:feat/gif-support-only
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.
+1,923
−13
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,5 @@ enum EmbedKind: string | |
| case Video = 'video'; | ||
| case Audio = 'audio'; | ||
| case Code = 'code'; | ||
| case Gif = 'gif'; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Services\EmbedProviders; | ||
|
|
||
| use App\Contracts\Services\EmbedProvider; | ||
| use App\Dto\Embed; | ||
| use Illuminate\Support\Str; | ||
| use Uri\WhatWg\Url; | ||
|
|
||
| final readonly class Giphy implements EmbedProvider | ||
| { | ||
| private const array SUPPORTED_SCHEMES = ['http', 'https']; | ||
|
|
||
| private const array SUPPORTED_HOSTS = ['giphy.com', 'www.giphy.com']; | ||
|
|
||
| private const int PATH_SEGMENT_COUNT = 2; | ||
|
|
||
| public function embed(Url $url): ?Embed | ||
| { | ||
| if (! $this->supportsUrl($url)) { | ||
| return null; | ||
| } | ||
|
|
||
| $identifier = $this->getGifId($url->getPath()); | ||
|
|
||
| if ($identifier === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $mediaUrl = "https://media.giphy.com/media/{$identifier}"; | ||
|
|
||
| return Embed::gif('GIPHY', "{$mediaUrl}/giphy.gif", "{$mediaUrl}/giphy_s.gif"); | ||
| } | ||
|
|
||
| private function supportsUrl(Url $url): bool | ||
| { | ||
| if (! in_array($url->getScheme(), self::SUPPORTED_SCHEMES, true)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (! in_array($url->getAsciiHost(), self::SUPPORTED_HOSTS, true)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (($url->getUsername() ?? '') !== '') { | ||
| return false; | ||
| } | ||
|
|
||
| if (($url->getPassword() ?? '') !== '') { | ||
| return false; | ||
| } | ||
|
|
||
| return $url->getPort() === null; | ||
| } | ||
|
|
||
| private function getGifId(string $path): ?string | ||
| { | ||
| $segments = explode('/', mb_trim($path, '/')); | ||
|
|
||
| if (count($segments) !== self::PATH_SEGMENT_COUNT) { | ||
| return null; | ||
| } | ||
|
|
||
| [$section, $slug] = $segments; | ||
| $identifier = match (Str::lower($section)) { | ||
| 'gifs' => Str::afterLast($slug, '-'), | ||
| 'embed' => $slug, | ||
| default => '', | ||
| }; | ||
|
|
||
| return ctype_alnum($identifier) ? $identifier : null; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Services\LinkPreviews; | ||
|
|
||
| use GuzzleHttp\Psr7\Stream; | ||
| use GuzzleHttp\Psr7\Utils; | ||
|
|
||
| final class LimitedResponseBody extends Stream | ||
| { | ||
| private const string BINARY_ENCODING = '8bit'; | ||
|
|
||
| public function __construct(private readonly int $maxBytes) | ||
| { | ||
| parent::__construct(Utils::tryFopen('php://temp', 'w+')); | ||
| } | ||
|
|
||
| public function write(string $string): int | ||
| { | ||
| $remaining = max(0, $this->maxBytes - $this->tell()); | ||
|
|
||
| return parent::write(mb_substr($string, 0, $remaining, self::BINARY_ENCODING)); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 5832
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 41649
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 8173
Web query:
official Guzzle 7 CurlHandler write callback CURLOPT_WRITEFUNCTION callback return fewer bytes CURLE_WRITE_ERROR sink streamResult:
Web query:
raw GitHub Guzzle 8.3.0 CurlFactory.php CURLOPT_WRITEFUNCTION sink->write and Laravel framework v13.38.0 PendingRequest Guzzle client send exception handlingResult:
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 3746
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 8204
Script executed:
Repository: dev-null-sh/dev-null.sh
Length of output: 8879
Return the full callback length from the bounded sink.
When a GIF server ignores
Range, the fixture sends 254 bytes.LimitedResponseBody::write()keeps 32 bytes but returns32. Guzzle passes that value to cURL, which raises a response transfer error because the callback did not report the full buffer. Laravel does not convert thatResponseTransferExceptioninto a response, so GIF verification fails instead of returning the bounded body.Suggested fix
public function write(string $string): int { $remaining = max(0, $this->maxBytes - $this->tell()); - return parent::write(mb_substr($string, 0, $remaining, self::BINARY_ENCODING)); + parent::write(mb_substr($string, 0, $remaining, self::BINARY_ENCODING)); + + return strlen($string); }Committable suggestion