Skip to content

Commit

Permalink
Add url validation before generating video tags
Browse files Browse the repository at this point in the history
Before, the process was adding both a source for webm and mp4 videos. But this
was an issue since not every source provide the 2 formats. Thus making the
rendering quite unusable when in third-party tools.
Now, the mp4 format is preferred. On top of that the URL is checked before being
added to the video tag. It's a bit longer to process but it's much safer since
all URL are validated beforehand.

See #5
  • Loading branch information
aledeg committed Apr 10, 2021
1 parent 9153811 commit bfba085
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions Transformer/DisplayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,36 @@ private function getNewVideoContent($baseUrl) {
$video->setAttribute('class', 'reddit-image');
$video->setAttribute('muted', $this->mutedVideo);

$webm = $video->appendChild($dom->createElement('source'));
$webm->setAttribute('src', "{$baseUrl}webm");
$webm->setAttribute('type', 'video/webm');
$mp4Url = "{$baseUrl}mp4";
if ($this->isAccessible($mp4Url)) {
$mp4 = $video->appendChild($dom->createElement('source'));
$mp4->setAttribute('src', $mp4Url);
$mp4->setAttribute('type', 'video/mp4');
return $dom->saveHTML();
}

$mp4 = $video->appendChild($dom->createElement('source'));
$mp4->setAttribute('src', "{$baseUrl}mp4");
$mp4->setAttribute('type', 'video/mp4');
$webmUrl = "{$baseUrl}webm";
if ($this->isAccessible($webmUrl)) {
$webm = $video->appendChild($dom->createElement('source'));
$webm->setAttribute('src', $webmUrl);
$webm->setAttribute('type', 'video/webm');
return $dom->saveHTML();
}

return $dom->saveHTML();
return;
}

/**
* @return bool
*/
private function isAccessible($href) {
$channel = curl_init($href);
curl_setopt($channel, CURLOPT_NOBODY, true);
curl_exec($channel);
$httpCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);
curl_close($channel);

return 200 === $httpCode;
}

/**
Expand Down

0 comments on commit bfba085

Please sign in to comment.