Skip to content

Commit 6269a08

Browse files
committed
Finalize service option import/export
1 parent d608c31 commit 6269a08

File tree

16 files changed

+405
-271
lines changed

16 files changed

+405
-271
lines changed

app/Contracts/Repository/ServiceOptionRepositoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function getWithVariables(int $id): ServiceOption;
3333
*/
3434
public function getWithCopyAttributes(int $id): ServiceOption;
3535

36+
/**
37+
* Return all of the data needed to export a service.
38+
*
39+
* @param int $id
40+
* @return \Pterodactyl\Models\ServiceOption
41+
*
42+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
43+
*/
44+
public function getWithExportAttributes(int $id): ServiceOption;
45+
3646
/**
3747
* Confirm a copy script belongs to the same service as the item trying to use it.
3848
*

app/Contracts/Repository/ServiceRepositoryInterface.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,36 @@
1010
namespace Pterodactyl\Contracts\Repository;
1111

1212
use Pterodactyl\Models\Service;
13-
use Illuminate\Support\Collection;
1413

1514
interface ServiceRepositoryInterface extends RepositoryInterface
1615
{
1716
/**
1817
* Return a service or all services with their associated options, variables, and packs.
1918
*
2019
* @param int $id
21-
* @return \Illuminate\Support\Collection
20+
* @return \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Service
21+
*
22+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2223
*/
23-
public function getWithOptions(int $id = null): Collection;
24+
public function getWithOptions(int $id = null);
2425

2526
/**
2627
* Return a service or all services and the count of options, packs, and servers for that service.
2728
*
2829
* @param int|null $id
29-
* @return \Illuminate\Support\Collection
30+
* @return \Pterodactyl\Models\Service|\Illuminate\Database\Eloquent\Collection
31+
*
32+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
3033
*/
31-
public function getWithCounts(int $id = null): Collection;
34+
public function getWithCounts(int $id = null);
3235

3336
/**
3437
* Return a service along with its associated options and the servers relation on those options.
3538
*
3639
* @param int $id
37-
* @return mixed
40+
* @return \Pterodactyl\Models\Service
41+
*
42+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
3843
*/
3944
public function getWithOptionServers(int $id): Service;
4045
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Exceptions\Service\ServiceOption;
11+
12+
use Pterodactyl\Exceptions\DisplayException;
13+
14+
class DuplicateOptionTagException extends DisplayException
15+
{
16+
}

app/Http/Controllers/Admin/Services/Options/OptionShareController.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,38 @@
99

1010
namespace Pterodactyl\Http\Controllers\Admin\Services\Options;
1111

12+
use Illuminate\Http\RedirectResponse;
1213
use Pterodactyl\Models\ServiceOption;
1314
use Pterodactyl\Http\Controllers\Controller;
1415
use Symfony\Component\HttpFoundation\Response;
15-
use Pterodactyl\Services\Services\Exporter\XMLExporterService;
16+
use Pterodactyl\Http\Requests\Admin\Service\OptionImportFormRequest;
17+
use Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService;
18+
use Pterodactyl\Services\Services\Sharing\ServiceOptionImporterService;
1619

