refactor downloader

This commit is contained in:
Pig Fang 2020-01-14 10:58:20 +08:00
parent 16916fd006
commit 078fcf47ec
8 changed files with 110 additions and 179 deletions

View File

@ -2,9 +2,10 @@
namespace App\Http\Controllers;
use App\Services\PackageManager;
use App\Services\Plugin;
use App\Services\PluginManager;
use App\Services\Unzip;
use Composer\CaBundle\CaBundle;
use Composer\Semver\Comparator;
use Exception;
use Illuminate\Http\Request;
@ -58,7 +59,7 @@ class MarketController extends Controller
return $plugins;
}
public function download(Request $request, PluginManager $manager, PackageManager $package)
public function download(Request $request, PluginManager $manager, Unzip $unzip)
{
$name = $request->get('name');
$metadata = $this->getPluginMetadata($name);
@ -77,12 +78,15 @@ class MarketController extends Controller
}
$url = $metadata['dist']['url'];
$filename = Arr::last(explode('/', $url));
$pluginsDir = $manager->getPluginsDirs()->first();
$path = storage_path("packages/$name".'_'.$metadata['version'].'.zip');
try {
$package->download($url, $path, $metadata['dist']['shasum'])->extract($pluginsDir);
$this->guzzle->get($url, [
'sink' => $path,
'verify' => CaBundle::getSystemCaRootBundlePath(),
]);
$unzip->extract($path, $pluginsDir);
} catch (Exception $e) {
return json($e->getMessage(), 1);
}

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers;
use App\Services\PackageManager;
use App\Services\Unzip;
use Cache;
use Composer\CaBundle\CaBundle;
use Composer\Semver\Comparator;
@ -33,20 +33,21 @@ class UpdateController extends Controller
]);
}
public function download(
PackageManager $package,
Filesystem $filesystem,
Client $client
) {
public function download(Unzip $unzip, Filesystem $filesystem, Client $client)
{
$info = $this->getUpdateInfo($client);
if (!$info['ok'] || !$this->canUpdate($info['info'])['can']) {
return json(trans('admin.update.info.up-to-date'), 1);
}
$info = $info['info'];
$path = storage_path('packages/bs_'.$info['latest'].'.zip');
$path = tempnam(sys_get_temp_dir(), 'bs');
try {
$package->download($info['url'], $path)->extract(base_path());
$client->get($info['url'], [
'sink' => $path,
'verify' => CaBundle::getSystemCaRootBundlePath(),
]);
$unzip->extract($path, base_path());
// Delete options cache. This allows us to update the version.
$filesystem->delete(storage_path('options.php'));
@ -55,7 +56,7 @@ class UpdateController extends Controller
} catch (Exception $e) {
report($e);
return json($e->getMessage(), 1);
return json(trans('admin.download.errors.download', ['error' => $e->getMessage()]), 1);
}
}
@ -88,6 +89,7 @@ class UpdateController extends Controller
'verify' => CaBundle::getSystemCaRootBundlePath(),
]);
$info = json_decode($response->getBody(), true);
if (Arr::get($info, 'spec') === self::SPEC) {
return ['ok' => true, 'info' => $info];
} else {

View File

@ -1,67 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Filesystem\Filesystem;
use ZipArchive;
class PackageManager
{
protected $path;
/** @var Client */
protected $guzzle;
/** @var Filesystem */
protected $filesystem;
/** @var ZipArchive */
protected $zipper;
public function __construct(
Client $guzzle,
Filesystem $filesystem,
ZipArchive $zipper
) {
$this->guzzle = $guzzle;
$this->filesystem = $filesystem;
$this->zipper = $zipper;
}
public function download(string $url, string $path, $shasum = null): self
{
$this->path = $path;
try {
$this->guzzle->request('GET', $url, [
'sink' => $path,
'verify' => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
]);
} catch (Exception $e) {
throw new Exception(trans('admin.download.errors.download', ['error' => $e->getMessage()]));
}
if (is_string($shasum) && sha1_file($path) !== strtolower($shasum)) {
$this->filesystem->delete($path);
throw new Exception(trans('admin.download.errors.shasum'));
}
return $this;
}
public function extract(string $destination): void
{
$zip = $this->zipper;
$resource = $zip->open($this->path);
if ($resource === true && $zip->extractTo($destination)) {
$zip->close();
$this->filesystem->delete($this->path);
} else {
throw new Exception(trans('admin.download.errors.unzip'));
}
}
}

37
app/Services/Unzip.php Normal file
View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
use Illuminate\Filesystem\Filesystem;
use ZipArchive;
class Unzip
{
/** @var Filesystem */
protected $filesystem;
/** @var ZipArchive */
protected $zipper;
public function __construct(Filesystem $filesystem, ZipArchive $zipper)
{
$this->filesystem = $filesystem;
$this->zipper = $zipper;
}
public function extract(string $file, string $destination): void
{
$zip = $this->zipper;
$resource = $zip->open($file);
if ($resource === true && $zip->extractTo($destination)) {
$zip->close();
$this->filesystem->delete($file);
} else {
throw new Exception(trans('admin.download.errors.unzip'));
}
}
}

View File

@ -2,9 +2,9 @@
namespace Tests;
use App\Services\PackageManager;
use App\Services\Plugin;
use App\Services\PluginManager;
use App\Services\Unzip;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
@ -64,25 +64,19 @@ class MarketControllerTest extends TestCase
'dist' => ['url' => 'http://nowhere.test/', 'shasum' => 'deadbeef'],
],
]]);
$this->appendToGuzzleQueue([new Response(200, [], $fakeRegistry)]);
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')
->withArgs(['http://nowhere.test/', storage_path('packages/fake_0.0.0.zip'), 'deadbeef'])
->once()
->andThrow(new \Exception());
});
$this->appendToGuzzleQueue([
new Response(200, [], $fakeRegistry),
new Response(404),
]);
$this->postJson('/admin/plugins/market/download', ['name' => 'fake'])
->assertJson(['code' => 1]);
$this->appendToGuzzleQueue([new Response(200, [], $fakeRegistry)]);
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')
->withArgs(['http://nowhere.test/', storage_path('packages/fake_0.0.0.zip'), 'deadbeef'])
->once()
->andReturnSelf();
$mock->shouldReceive('extract')
->with(base_path('plugins'))
->once();
$this->appendToGuzzleQueue([
new Response(200, [], $fakeRegistry),
new Response(200),
]);
$this->mock(Unzip::class, function ($mock) {
$mock->shouldReceive('extract')->once();
});
$this->postJson('/admin/plugins/market/download', ['name' => 'fake'])
->assertJson([

View File

@ -2,7 +2,7 @@
namespace Tests;
use App\Services\PackageManager;
use App\Services\Unzip;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
@ -69,15 +69,13 @@ class UpdateControllerTest extends TestCase
// Download
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
new Response(404),
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
new Response(200),
]);
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')->andThrow(new \Exception('ddd'));
});
$this->postJson('/admin/update/download')->assertJson(['code' => 1]);
$this->mock(PackageManager::class, function ($mock) {
$mock->shouldReceive('download')->andReturnSelf();
$mock->shouldReceive('extract')->andReturn(true);
$this->mock(Unzip::class, function ($mock) {
$mock->shouldReceive('extract')->once()->andReturn();
});
$this->mock(\Illuminate\Filesystem\Filesystem::class, function ($mock) {
$mock->shouldReceive('delete')->with(storage_path('options.php'))->once();

View File

@ -1,75 +0,0 @@
<?php
namespace Tests;
use App\Services\PackageManager;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Filesystem\Filesystem;
use ZipArchive;
class PackageManagerTest extends TestCase
{
public function testDownload()
{
$mock = new MockHandler([
new Response(200, [], 'contents'),
new RequestException('error', new Request('GET', 'url')),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->instance(Client::class, $client);
$package = resolve(PackageManager::class);
$this->assertInstanceOf(
PackageManager::class,
$package->download('url', storage_path('packages/temp'))
);
$this->expectExceptionMessage(trans('admin.download.errors.download', ['error' => 'error']));
$package->download('url', storage_path('packages/temp'));
}
public function testShasumCheck()
{
$mock = new MockHandler([new Response(200, [], 'contents')]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->instance(Client::class, $client);
$package = resolve(PackageManager::class);
$this->expectExceptionMessage(trans('admin.download.errors.shasum'));
$package->download('url', storage_path('packages/temp'), 'deadbeef');
}
public function testExtract()
{
$this->mock(ZipArchive::class, function ($mock) {
$mock->shouldReceive('open')
->twice()
->andReturn(true, false);
$mock->shouldReceive('extractTo')
->with('dest')
->once()
->andReturn(true);
$mock->shouldReceive('close')->once();
});
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('delete')->once();
});
$package = resolve(PackageManager::class);
// The call below is expected success.
$package->extract('dest');
$this->expectException(Exception::class);
$package->extract('dest');
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Tests;
use App\Services\Unzip;
use Exception;
use Illuminate\Filesystem\Filesystem;
use ZipArchive;
class UnzipTest extends TestCase
{
public function testExtract()
{
$this->mock(ZipArchive::class, function ($mock) {
$mock->shouldReceive('open')
->twice()
->andReturn(true, false);
$mock->shouldReceive('extractTo')
->with('dest')
->once()
->andReturn(true);
$mock->shouldReceive('close')->once();
});
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('delete')->once();
});
/** @var Unzip */
$unzip = resolve(Unzip::class);
// The call below is expected success.
$unzip->extract('f.zip', 'dest');
$this->expectException(Exception::class);
$unzip->extract('f.zip', 'dest');
}
}