2017-08-20 01:40:00 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-08-20 01:40:00 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-20 01:40:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Packs;
|
|
|
|
|
|
|
|
use ZipArchive;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Pterodactyl\Exceptions\Service\Pack\ZipExtractionException;
|
|
|
|
use Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException;
|
|
|
|
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
|
|
|
|
use Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException;
|
|
|
|
use Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException;
|
|
|
|
|
|
|
|
class TemplateUploadService
|
|
|
|
{
|
|
|
|
const VALID_UPLOAD_TYPES = [
|
|
|
|
'application/zip',
|
|
|
|
'text/plain',
|
|
|
|
'application/json',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \ZipArchive
|
|
|
|
*/
|
|
|
|
protected $archive;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Packs\PackCreationService
|
|
|
|
*/
|
|
|
|
protected $creationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TemplateUploadService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\Packs\PackCreationService $creationService
|
|
|
|
* @param \ZipArchive $archive
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
PackCreationService $creationService,
|
|
|
|
ZipArchive $archive
|
|
|
|
) {
|
|
|
|
$this->archive = $archive;
|
|
|
|
$this->creationService = $creationService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process an uploaded file to create a new pack from a JSON or ZIP format.
|
|
|
|
*
|
2017-10-07 23:08:19 +00:00
|
|
|
* @param int $egg
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\UploadedFile $file
|
2017-08-20 01:40:00 +00:00
|
|
|
* @return \Pterodactyl\Models\Pack
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\ZipExtractionException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException
|
|
|
|
*/
|
2017-10-07 23:08:19 +00:00
|
|
|
public function handle($egg, UploadedFile $file)
|
2017-08-20 01:40:00 +00:00
|
|
|
{
|
|
|
|
if (! $file->isValid()) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new InvalidFileUploadException(trans('exceptions.packs.invalid_upload'));
|
2017-08-20 01:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! in_array($file->getMimeType(), self::VALID_UPLOAD_TYPES)) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new InvalidFileMimeTypeException(trans('exceptions.packs.invalid_mime', [
|
2017-08-20 01:40:00 +00:00
|
|
|
'type' => implode(', ', self::VALID_UPLOAD_TYPES),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file->getMimeType() === 'application/zip') {
|
2017-10-07 23:08:19 +00:00
|
|
|
return $this->handleArchive($egg, $file);
|
2017-08-20 01:40:00 +00:00
|
|
|
} else {
|
|
|
|
$json = json_decode($file->openFile()->fread($file->getSize()), true);
|
2017-10-07 23:08:19 +00:00
|
|
|
$json['egg_id'] = $egg;
|
2017-08-20 01:40:00 +00:00
|
|
|
|
|
|
|
return $this->creationService->handle($json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a ZIP file to create a pack and stored archive.
|
|
|
|
*
|
2017-10-07 23:08:19 +00:00
|
|
|
* @param int $egg
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\UploadedFile $file
|
2017-08-20 01:40:00 +00:00
|
|
|
* @return \Pterodactyl\Models\Pack
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\ZipExtractionException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException
|
|
|
|
*/
|
2017-10-07 23:08:19 +00:00
|
|
|
protected function handleArchive($egg, $file)
|
2017-08-20 01:40:00 +00:00
|
|
|
{
|
|
|
|
if (! $this->archive->open($file->getRealPath())) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new UnreadableZipArchiveException(trans('exceptions.packs.unreadable'));
|
2017-08-20 01:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->archive->locateName('import.json') || ! $this->archive->locateName('archive.tar.gz')) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new InvalidPackArchiveFormatException(trans('exceptions.packs.invalid_archive_exception'));
|
2017-08-20 01:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode($this->archive->getFromName('import.json'), true);
|
2017-10-07 23:08:19 +00:00
|
|
|
$json['egg_id'] = $egg;
|
2017-08-20 01:40:00 +00:00
|
|
|
|
|
|
|
$pack = $this->creationService->handle($json);
|
|
|
|
if (! $this->archive->extractTo(storage_path('app/packs/' . $pack->uuid), 'archive.tar.gz')) {
|
|
|
|
// @todo delete the pack that was created.
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new ZipExtractionException(trans('exceptions.packs.zip_extraction'));
|
2017-08-20 01:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->archive->close();
|
|
|
|
|
|
|
|
return $pack;
|
|
|
|
}
|
|
|
|
}
|