| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Services\Archiver\Adapters; |
| 4 |
|
| 5 |
use WPIDE\App\Services\Archiver\ArchiverInterface; |
| 6 |
use WPIDE\App\Services\Service; |
| 7 |
use WPIDE\App\Services\Storage\Filesystem as Storage; |
| 8 |
use WPIDE\App\Services\Tmpfs\TmpfsInterface; |
| 9 |
use League\Flysystem\Config as Flyconfig; |
| 10 |
use League\Flysystem\Filesystem as Flysystem; |
| 11 |
use League\Flysystem\ZipArchive\ZipArchiveAdapter; |
| 12 |
|
| 13 |
class ZipArchiver implements Service, ArchiverInterface |
| 14 |
{ |
| 15 |
/* @var $archive Flysystem */ |
| 16 |
protected $archive; |
| 17 |
|
| 18 |
/* @var $storage Storage */ |
| 19 |
protected $storage; |
| 20 |
|
| 21 |
/* @var $tmpfs TmpfsInterface */ |
| 22 |
protected $tmpfs; |
| 23 |
|
| 24 |
protected $uniqid; |
| 25 |
|
| 26 |
protected $tmp_files = []; |
| 27 |
|
| 28 |
public function __construct(TmpfsInterface $tmpfs) |
| 29 |
{ |
| 30 |
$this->tmpfs = $tmpfs; |
| 31 |
} |
| 32 |
|
| 33 |
public function init(array $config = []) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
public function createArchive(Storage $storage): string |
| 38 |
{ |
| 39 |
$this->uniqid = uniqid(); |
| 40 |
|
| 41 |
$this->archive = new Flysystem( |
| 42 |
new ZipAdapter($this->tmpfs->getFileLocation($this->uniqid)) |
| 43 |
); |
| 44 |
|
| 45 |
$this->storage = $storage; |
| 46 |
|
| 47 |
return $this->uniqid; |
| 48 |
} |
| 49 |
|
| 50 |
public function setArchive(Storage $storage, string $uniqid) |
| 51 |
{ |
| 52 |
$this->uniqid = $uniqid; |
| 53 |
|
| 54 |
$this->archive = new Flysystem( |
| 55 |
new ZipAdapter($this->tmpfs->getFileLocation($this->uniqid)) |
| 56 |
); |
| 57 |
|
| 58 |
$this->storage = $storage; |
| 59 |
} |
| 60 |
|
| 61 |
public function addEmptyDirectory(string $path, string $destination){ |
| 62 |
|
| 63 |
$path = str_replace($destination, "/", $path); |
| 64 |
$this->archive->createDir($path); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @throws \Exception |
| 69 |
*/ |
| 70 |
public function addDirectoryFromStorage(string $path, string $destination) |
| 71 |
{ |
| 72 |
$content = $this->storage->getDirectoryCollection($path, true); |
| 73 |
$path = str_replace($destination, "/", $path); |
| 74 |
$this->archive->createDir($path); |
| 75 |
|
| 76 |
foreach ($content->all() as $item) { |
| 77 |
if ($item['type'] == 'dir') { |
| 78 |
$this->archive->createDir($item['path']); |
| 79 |
} |
| 80 |
if ($item['type'] == 'file') { |
| 81 |
$this->addFileFromStorage($item['path']); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public function addFileFromStorage(string $path, string $destination) |
| 87 |
{ |
| 88 |
|
| 89 |
if(!$this->archive->has($path)){ |
| 90 |
|
| 91 |
$file_uniqid = uniqid(); |
| 92 |
|
| 93 |
$file = $this->storage->readStream($path); |
| 94 |
|
| 95 |
$this->tmpfs->write($file_uniqid, $file['stream']); |
| 96 |
|
| 97 |
$path = str_replace($destination, "/", $path); |
| 98 |
|
| 99 |
$this->archive->write($path, $this->tmpfs->getFileLocation($file_uniqid)); |
| 100 |
|
| 101 |
$this->tmp_files[] = $file_uniqid; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function uncompress(string $source, string $destination, Storage $storage) |
| 106 |
{ |
| 107 |
$name = uniqid().'.zip'; |
| 108 |
|
| 109 |
$remote_archive = $storage->readStream($source); |
| 110 |
$this->tmpfs->write($name, $remote_archive['stream']); |
| 111 |
|
| 112 |
$archive = new Flysystem( |
| 113 |
new ZipAdapter($this->tmpfs->getFileLocation($name)) |
| 114 |
); |
| 115 |
|
| 116 |
$contents = $archive->listContents('/', true); |
| 117 |
|
| 118 |
$firstItem = current($contents); |
| 119 |
$existingDirBackupPath = $firstItem['path'] . '--tmp--'; |
| 120 |
|
| 121 |
if($storage->isDir($destination.'/'.$firstItem['path'])) { |
| 122 |
$storage->rename($destination, $firstItem['path'], $existingDirBackupPath); |
| 123 |
} |
| 124 |
|
| 125 |
foreach ($contents as $item) { |
| 126 |
$stream = null; |
| 127 |
if ($item['type'] == 'dir') { |
| 128 |
$storage->createDir($destination, $item['path']); |
| 129 |
} |
| 130 |
if ($item['type'] == 'file') { |
| 131 |
$stream = $archive->readStream($item['path']); |
| 132 |
$storage->storeStream($destination.'/'.$item['dirname'], $item['basename'], $stream); |
| 133 |
} |
| 134 |
if (is_resource($stream)) { |
| 135 |
fclose($stream); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if($storage->isDir($destination.'/'.$existingDirBackupPath)) { |
| 140 |
if ($storage->isDir($destination.'/'.$firstItem['path'])) { |
| 141 |
$storage->deleteDir($destination.'/'.$existingDirBackupPath); |
| 142 |
} else { |
| 143 |
$storage->rename($destination, $existingDirBackupPath, $firstItem['path']); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$this->tmpfs->remove($name); |
| 148 |
} |
| 149 |
|
| 150 |
public function closeArchive() |
| 151 |
{ |
| 152 |
$this->archive->getAdapter()->getArchive()->close(); |
| 153 |
|
| 154 |
foreach ($this->tmp_files as $file) { |
| 155 |
$this->tmpfs->remove($file); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public function storeArchive($destination, $name) |
| 160 |
{ |
| 161 |
$this->closeArchive(); |
| 162 |
|
| 163 |
$file = $this->tmpfs->readStream($this->uniqid); |
| 164 |
|
| 165 |
$this->storage->storeStream($destination, $name, $file['stream']); |
| 166 |
if (is_resource($file['stream'])) { |
| 167 |
fclose($file['stream']); |
| 168 |
} |
| 169 |
|
| 170 |
$this->tmpfs->remove($this->uniqid); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
class ZipAdapter extends ZipArchiveAdapter |
| 175 |
{ |
| 176 |
public function write($path, $contents, Flyconfig $config) |
| 177 |
{ |
| 178 |
$location = $this->applyPathPrefix($path); |
| 179 |
|
| 180 |
// using addFile instead of addFromString |
| 181 |
// is more memory efficient |
| 182 |
$this->archive->addFile($contents, $location); |
| 183 |
|
| 184 |
return compact('path', 'contents'); |
| 185 |
} |
| 186 |
} |
| 187 |
|