/**
* Proxies requests for embedded images.
*
- * @author Matthias Schmidt
- * @copyright 2001-2019 WoltLab GmbH
+ * @author Tim Duesterhus, Matthias Schmidt
+ * @copyright 2001-2021 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package WoltLabSuite\Core\Action
* @since 3.0
}
// download image
+ $file = null;
+ $response = null;
try {
$client = HttpFactory::makeClient([
RequestOptions::TIMEOUT => 10,
$request = new Request('GET', $url, [
'via' => '1.1 wsc',
'accept' => 'image/*',
- 'range' => 'bytes=0-' . (self::MAX_SIZE - 1),
]);
$response = $client->send($request);
$file->write($response->getBody()->read(8192));
if ($response->getBody()->tell() >= self::MAX_SIZE) {
- break;
+ throw new \DomainException(\sprintf(
+ 'Response body is larger than the accepted maximum size (%d Bytes).',
+ self::MAX_SIZE
+ ));
}
}
- $response->getBody()->close();
$file->flush();
- $file->close();
} catch (TransferException $e) {
throw new \DomainException('Failed to request', 0, $e);
+ } finally {
+ if ($response && $response->getBody()) {
+ $response->getBody()->close();
+ }
+ if ($file) {
+ $file->close();
+ }
}
// check file type