Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 3996 bzip2 compression #89

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"ext-brotli": "*",
"ext-lz4": "*",
"ext-snappy": "*",
"ext-bz2":"*",
"php": ">=8.0",
"utopia-php/framework": "0.*.*",
"utopia-php/system": "0.*.*"
Expand Down
42 changes: 42 additions & 0 deletions src/Storage/Compression/Algorithms/BZIP2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Utopia\Storage\Compression\Algorithms;

use Utopia\Storage\Compression\Compression;

class BZIP2 extends Compression
{
/**
* @return string
*/
public function getName(): string
{
return 'bzip2';
}

/**
* Compress.
*
* @see https://www.php.net/manual/en/function.bzcompress.php
*
* @param string $data
* @return string
*/
public function compress(string $data): string
{
return \bzcompress($data);
}

/**
* Decompress.
*
* @see https://www.php.net/manual/en/function.bzdecompress.php
*
* @param string $data
* @return string
*/
public function decompress(string $data):string
{
return \bzdecompress($data);
}
}
73 changes: 73 additions & 0 deletions tests/Storage/Compression/Algorithms/BZIP2TEST.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this file BZIP2Test.php?

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Utopia\Tests\Storage\Compression\Algorithms;
use Utopia\Storage\Compression\Algorithms\BZIP2;
use PHPUnit\Framework\TestCase;

class BZIP2Test extends TestCase
{
/** @var BZIP2 */
protected BZIP2 $object;

public function setUp(): void
{
$this->object = new BZIP2();
}

public function testName()
{
$this->assertEquals($this->object->getName(), 'bzip2');
}

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, 58);

$this->assertEquals($this->object->decompress($data), $demo);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the testCompressDecompressWithLargeText() as seen in other compression tests?

public function testCompressDecompressWithJPGImage()
{
$demo = \file_get_contents(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg');
$demoSize = mb_strlen($demo, '8bit');

$data = $this->object->compress($demo);
$dataSize = mb_strlen($data, '8bit');

$this->assertEquals($demoSize, 599639);
$this->assertEquals($dataSize, 598565);

$this->assertGreaterThan($dataSize, $demoSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary


$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');

$data = $this->object->compress($demo);
$dataSize = mb_strlen($data, '8bit');

$this->assertEquals($demoSize, 3038056);
$this->assertEquals($dataSize, 2999345);

$this->assertGreaterThan($dataSize, $demoSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this


$data = $this->object->decompress($data);
$dataSize = mb_strlen($data, '8bit');

$this->assertEquals($dataSize, 3038056);
}
}
Loading