1720
class OptionShareController extends Controller
1821
{
1922
/**
20-
* @var \Pterodactyl\Services\Services\Exporter\XMLExporterService
23+
* @var \Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService
2124
*/
2225
protected $exporterService;
2326

27+
/**
28+
* @var \Pterodactyl\Services\Services\Sharing\ServiceOptionImporterService
29+
*/
30+
protected $importerService;
31+
2432
/**
2533
* OptionShareController constructor.
2634
*
27-
* @param \Pterodactyl\Services\Services\Exporter\XMLExporterService $exporterService
35+
* @param \Pterodactyl\Services\Services\Sharing\ServiceOptionExporterService $exporterService
36+
* @param \Pterodactyl\Services\Services\Sharing\ServiceOptionImporterService $importerService
2837
*/
29-
public function __construct(XMLExporterService $exporterService)
30-
{
38+
public function __construct(
39+
ServiceOptionExporterService $exporterService,
40+
ServiceOptionImporterService $importerService
41+
) {
3142
$this->exporterService = $exporterService;
43+
$this->importerService = $importerService;
3244
}
3345

3446
/**
@@ -42,8 +54,25 @@ public function export(ServiceOption $option): Response
4254
return response($this->exporterService->handle($option->id), 200, [
4355
'Content-Transfer-Encoding' => 'binary',
4456
'Content-Description' => 'File Transfer',
45-
'Content-Disposition' => 'attachment; filename=' . kebab_case($option->name) . '.xml',
46-
'Content-Type' => 'application/xml',
57+
'Content-Disposition' => 'attachment; filename=' . kebab_case($option->name) . '.json',
58+
'Content-Type' => 'application/json',
4759
]);
4860
}
61+
62+
/**
63+
* Import a new service option using an XML file.
64+
*
65+
* @param \Pterodactyl\Http\Requests\Admin\Service\OptionImportFormRequest $request
66+
* @return \Illuminate\Http\RedirectResponse
67+
*
68+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
69+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
70+
* @throws \Pterodactyl\Exceptions\Service\Pack\InvalidFileUploadException
71+
*/
72+
public function import(OptionImportFormRequest $request): RedirectResponse
73+
{
74+
$option = $this->importerService->handle($request->file('import_file'), $request->input('import_to_service'));
75+
76+
return redirect()->route('admin.services.option.view', ['option' => $option->id]);
77+
}
4978
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Http\Requests\Admin\Service;
11+
12+
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
13+
14+
class OptionImportFormRequest extends AdminFormRequest
15+
{
16+
/**
17+
* @return array
18+
*/
19+
public function rules(): array
20+
{
21+
return [
22+
'import_file' => 'bail|required|file|max:1000|mimetypes:application/json,text/plain',
23+
'import_to_service' => 'bail|required|integer|exists:services,id',
24+
];
25+
}
26+
}

app/Models/ServiceOption.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class ServiceOption extends Model implements CleansAttributes, ValidableContract
6565
*/
6666
protected static $applicationRules = [
6767
'service_id' => 'required',
68-
'author' => 'required',
6968
'name' => 'required',
7069
'description' => 'required',
7170
'tag' => 'required',
@@ -83,10 +82,10 @@ class ServiceOption extends Model implements CleansAttributes, ValidableContract
8382
*/
8483
protected static $dataIntegrityRules = [
8584
'service_id' => 'bail|numeric|exists:services,id',
86-
'author' => 'email',
85+
'uuid' => 'string|size:36',
8786
'name' => 'string|max:255',
8887
'description' => 'string',
89-
'tag' => 'bail|alpha_num|max:60|unique:service_options,tag',
88+
'tag' => 'bail|string|max:150',
9089
'docker_image' => 'string|max:255',
9190
'startup' => 'nullable|string',
9291
'config_from' => 'bail|nullable|numeric|exists:service_options,id',

app/Repositories/Eloquent/ServiceOptionRepository.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ public function getWithCopyAttributes(int $id): ServiceOption
6161
return $instance;
6262
}
6363

64+
/**
65+
* Return all of the data needed to export a service.
66+
*
67+
* @param int $id
68+
* @return \Pterodactyl\Models\ServiceOption
69+
*
70+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
71+
*/
72+
public function getWithExportAttributes(int $id): ServiceOption
73+
{
74+
/** @var \Pterodactyl\Models\ServiceOption $instance */
75+
$instance = $this->getBuilder()->with('scriptFrom', 'configFrom', 'variables')->find($id, $this->getColumns());
76+
if (! $instance) {
77+
throw new RecordNotFoundException;
78+
}
79+
80+
return $instance;
81+
}
82+
6483
/**
6584
* Confirm a copy script belongs to the same service as the item trying to use it.
6685
*

app/Repositories/Eloquent/ServiceRepository.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Pterodactyl\Repositories\Eloquent;
1111

1212
use Pterodactyl\Models\Service;
13-
use Illuminate\Support\Collection;
1413
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
1514
use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface;
1615

@@ -25,9 +24,14 @@ public function model()
2524
}
2625

2726
/**
28-
* {@inheritdoc}
27+
* Return a service or all services with their associated options, variables, and packs.
28+
*
29+
* @param int $id
30+
* @return \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Service
31+
*
32+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2933
*/
30-
public function getWithOptions(int $id = null): Collection
34+
public function getWithOptions(int $id = null)
3135
{
3236
$instance = $this->getBuilder()->with('options.packs', 'options.variables');
3337

@@ -44,9 +48,14 @@ public function getWithOptions(int $id = null): Collection
4448
}
4549

4650
/**
47-
* {@inheritdoc}
51+
* Return a service or all services and the count of options, packs, and servers for that service.
52+
*
53+
* @param int|null $id
54+
* @return \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Service
55+
*
56+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
4857
*/
49-
public function getWithCounts(int $id = null): Collection
58+
public function getWithCounts(int $id = null)
5059
{
5160
$instance = $this->getBuilder()->withCount(['options', 'packs', 'servers']);
5261

@@ -63,7 +72,12 @@ public function getWithCounts(int $id = null): Collection
6372
}
6473

6574
/**
66-
* {@inheritdoc}
75+
* Return a service along with its associated options and the servers relation on those options.
76+
*
77+
* @param int $id
78+
* @return \Pterodactyl\Models\Service
79+
*
80+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6781
*/
6882
public function getWithOptionServers(int $id): Service
6983
{

0 commit comments

Comments
 (0)