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 3 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
25 changes: 24 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ RUN composer update \

FROM php:8.0-cli-alpine as compile

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

RUN apk add --no-cache \
git \
Expand All @@ -34,20 +36,41 @@ 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
&& phpize \
&& ./configure \
&& make && make install

FROM compile as final

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 mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
&& echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini \
&& echo "memory_limit=1024M" >> $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/

# Add Source Code
COPY . /usr/src/code
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ext-fileinfo": "*",
"ext-zlib": "*",
"ext-zstd": "*",
"ext-xz": "*",
"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);
}
}