View source
<?php
namespace Drupal\backup_migrate\Core\Filter;
use Drupal\backup_migrate\Core\Config\Config;
use Drupal\backup_migrate\Core\Plugin\FileProcessorInterface;
use Drupal\backup_migrate\Core\Plugin\FileProcessorTrait;
use Drupal\backup_migrate\Core\Plugin\PluginBase;
use Drupal\backup_migrate\Core\File\BackupFileReadableInterface;
use Drupal\backup_migrate\Core\File\BackupFileWritableInterface;
class CompressionFilter extends PluginBase implements FileProcessorInterface {
use FileProcessorTrait;
public function supportedOps() {
return [
'getFileTypes' => [],
'backupSettings' => [],
'afterBackup' => [
'weight' => 100,
],
'beforeRestore' => [
'weight' => -100,
],
];
}
public function getFileTypes() {
return [
[
"gzip" => [
"extension" => "gz",
"filemime" => "application/x-gzip",
'ops' => [
'backup',
'restore',
],
],
"bzip" => [
"extension" => "bz",
"filemime" => "application/x-bzip",
'ops' => [
'backup',
'restore',
],
],
"bzip2" => [
"extension" => "bz2",
"filemime" => "application/x-bzip",
'ops' => [
'backup',
'restore',
],
],
"zip" => [
"extension" => "zip",
"filemime" => "application/zip",
'ops' => [
'backup',
'restore',
],
],
],
];
}
public function configSchema(array $params = []) {
$schema = [];
if ($params['operation'] == 'backup') {
$schema['groups']['file'] = [
'title' => 'Backup File',
];
$compression_options = $this
->availableCompressionAlgorithms();
$schema['fields']['compression'] = [
'group' => 'file',
'type' => 'enum',
'title' => 'Compression',
'options' => $compression_options,
'actions' => [
'backup',
],
];
}
return $schema;
}
public function configDefaults() {
return new Config([
'compression' => $this
->defaultCompressionAlgorithm(),
]);
}
public function afterBackup(BackupFileReadableInterface $file) {
$out = $success = FALSE;
if ($this
->confGet('compression') == 'gzip') {
$out = $this
->getTempFileManager()
->pushExt($file, 'gz');
$success = $this
->gzipEncode($file, $out);
}
if ($this
->confGet('compression') == 'bzip') {
$out = $this
->getTempFileManager()
->pushExt($file, 'bz2');
$success = $this
->bzipEncode($file, $out);
}
if ($this
->confGet('compression') == 'zip') {
$out = $this
->getTempFileManager()
->pushExt($file, 'zip');
$success = $this
->zipEncode($file, $out);
}
if ($out && $success) {
$out
->setMeta('filesize_uncompressed', $file
->getMeta('filesize'));
$out
->setMeta('compression', $this
->confGet('compression'));
return $out;
}
return $file;
}
public function beforeRestore(BackupFileReadableInterface $file) {
$out = $file;
$type = $file
->getExtLast();
switch (strtolower($type)) {
case "gz":
case "gzip":
$out = $this
->getTempFileManager()
->popExt($file);
$this
->gzipDecode($file, $out);
break;
case "bz":
case "bz2":
case "bzip":
case "bzip2":
$out = $this
->getTempFileManager()
->popExt($file);
$this
->bzipDecode($file, $out);
break;
case "zip":
$out = $this
->getTempFileManager()
->popExt($file);
$this
->zipDecode($file, $out);
break;
}
return $out;
}
protected function gzipEncode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
$success = FALSE;
if (!$success && function_exists("gzopen")) {
if (($fp_out = gzopen($to
->realpath(), 'wb9')) && $from
->openForRead()) {
while ($data = $from
->readBytes(1024 * 512)) {
gzwrite($fp_out, $data);
}
$success = TRUE;
$from
->close();
gzclose($fp_out);
$fileszc = filesize(\Drupal::service('file_system')
->realpath($to
->realpath()));
$to
->setMeta('filesize', $fileszc);
}
}
return $success;
}
protected function gzipDecode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
$success = FALSE;
if (!$success && function_exists("gzopen")) {
if ($fp_in = gzopen($from
->realpath(), 'rb9')) {
while (!feof($fp_in)) {
$to
->write(gzread($fp_in, 1024 * 512));
}
$success = TRUE;
gzclose($fp_in);
$to
->close();
}
}
return $success;
}
protected function bzipEncode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
$success = FALSE;
if (!$success && function_exists("bzopen")) {
if (($fp_out = bzopen($to
->realpath(), 'w')) && $from
->openForRead()) {
while ($data = $from
->readBytes(1024 * 512)) {
bzwrite($fp_out, $data);
}
$success = TRUE;
$from
->close();
bzclose($fp_out);
$fileszc = filesize(\Drupal::service('file_system')
->realpath($to
->realpath()));
$to
->setMeta('filesize', $fileszc);
}
}
return $success;
}
protected function bzipDecode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
$success = FALSE;
if (!$success && function_exists("bzopen")) {
if ($fp_in = bzopen($from
->realpath(), 'r')) {
while (!feof($fp_in)) {
$to
->write(bzread($fp_in, 1024 * 512));
}
$success = TRUE;
bzclose($fp_in);
$to
->close();
}
}
return $success;
}
protected function zipEncode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
$success = FALSE;
if (class_exists('ZipArchive')) {
$zip = new \ZipArchive();
$res = $zip
->open(\Drupal::service('file_system')
->realpath($to
->realpath()), constant("ZipArchive::CREATE"));
if ($res === TRUE) {
$zip
->addFile(\Drupal::service('file_system')
->realpath($from
->realpath()), $from
->getFullName());
}
$success = $zip
->close();
}
$fileszc = filesize(\Drupal::service('file_system')
->realpath($to
->realpath()));
$to
->setMeta('filesize', $fileszc);
return $success;
}
protected function zipDecode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
$success = FALSE;
if (class_exists('ZipArchive')) {
$zip = new \ZipArchive();
if ($zip
->open(\Drupal::service('file_system')
->realpath($from
->realpath()))) {
$filename = $zip
->getNameIndex(0);
if ($fp_in = $zip
->getStream($filename)) {
while (!feof($fp_in)) {
$to
->write(fread($fp_in, 1024 * 512));
}
fclose($fp_in);
$success = $to
->close();
}
}
return $success;
}
}
protected function availableCompressionAlgorithms() {
$compression_options = [
"none" => "No Compression",
];
if (function_exists("gzencode")) {
$compression_options['gzip'] = "GZip";
}
if (function_exists("bzcompress")) {
$compression_options['bzip'] = "BZip";
}
if (class_exists('ZipArchive')) {
$compression_options['zip'] = "Zip";
}
return $compression_options;
}
protected function defaultCompressionAlgorithm() {
$available = array_keys($this
->availableCompressionAlgorithms());
array_shift($available);
$out = array_shift($available);
return $out ? $out : 'none';
}
}