-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from mattschlosser/feat-3995-add-brotli-compre…
…ssion Feat 3995 add brotli compression
- Loading branch information
Showing
7 changed files
with
1,269 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,106 @@ | ||
<?php | ||
|
||
namespace Utopia\Storage\Compression\Algorithms; | ||
|
||
use Utopia\Storage\Compression\Compression; | ||
|
||
class Brotli extends Compression | ||
{ | ||
protected int $level = BROTLI_COMPRESS_LEVEL_DEFAULT; | ||
|
||
protected int $mode = BROTLI_GENERIC; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'brotli'; | ||
} | ||
|
||
/** | ||
* Get the compression level. | ||
* | ||
* @return int | ||
*/ | ||
public function getLevel(): int | ||
{ | ||
return $this->level; | ||
} | ||
|
||
/** | ||
* Sets the brotli compression mode to generic. | ||
* | ||
* This is the default mode | ||
*/ | ||
public function useGenericMode(): void | ||
{ | ||
$this->mode = BROTLI_GENERIC; | ||
} | ||
|
||
/** | ||
* Sets the brotli compression mode to UTF-8 text mode. | ||
* | ||
* Optimizes compression for UTF-8 formatted text | ||
* | ||
* @link https://github.com/kjdev/php-ext-brotli#parameters | ||
*/ | ||
public function useTextMode(): void | ||
{ | ||
$this->mode = BROTLI_TEXT; | ||
} | ||
|
||
/** | ||
* Sets the brotli compression mode to font mode. | ||
* | ||
* Optimized compression for WOFF 2.0 Fonts | ||
* | ||
* @link https://github.com/kjdev/php-ext-brotli#parameters | ||
*/ | ||
public function useFontMode(): void | ||
{ | ||
$this->mode = BROTLI_FONT; | ||
} | ||
|
||
/** | ||
* Set the compression level. | ||
* | ||
* Allow values from 0 up to a current max of 11. | ||
* | ||
* @param int $level | ||
* @return void | ||
*/ | ||
public function setLevel(int $level): void | ||
{ | ||
$min = BROTLI_COMPRESS_LEVEL_MIN; | ||
$max = BROTLI_COMPRESS_LEVEL_MAX; | ||
if ($level < $min || $level > $max) { | ||
throw new \InvalidArgumentException("Level must be between {$min} and {$max}"); | ||
} | ||
$this->level = $level; // $level; | ||
} | ||
|
||
/** | ||
* Compress. | ||
* | ||
* @param string $data | ||
* | ||
* @return string | ||
*/ | ||
public function compress(string $data):string | ||
{ | ||
return \brotli_compress($data, $this->getLevel(), $this->mode); | ||
} | ||
|
||
/** | ||
* Decompress. | ||
* | ||
* @param string $data | ||
* | ||
* @return string | ||
*/ | ||
public function decompress(string $data):string | ||
{ | ||
return \brotli_uncompress($data); | ||
} | ||
} |
This file contains 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,108 @@ | ||
<?php | ||
|
||
namespace Utopia\Tests\Storage\Compression\Algorithms; | ||
|
||
use InvalidArgumentException; | ||
use Utopia\Storage\Compression\Algorithms\Brotli; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BrotliTest extends TestCase | ||
{ | ||
/** | ||
* @var Brotli | ||
*/ | ||
protected $object = null; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->object = new Brotli(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
} | ||
|
||
public function testName() | ||
{ | ||
$this->assertEquals($this->object->getName(), 'brotli'); | ||
} | ||
|
||
public function testErrorsWhenSettingLevel() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->object->setLevel(-1); | ||
} | ||
|
||
public function testCompressDecompressWithText() | ||
{ | ||
$demo = 'This is a demo string'; | ||
$demoSize = mb_strlen($demo, '8bit'); | ||
|
||
$data = $this->object->compress($demo); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($demoSize, 21); | ||
$this->assertEquals($dataSize, 25); | ||
|
||
$this->assertEquals($this->object->decompress($data), $demo); | ||
} | ||
|
||
public function testCompressDecompressWithLargeText() | ||
{ | ||
$demo = \file_get_contents(__DIR__ . '/../../../resources/disk-a/lorem.txt'); | ||
$demoSize = mb_strlen($demo, '8bit'); | ||
|
||
$this->object->setLevel(8); | ||
$data = $this->object->compress($demo); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($demoSize, 386795); | ||
$this->assertEquals($dataSize, 33128); | ||
|
||
$this->assertGreaterThan($dataSize, $demoSize); | ||
|
||
$data = $this->object->decompress($data); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($dataSize, 386795); | ||
$this->assertEquals($data, $demo); | ||
} | ||
|
||
public function testCompressDecompressWithJPGImage() | ||
{ | ||
$demo = \file_get_contents(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'); | ||
$demoSize = mb_strlen($demo, '8bit'); | ||
|
||
$this->object->setLevel(8); | ||
$data = $this->object->compress($demo); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($demoSize, 599639); | ||
// brotli is not the best for images | ||
$this->assertEquals($dataSize, 599644); | ||
|
||
$data = $this->object->decompress($data); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($dataSize, 599639); | ||
} | ||
|
||
public function testCompressDecompressWithPNGImage() | ||
{ | ||
$demo = \file_get_contents(__DIR__ . '/../../../resources/disk-b/kitten-1.png'); | ||
$demoSize = mb_strlen($demo, '8bit'); | ||
|
||
$this->object->setLevel(8); | ||
$data = $this->object->compress($demo); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($demoSize, 3038056); | ||
// brotli is not the best for images | ||
$this->assertEquals($dataSize, 3038068); | ||
|
||
$data = $this->object->decompress($data); | ||
$dataSize = mb_strlen($data, '8bit'); | ||
|
||
$this->assertEquals($dataSize, 3038056); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.