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: add xz compression #59

Merged
merged 8 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 20 additions & 0 deletions Dockerfile
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ RUN composer update \
FROM php:8.0-cli-alpine as compile

ENV PHP_ZSTD_VERSION="master" \
PHP_XZ_VERSION=5.2.7 \
PHP_EXT_XZ_VERSION=1.1.2 \
PHP_SNAPPY_VERSION=bfefe4906e0abb1f6cc19005b35f9af5240d9025

RUN apk add --no-cache \
Expand All @@ -35,6 +37,22 @@ RUN git clone --recursive --depth 1 --branch $PHP_ZSTD_VERSION https://github.co
&& ./configure --with-libzstd \
&& make && make install

## Xz Extension
FROM compile as xz
RUN wget https://tukaani.org/xz/xz-${PHP_XZ_VERSION}.tar.xz -O xz.tar.xz \
&& tar -xJf xz.tar.xz \
&& rm xz.tar.xz \
&& ( \
cd xz-${PHP_XZ_VERSION} \
&& ./configure \
&& make \
&& make install \
) \
&& rm -r xz-${PHP_XZ_VERSION}

RUN git clone https://github.com/codemasher/php-ext-xz.git --branch ${PHP_EXT_XZ_VERSION} \
&& cd php-ext-xz \
unickorn marked this conversation as resolved.
Show resolved Hide resolved

## Snappy Extension
FROM compile AS snappy
RUN git clone --recursive --depth 1 https://github.com/kjdev/php-ext-snappy.git \
Expand All @@ -51,6 +69,7 @@ LABEL maintainer="[email protected]"
WORKDIR /usr/src/code

RUN echo extension=zstd.so >> /usr/local/etc/php/conf.d/zstd.ini
RUN echo extension=xz.so >> /usr/local/etc/php/conf.d/xz.ini
RUN echo extension=snappy.so >> /usr/local/etc/php/conf.d/snappy.ini

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
Expand All @@ -59,6 +78,7 @@ RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \

COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=zstd /usr/local/lib/php/extensions/no-debug-non-zts-20200930/zstd.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
COPY --from=xz /usr/local/lib/php/extensions/no-debug-non-zts-20200930/xz.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
COPY --from=snappy /usr/local/lib/php/extensions/no-debug-non-zts-20200930/snappy.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/

# Add Source Code
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ext-fileinfo": "*",
"ext-zlib": "*",
"ext-zstd": "*",
"ext-xz": "*",
"ext-snappy": "*",
"php": ">=8.0",
"utopia-php/framework": "0.*.*"
Expand Down
40 changes: 40 additions & 0 deletions src/Storage/Compression/Algorithms/XZ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Utopia\Storage\Compression\Algorithms;

use Utopia\Storage\Compression\Compression;

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

/**
* Compress.
*
* @param string $data
*
* @return string
*/
public function compress(string $data): string
{
return \xzencode($data);
}

/**
* Decompress.
*
* @param string $data
*
* @return string
*/
public function decompress(string $data): string
{
return \xzdecode($data);
}
}
77 changes: 77 additions & 0 deletions tests/Storage/Compression/Algorithms/XZTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Utopia\Tests\Storage\Compression\Algorithms;

use Utopia\Storage\Compression\Algorithms\XZ;
use PHPUnit\Framework\TestCase;

class XZTest extends TestCase
{
protected XZ $object;

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

public function tearDown(): void
{
}

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

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

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

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

$this->assertGreaterThan($dataSize, $demoSize);

$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, 2981000);

$this->assertGreaterThan($dataSize, $demoSize);

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

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