forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateUploadService.php
More file actions
125 lines (109 loc) · 4.46 KB
/
TemplateUploadService.php
File metadata and controls
125 lines (109 loc) · 4.46 KB
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
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
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.
*
* @param int $option
* @param \Illuminate\Http\UploadedFile $file
* @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
*/
public function handle($option, UploadedFile $file)
{
if (! $file->isValid()) {
throw new InvalidFileUploadException(trans('exceptions.packs.invalid_upload'));
}
if (! in_array($file->getMimeType(), self::VALID_UPLOAD_TYPES)) {
throw new InvalidFileMimeTypeException(trans('exceptions.packs.invalid_mime', [
'type' => implode(', ', self::VALID_UPLOAD_TYPES),
]));
}
if ($file->getMimeType() === 'application/zip') {
return $this->handleArchive($option, $file);
} else {
$json = json_decode($file->openFile()->fread($file->getSize()), true);
$json['option_id'] = $option;
return $this->creationService->handle($json);
}
}
/**
* Process a ZIP file to create a pack and stored archive.
*
* @param int $option
* @param \Illuminate\Http\UploadedFile $file
* @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
*/
protected function handleArchive($option, $file)
{
if (! $this->archive->open($file->getRealPath())) {
throw new UnreadableZipArchiveException(trans('exceptions.packs.unreadable'));
}
if (! $this->archive->locateName('import.json') || ! $this->archive->locateName('archive.tar.gz')) {
throw new InvalidPackArchiveFormatException(trans('exceptions.packs.invalid_archive_exception'));
}
$json = json_decode($this->archive->getFromName('import.json'), true);
$json['option_id'] = $option;
$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.
throw new ZipExtractionException(trans('exceptions.packs.zip_extraction'));
}
$this->archive->close();
return $pack;
}
}