Skip to content

Commit e7129fe

Browse files
committed
Autodetect binary content types to avoid download large files
1 parent 414183c commit e7129fe

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

Embed/Adapters/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class Adapter
4040
'soundcloudClientId' => null,
4141
'embedlyKey' => null,
4242
'oembedParameters' => array(),
43-
'ignoreFacebookProvider' => false
43+
'facebookProvider' => false
4444
);
4545

4646
/**

Embed/Adapters/Webpage.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ protected function initProviders(Request $request)
2828

2929
$this->providers = array(
3030
'Html' => new Providers\Html($request),
31-
'Facebook' => new Providers\Facebook($request),
3231
'TwitterCards' => new Providers\TwitterCards($request),
3332
'OpenGraph' => new Providers\OpenGraph($request)
3433
);
3534

36-
if ($this->options['ignoreFacebookProvider']) {
37-
unset($this->providers['Facebook']);
35+
if ($this->options['facebookProvider']) {
36+
$this->providers['Facebook'] = new Providers\Facebook($request);
3837
}
3938

4039
if ($this->providers['Html']->get('oembed')) {

Embed/RequestResolvers/Curl.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class Curl implements RequestResolverInterface
1616
'timeout' => 10,
1717
);
1818

19+
public static $binaryContentTypes = array(
20+
'#image/.*#',
21+
'#application/(pdf|x-download|zip|pdf|msword|vnd\\.ms|postscript|octet-stream)#',
22+
'#application/x-zip.*#'
23+
);
24+
1925

2026
/**
2127
* {@inheritdoc}
@@ -149,7 +155,39 @@ protected function resolve()
149155
CURLOPT_USERAGENT => $this->config['user_agent']
150156
));
151157

152-
$this->content = curl_exec($connection);
158+
$this->content = '';
159+
$isBinary = null;
160+
161+
curl_setopt($connection, CURLOPT_HEADERFUNCTION, function ($connection, $string) use (&$isBinary) {
162+
if (($isBinary === null) && strpos($string, ':')) {
163+
list($name, $value) = array_map('trim', explode(':', $string, 2));
164+
165+
if (strtolower($name) === 'content-type') {
166+
$isBinary = false;
167+
168+
foreach (self::$binaryContentTypes as $regex) {
169+
if (preg_match($regex, strtolower($value))) {
170+
$isBinary = true;
171+
break;
172+
}
173+
}
174+
}
175+
}
176+
177+
return strlen($string);
178+
});
179+
180+
curl_setopt($connection, CURLOPT_WRITEFUNCTION, function ($connection, $string) use (&$isBinary) {
181+
if ($isBinary) {
182+
return 0;
183+
}
184+
185+
$this->content .= $string;
186+
187+
return strlen($string);
188+
});
189+
190+
curl_exec($connection);
153191
$this->result = curl_getinfo($connection);
154192

155193
if ($this->content === false) {

demo/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function getResolverOption($name, $default = null)
139139
<label><span>Facebook access token:</span> <input type="text" name="options[facebookAccessToken]" value="<?php echo getOption('facebookAccessToken'); ?>"></label>
140140
<label><span>Embedly key:</span> <input type="text" name="options[embedlyKey]" value="<?php echo getOption('embedlyKey'); ?>"></label>
141141
<label><span>Soundcloud client id:</span> <input type="text" name="options[soundcloudClientId]" value="<?php echo getOption('soundcloudClientId', 'YOUR_CLIENT_ID'); ?>"></label>
142-
<label><span>Ignore facebook provider</span> <input type="checkbox" name="options[ignoreFacebookProvider]" value="1" <?php echo getOption('ignoreFacebookProvider') ? 'checked' : ''; ?>></label>
142+
<label><span>Use facebook provider</span> <input type="checkbox" name="options[facebookProvider]" value="1" <?php echo getOption('facebookProvider') ? 'checked' : ''; ?>></label>
143143
<label><span>oEmbed extra Parameters (in json format):</span> <input type="text" name="options[oembedParameters]" value="<?php echo getOption('oembedParameters'); ?>"></label>
144144
</fieldset>
145145
<fieldset class="options">

0 commit comments

Comments
 (0)