1 | <?php |
||
36 | class PackRepository |
||
37 | { |
||
38 | /** |
||
39 | * Creates a new pack on the system. |
||
40 | * |
||
41 | * @param array $data |
||
42 | * @return \Pterodactyl\Models\Pack |
||
43 | * |
||
44 | * @throws \Pterodactyl\Exceptions\DisplayException |
||
45 | * @throws \Pterodactyl\Exceptions\DisplayValidationException |
||
46 | */ |
||
47 | public function create(array $data) |
||
48 | { |
||
49 | $validator = Validator::make($data, [ |
||
50 | 'name' => 'required|string', |
||
51 | 'version' => 'required|string', |
||
52 | 'description' => 'sometimes|nullable|string', |
||
53 | 'selectable' => 'sometimes|required|boolean', |
||
54 | 'visible' => 'sometimes|required|boolean', |
||
55 | 'locked' => 'sometimes|required|boolean', |
||
56 | 'option_id' => 'required|exists:service_options,id', |
||
57 | ]); |
||
58 | |||
59 | if ($validator->fails()) { |
||
60 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
61 | } |
||
62 | |||
63 | if (isset($data['file_upload'])) { |
||
64 | if (! $data['file_upload']->isValid()) { |
||
65 | throw new DisplayException('The file provided does not appear to be valid.'); |
||
66 | } |
||
67 | |||
68 | if (! in_array($data['file_upload']->getMimeType(), ['application/gzip', 'application/x-gzip'])) { |
||
69 | throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetype of application/gzip.'); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | return DB::transaction(function () use ($data) { |
||
74 | $uuid = new UuidService(); |
||
75 | |||
76 | $pack = new Pack; |
||
77 | $pack->uuid = $uuid->generate('packs', 'uuid'); |
||
78 | $pack->fill([ |
||
79 | 'option_id' => $data['option_id'], |
||
80 | 'name' => $data['name'], |
||
81 | 'version' => $data['version'], |
||
82 | 'description' => (empty($data['description'])) ? null : $data['description'], |
||
83 | 'selectable' => isset($data['selectable']), |
||
84 | 'visible' => isset($data['visible']), |
||
85 | 'locked' => isset($data['locked']), |
||
86 | ])->save(); |
||
87 | |||
88 | if (! $pack->exists) { |
||
89 | throw new DisplayException('Model does not exist after creation. Did an event prevent it from saving?'); |
||
90 | } |
||
91 | |||
92 | Storage::makeDirectory('packs/' . $pack->uuid); |
||
|
|||
93 | if (isset($data['file_upload'])) { |
||
94 | $data['file_upload']->storeAs('packs/' . $pack->uuid, 'archive.tar.gz'); |
||
95 | } |
||
96 | |||
97 | return $pack; |
||
98 | }); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Creates a new pack on the system given a template file. |
||
103 | * |
||
104 | * @param array $data |
||
105 | * @return \Pterodactyl\Models\Pack |
||
106 | * |
||
107 | * @throws \Pterodactyl\Exceptions\DisplayException |
||
108 | */ |
||
109 | public function createWithTemplate(array $data) |
||
110 | { |
||
111 | if (! isset($data['file_upload'])) { |
||
112 | throw new DisplayException('No template file was found submitted with this request.'); |
||
113 | } |
||
114 | |||
115 | if (! $data['file_upload']->isValid()) { |
||
116 | throw new DisplayException('The file provided does not appear to be valid.'); |
||
117 | } |
||
118 | |||
119 | if (! in_array($data['file_upload']->getMimeType(), [ |
||
120 | 'application/zip', |
||
121 | 'text/plain', |
||
122 | 'application/json', |
||
123 | ])) { |
||
124 | throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.'); |
||
125 | } |
||
126 | |||
127 | if ($data['file_upload']->getMimeType() === 'application/zip') { |
||
128 | $zip = new \ZipArchive; |
||
129 | if (! $zip->open($data['file_upload']->path())) { |
||
130 | throw new DisplayException('The uploaded archive was unable to be opened.'); |
||
131 | } |
||
132 | |||
133 | $isTar = $zip->locateName('archive.tar.gz'); |
||
134 | |||
135 | if (! $zip->locateName('import.json') || ! $isTar) { |
||
136 | throw new DisplayException('This contents of the provided archive were in an invalid format.'); |
||
137 | } |
||
138 | |||
139 | $json = json_decode($zip->getFromName('import.json')); |
||
140 | $pack = $this->create([ |
||
141 | 'name' => $json->name, |
||
142 | 'version' => $json->version, |
||
143 | 'description' => $json->description, |
||
144 | 'option_id' => $data['option_id'], |
||
145 | 'selectable' => $json->selectable, |
||
146 | 'visible' => $json->visible, |
||
147 | 'locked' => $json->locked, |
||
148 | ]); |
||
149 | |||
150 | if (! $zip->extractTo(storage_path('app/packs/' . $pack->uuid), 'archive.tar.gz')) { |
||
151 | $pack->delete(); |
||
152 | throw new DisplayException('Unable to extract the archive file to the correct location.'); |
||
153 | } |
||
154 | |||
155 | $zip->close(); |
||
156 | |||
157 | return $pack; |
||
158 | } else { |
||
159 | $json = json_decode(file_get_contents($data['file_upload']->path())); |
||
160 | |||
161 | return $this->create([ |
||
162 | 'name' => $json->name, |
||
163 | 'version' => $json->version, |
||
164 | 'description' => $json->description, |
||
165 | 'option_id' => $data['option_id'], |
||
166 | 'selectable' => $json->selectable, |
||
167 | 'visible' => $json->visible, |
||
168 | 'locked' => $json->locked, |
||
169 | ]); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Updates a pack on the system. |
||
175 | * |
||
176 | * @param int $id |
||
177 | * @param array $data |
||
178 | * @return \Pterodactyl\Models\Pack |
||
179 | * |
||
180 | * @throws \Pterodactyl\Exceptions\DisplayException |
||
181 | * @throws \Pterodactyl\Exceptions\DisplayValidationException |
||
182 | */ |
||
183 | public function update($id, array $data) |
||
217 | |||
218 | /** |
||
219 | * Deletes a pack and files from the system. |
||
220 | * |
||
221 | * @param int $id |
||
222 | * @return void |
||
223 | * |
||
224 | * @throws \Pterodactyl\Exceptions\DisplayException |
||
225 | */ |
||
226 | public function delete($id) |
||
239 | } |
||
240 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.