Slam / flysystem-compress-and-encrypt-proxy
Compress and Encrypt files and streams before saving them to the final Flysystem destination.
Installation
To install with composer run the following command:
$ composer require slam/flysystem-compress-and-encrypt-proxy
Usage
use SlamCompressAndEncryptProxy\CompressAdapter;
use SlamCompressAndEncryptProxy\EncryptAdapter;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
// Create a strong key and save it somewhere
$key = EncryptAdapter::generateKey();
// Create the final FilesystemAdapter, for example Aws S3
$remoteAdapter = new AwsS3V3Adapter(/* ... */);
$adapter = new CompressAdapter(new EncryptAdapter(
$remoteAdapter,
$key
));
// The FilesystemOperator
$filesystem = new \League\Flysystem\Filesystem($adapter);
// Upload a file, with stream
$handle = fopen('my-huge-file.txt', 'r');
$filesystem->writeStream('data.txt', $handle);
fclose($handle);
// Remotely a data.txt.gz.encrypted file has now been created
// Download a file, with stream
$handle = $filesystem->readStream('data.txt');
file_put_contents('my-huge-file.txt', $handle);
fclose($handle);
Streams
Both write and read operations leverage streams to keep memory usage low.
A 10 Gb mysqldump
output can be streamed into a 1 Gb dump.sql.gz.encrypted
file with a 10 Mb RAM footprint of the running php process, and no additional local disk space required.
Compression
GZip's zlib.deflate
and zlib.inflate
compression filters are used.
You can opt-out compression by using just the EncryptAdapter
.
Encryption
Sodium extension provides the backend for the encrypted stream with XChaCha20-Poly1305
algorithm.
Caveats
MIME types detection
Some Flysystem adapters like the Local one try to guess the file mime type by its nature (content or extension): in such cases it will fail due to the custom extention and the encrypted content. Other adapters like the Aws S3 one allow you to specify it manually (for ex. with the ContentType
key in the Config): it is a good idea to always manually inject it, if you like the Filesystem::mimeType($path)
call to be reliable.
File size
The file size returned relates to the compressed and encrypted file, not the original one.