1
0
Fork 0
mirror of synced 2024-06-28 03:01:15 +12:00

Bugs fixed

- Updated qrcode to v4.2
- Removed baconqrcode from composer
- Removed comments
- Fixed some indents
- Fixed the QR endpoint
This commit is contained in:
Pedro Cisneros Santana 2020-10-26 14:23:58 +01:00
parent f91a641032
commit bff0e4db58
2 changed files with 40 additions and 60 deletions

View file

@ -11,22 +11,17 @@ use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\Filesystem; use Utopia\Cache\Adapter\Filesystem;
use Appwrite\Resize\Resize; use Appwrite\Resize\Resize;
use Appwrite\URL\URL as URLParse; use Appwrite\URL\URL as URLParse;
/* use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer; */
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\Validator\HexColor; use Utopia\Validator\HexColor;
use chillerlan\QRCode\QRCode; use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions; use chillerlan\QRCode\QROptions;
/* use SebastianBergmann\CodeCoverage\Report\Html\Renderer; */
$avatarCallback = function ($type, $code, $width, $height, $quality, $response) { $avatarCallback = function ($type, $code, $width, $height, $quality, $response) {
/** @var Utopia\Response $response */ /** @var Utopia\Response $response */
$code = \strtolower($code); $code = \strtolower($code);
$type = \strtolower($type); $type = \strtolower($type);
$set = Config::getParam('avatar-'.$type, []); $set = Config::getParam('avatar-' . $type, []);
if (empty($set)) { if (empty($set)) {
throw new Exception('Avatar set not found', 404); throw new Exception('Avatar set not found', 404);
@ -41,16 +36,16 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response)
} }
$output = 'png'; $output = 'png';
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$key = \md5('/v1/avatars/:type/:code-'.$code.$width.$height.$quality.$output); $key = \md5('/v1/avatars/:type/:code-' . $code . $width . $height . $quality . $output);
$path = $set[$code]; $path = $set[$code];
$type = 'png'; $type = 'png';
if (!\is_readable($path)) { if (!\is_readable($path)) {
throw new Exception('File not readable in '.$path, 500); throw new Exception('File not readable in ' . $path, 500);
} }
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size
$data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */); $data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */);
if ($data) { if ($data) {
@ -60,8 +55,7 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response)
->setContentType('image/png') ->setContentType('image/png')
->addHeader('Expires', $date) ->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'hit') ->addHeader('X-Appwrite-Cache', 'hit')
->send($data) ->send($data);
;
} }
$resize = new Resize(\file_get_contents($path)); $resize = new Resize(\file_get_contents($path));
@ -71,15 +65,14 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response)
$output = (empty($output)) ? $type : $output; $output = (empty($output)) ? $type : $output;
$data = $resize->output($output, $quality); $data = $resize->output($output, $quality);
$cache->save($key, $data); $cache->save($key, $data);
$response $response
->setContentType('image/png') ->setContentType('image/png')
->addHeader('Expires', $date) ->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss') ->addHeader('X-Appwrite-Cache', 'miss')
->send($data, null); ->send($data, null);;
;
unset($resize); unset($resize);
}; };
@ -93,7 +86,7 @@ App::get('/v1/avatars/credit-cards/:code')
->label('sdk.method', 'getCreditCard') ->label('sdk.method', 'getCreditCard')
->label('sdk.methodType', 'location') ->label('sdk.methodType', 'location')
->label('sdk.description', '/docs/references/avatars/get-credit-card.md') ->label('sdk.description', '/docs/references/avatars/get-credit-card.md')
->param('code', '', new WhiteList(\array_keys(Config::getParam('avatar-credit-cards'))), 'Credit Card Code. Possible values: '.\implode(', ', \array_keys(Config::getParam('avatar-credit-cards'))).'.') ->param('code', '', new WhiteList(\array_keys(Config::getParam('avatar-credit-cards'))), 'Credit Card Code. Possible values: ' . \implode(', ', \array_keys(Config::getParam('avatar-credit-cards'))) . '.')
->param('width', 100, new Range(0, 2000), 'Image width. Pass an integer between 0 to 2000. Defaults to 100.', true) ->param('width', 100, new Range(0, 2000), 'Image width. Pass an integer between 0 to 2000. Defaults to 100.', true)
->param('height', 100, new Range(0, 2000), 'Image height. Pass an integer between 0 to 2000. Defaults to 100.', true) ->param('height', 100, new Range(0, 2000), 'Image height. Pass an integer between 0 to 2000. Defaults to 100.', true)
->param('quality', 100, new Range(0, 100), 'Image quality. Pass an integer between 0 to 100. Defaults to 100.', true) ->param('quality', 100, new Range(0, 100), 'Image quality. Pass an integer between 0 to 100. Defaults to 100.', true)
@ -152,10 +145,10 @@ App::get('/v1/avatars/image')
$quality = 80; $quality = 80;
$output = 'png'; $output = 'png';
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$key = \md5('/v2/avatars/images-'.$url.'-'.$width.'/'.$height.'/'.$quality); $key = \md5('/v2/avatars/images-' . $url . '-' . $width . '/' . $height . '/' . $quality);
$type = 'png'; $type = 'png';
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size
$data = $cache->load($key, 60 * 60 * 24 * 7 /* 1 week */); $data = $cache->load($key, 60 * 60 * 24 * 7 /* 1 week */);
if ($data) { if ($data) {
@ -163,8 +156,7 @@ App::get('/v1/avatars/image')
->setContentType('image/png') ->setContentType('image/png')
->addHeader('Expires', $date) ->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'hit') ->addHeader('X-Appwrite-Cache', 'hit')
->send($data) ->send($data);
;
} }
if (!\extension_loaded('imagick')) { if (!\extension_loaded('imagick')) {
@ -186,17 +178,16 @@ App::get('/v1/avatars/image')
$resize->crop((int) $width, (int) $height); $resize->crop((int) $width, (int) $height);
$output = (empty($output)) ? $type : $output; $output = (empty($output)) ? $type : $output;
$data = $resize->output($output, $quality); $data = $resize->output($output, $quality);
$cache->save($key, $data); $cache->save($key, $data);
$response $response
->setContentType('image/png') ->setContentType('image/png')
->addHeader('Expires', $date) ->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss') ->addHeader('X-Appwrite-Cache', 'miss')
->send($data); ->send($data);;
;
unset($resize); unset($resize);
}, ['response']); }, ['response']);
@ -218,10 +209,10 @@ App::get('/v1/avatars/favicon')
$height = 56; $height = 56;
$quality = 80; $quality = 80;
$output = 'png'; $output = 'png';
$date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT'; // 45 days cache $date = \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT'; // 45 days cache
$key = \md5('/v2/avatars/favicon-'.$url); $key = \md5('/v2/avatars/favicon-' . $url);
$type = 'png'; $type = 'png';
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-0')); // Limit file number or size $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size
$data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */); $data = $cache->load($key, 60 * 60 * 24 * 30 * 3 /* 3 months */);
if ($data) { if ($data) {
@ -229,8 +220,7 @@ App::get('/v1/avatars/favicon')
->setContentType('image/png') ->setContentType('image/png')
->addHeader('Expires', $date) ->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'hit') ->addHeader('X-Appwrite-Cache', 'hit')
->send($data) ->send($data);
;
} }
if (!\extension_loaded('imagick')) { if (!\extension_loaded('imagick')) {
@ -244,7 +234,8 @@ App::get('/v1/avatars/favicon')
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3, CURLOPT_MAXREDIRS => 3,
CURLOPT_URL => $url, CURLOPT_URL => $url,
CURLOPT_USERAGENT => \sprintf(APP_USERAGENT, CURLOPT_USERAGENT => \sprintf(
APP_USERAGENT,
App::getEnv('_APP_VERSION', 'UNKNOWN'), App::getEnv('_APP_VERSION', 'UNKNOWN'),
App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY) App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
), ),
@ -305,7 +296,7 @@ App::get('/v1/avatars/favicon')
if (empty($outputHref) || empty($outputExt)) { if (empty($outputHref) || empty($outputExt)) {
$default = \parse_url($url); $default = \parse_url($url);
$outputHref = $default['scheme'].'://'.$default['host'].'/favicon.ico'; $outputHref = $default['scheme'] . '://' . $default['host'] . '/favicon.ico';
$outputExt = 'ico'; $outputExt = 'ico';
} }
@ -322,8 +313,7 @@ App::get('/v1/avatars/favicon')
->setContentType('image/x-icon') ->setContentType('image/x-icon')
->addHeader('Expires', $date) ->addHeader('Expires', $date)
->addHeader('X-Appwrite-Cache', 'miss') ->addHeader('X-Appwrite-Cache', 'miss')
->send($data) ->send($data);
;
} }
$fetch = @\file_get_contents($outputHref, false); $fetch = @\file_get_contents($outputHref, false);
@ -368,28 +358,20 @@ App::get('/v1/avatars/qr')
/** @var Utopia\Response $response */ /** @var Utopia\Response $response */
$download = ($download === '1' || $download === 'true' || $download === 1 || $download === true); $download = ($download === '1' || $download === 'true' || $download === 1 || $download === true);
/* $renderer = new ImageRenderer(
new RendererStyle($size, $margin),
new ImagickImageBackEnd('png', 100)
); */
$qropts = new QROptions([ $qropts = new QROptions([
'quietzone' => $size 'quietzone' => $size
]); ]);
$qrcode = new QRCode($qropts); $qrcode = new QRCode($qropts);
$qrcode->render($text); $qrcode->render($text);
/* $writer = new Writer($renderer); */
if ($download) { if ($download) {
$response->addHeader('Content-Disposition', 'attachment; filename="qr.png"'); $response->addHeader('Content-Disposition', 'attachment; filename="qr.png"');
} }
$response $response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->setContentType('image/png') ->setContentType('image/png')
/* ->send($writer->writeString($text)) */ ->send($qrcode->render($text));
;
}, ['response']); }, ['response']);
App::get('/v1/avatars/initials') App::get('/v1/avatars/initials')
@ -423,7 +405,7 @@ App::get('/v1/avatars/initials')
['color' => '#610008', 'background' => '#f6d2d5'] // RED ['color' => '#610008', 'background' => '#f6d2d5'] // RED
]; ];
$rand = \rand(0, \count($themes)-1); $rand = \rand(0, \count($themes) - 1);
$name = (!empty($name)) ? $name : $user->getAttribute('name', $user->getAttribute('email', '')); $name = (!empty($name)) ? $name : $user->getAttribute('name', $user->getAttribute('email', ''));
$words = \explode(' ', \strtoupper($name)); $words = \explode(' ', \strtoupper($name));
@ -440,23 +422,23 @@ App::get('/v1/avatars/initials')
} }
$length = \count($words); $length = \count($words);
$rand = \substr($code,-1); $rand = \substr($code, -1);
$background = (!empty($background)) ? '#'.$background : $themes[$rand]['background']; $background = (!empty($background)) ? '#' . $background : $themes[$rand]['background'];
$color = (!empty($color)) ? '#'.$color : $themes[$rand]['color']; $color = (!empty($color)) ? '#' . $color : $themes[$rand]['color'];
$image = new \Imagick(); $image = new \Imagick();
$draw = new \ImagickDraw(); $draw = new \ImagickDraw();
$fontSize = \min($width, $height) / 2; $fontSize = \min($width, $height) / 2;
$draw->setFont(__DIR__."/../../../public/fonts/poppins-v9-latin-500.ttf"); $draw->setFont(__DIR__ . "/../../../public/fonts/poppins-v9-latin-500.ttf");
$image->setFont(__DIR__."/../../../public/fonts/poppins-v9-latin-500.ttf"); $image->setFont(__DIR__ . "/../../../public/fonts/poppins-v9-latin-500.ttf");
$draw->setFillColor(new \ImagickPixel($color)); $draw->setFillColor(new \ImagickPixel($color));
$draw->setFontSize($fontSize); $draw->setFontSize($fontSize);
$draw->setTextAlignment(\Imagick::ALIGN_CENTER); $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
$draw->annotation($width / 1.97, ($height / 2) + ($fontSize / 3), $initials); $draw->annotation($width / 1.97, ($height / 2) + ($fontSize / 3), $initials);
$image->newImage($width, $height, $background); $image->newImage($width, $height, $background);
$image->setImageFormat("png"); $image->setImageFormat("png");
$image->drawImage($draw); $image->drawImage($draw);
@ -464,8 +446,7 @@ App::get('/v1/avatars/initials')
//$image->setImageCompressionQuality(9 - round(($quality / 100) * 9)); //$image->setImageCompressionQuality(9 - round(($quality / 100) * 9));
$response $response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)).' GMT') // 45 days cache ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->setContentType('image/png') ->setContentType('image/png')
->send($image->getImageBlob()) ->send($image->getImageBlob());
; }, ['response', 'user']);
}, ['response', 'user']);

View file

@ -49,9 +49,8 @@
"dragonmantank/cron-expression": "3.0.1", "dragonmantank/cron-expression": "3.0.1",
"domnikl/statsd": "3.0.*", "domnikl/statsd": "3.0.*",
"influxdb/influxdb-php": "1.15.*", "influxdb/influxdb-php": "1.15.*",
"bacon/bacon-qr-code": "2.0.2",
"phpmailer/phpmailer": "6.1.7", "phpmailer/phpmailer": "6.1.7",
"chillerlan/php-qrcode": "^4.1" "chillerlan/php-qrcode": "^4.2"
}, },
"require-dev": { "require-dev": {
"swoole/ide-helper": "4.5.4", "swoole/ide-helper": "4.5.4",