This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
DownloadGoogleBinaries.php
114 lines (91 loc) · 3.37 KB
/
DownloadGoogleBinaries.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace FroshWebP\Commands;
use Phar;
use PharData;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Shopware\Commands\ShopwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class DownloadGoogleBinaries extends ShopwareCommand
{
protected function configure()
{
$this
->setName('frosh:webp:download-google-binaries')
->setDescription('Downloads google binaries that can convert images to webp')
;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$style = new SymfonyStyle($input, $output);
if (!$this->is64bit()) {
$style->error('There are no binaries for non-64-bit systems');
return 1;
}
if ($this->isLinux()) {
$packageDirectory = 'libwebp-1.0.1-linux-x86-64';
} elseif ($this->isMac()) {
$packageDirectory = 'libwebp-1.0.1-mac-10.13';
} else {
$style->error('Downloading binaries is supported for linux and mac only');
return 2;
}
$downloadedPackage = tempnam(sys_get_temp_dir(), 'libwebp') . '.tar.gz';
$url = 'https://storage.googleapis.com/downloads.webmproject.org/releases/webp/' . $packageDirectory . '.tar.gz';
copy($url, $downloadedPackage);
if (!file_exists($downloadedPackage)) {
$style->error('Downloading package failed');
return 3;
}
$cacheDownloadDir = $this->container->getParameter('shyim_web_p.cached_download_dir');
$downloadDir = $downloadedPackage . '.d';
$cwebpPath = $downloadDir . DIRECTORY_SEPARATOR . $packageDirectory . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'cwebp';
$package = new PharData($downloadedPackage, null, null, Phar::TAR | Phar::GZ);
$this->clearDirectory($downloadDir);
$package->extractTo($downloadDir);
unlink($downloadedPackage);
if (!file_exists($cwebpPath)) {
$style->error('Downloaded package does not contain a cwebp executable');
return 4;
}
$this->clearDirectory($cacheDownloadDir);
rename($downloadDir . DIRECTORY_SEPARATOR . $packageDirectory, $cacheDownloadDir);
$this->clearDirectory($downloadDir);
return 0;
}
protected function isLinux()
{
return strtolower(php_uname('s')) === 'linux';
}
protected function isMac()
{
return strtolower(php_uname('s')) === 'darwin';
}
protected function is64bit()
{
return strpos(php_uname('m'), '64') !== false;
}
protected function clearDirectory($directory)
{
if (file_exists($directory)) {
$it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($directory);
}
}
}