From 6746f0f78b09965cd32e2bc421065b46066db326 Mon Sep 17 00:00:00 2001 From: Job Rapati Date: Fri, 22 Sep 2023 11:07:51 +0200 Subject: [PATCH 1/5] #2 #5 #8 first iteration modpack overview page --- .../Servers/CurseForge/ModpackController.php | 51 ++++++ .../scripts/api/server/modpacks/Modpack.ts | 109 +++++++++++++ .../api/server/modpacks/getModpacks.ts | 40 +++++ .../server/modpacks/ModpackContainer.tsx | 54 +++++++ .../server/modpacks/ModpackItem.tsx | 21 +++ routes/api-client.php | 148 ++++++++++++++++++ 6 files changed, 423 insertions(+) create mode 100644 app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php create mode 100644 resources/scripts/api/server/modpacks/Modpack.ts create mode 100644 resources/scripts/api/server/modpacks/getModpacks.ts create mode 100644 resources/scripts/components/server/modpacks/ModpackContainer.tsx create mode 100644 resources/scripts/components/server/modpacks/ModpackItem.tsx create mode 100644 routes/api-client.php diff --git a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php new file mode 100644 index 0000000..7990c6b --- /dev/null +++ b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php @@ -0,0 +1,51 @@ +http_client = new Client([ + 'base_uri' => 'https://api.curseforge.com/v1/', + 'headers' => [ + 'x-api-key' => config('curseforge.api_key'), + ] + ]); + + $this->minecraft_game_id = config('curseforge.minecraft_game_id'); + $this->modpack_class_id = config('curseforge.minecraft_modpack_class_id'); + } + + public function index() { + $result = $this->http_client->get("mods/search?gameid=$this->minecraft_game_id&classid=$this->modpack_class_id&sortField=2"); + + if($result->getStatusCode() !== 200) { + throw new DisplayException('Failed to fetch modpacks from CurseForge.'); + } + + return $result->getBody()->getContents(); + } + + public function show() { + throw new NotImplementedException(); + } + + public function install() { + throw new NotImplementedException(); + } +} diff --git a/resources/scripts/api/server/modpacks/Modpack.ts b/resources/scripts/api/server/modpacks/Modpack.ts new file mode 100644 index 0000000..24b2f31 --- /dev/null +++ b/resources/scripts/api/server/modpacks/Modpack.ts @@ -0,0 +1,109 @@ + +export interface Modpack { + id: number; + gameId: number; + name: string; + slug: string; + links: Link; + summary: string; + status: number; + downloadCount: number; + isFeatured: boolean; + primaryCategoryId: number; + categories: Category[]; + classId: number; + authors: Author[]; + logo: Image; + screenshots: Image[]; + mainFileId: number; + latestFiles: File[]; + latestFileIndexes: FileIndex[]; + latestEarlyAccessFileIndexes: FileIndex[]; + dateCreated: string; + dateModified: string; + dateReleased: string; + allowModDistribution: boolean; + gamePopularityRank: number; + isAvailable: boolean; + thumbsUpCount: number; +} + +export interface Link { + websiteUrl: string; + wikiUrl: string|null; + issuesUrl: string|null; + sourceUrl: string|null; +} + +export interface Category { + id: number; + gameId: number; + name: string; + slug: string; + url: string; + iconUrl: string; + dateModified: string; + isClass: boolean; + classId: number; + parentCategoryId: number; +} + +export interface Author { + id: number; + name: string; + url: string; +} + +export interface Image { + id: number; + modId: number; + title: string; + description: string; + thumbnailUrl: string; + url: string; +} + +export interface File { + id: number; + gameId: number; + modId: number; + isAvailable: boolean; + displayName: string; + fileName: string; + releaseType: number; + fileStatus: number; + hashes: Hash[]; + fileDate: string; + fileLength: number; + downloadCount: number; + downloadUrl: string; + gameVersion: string[]; + sortableGameVersion: GameVersion[]; + dependencies: any[]; + alternateFileId: number; + isServerPack: boolean; + fileFingerprint: number; + modules: any[]; +} + +export interface Hash { + value: string; + algorithm: number; +} + +export interface GameVersion { + gameVersionName: string; + gameVersionPadded: string; + gameVersion: string; + gameVersionReleaseDate: string; + gameVersionTypeID: number; +} + +export interface FileIndex { + gameVersion: string; + fileId: number; + filename: string; + releaseType: number; + gameVersionTypeId: number; + modLoader: number; +} diff --git a/resources/scripts/api/server/modpacks/getModpacks.ts b/resources/scripts/api/server/modpacks/getModpacks.ts new file mode 100644 index 0000000..59e9fda --- /dev/null +++ b/resources/scripts/api/server/modpacks/getModpacks.ts @@ -0,0 +1,40 @@ +import http from '@/api/http' +import { Modpack } from './Modpack'; + +export default (uuid: string): Promise => { + return new Promise((resolve, reject) => { + http.get(`/api/client/servers/${uuid}/modpacks`) + .then((response) => + resolve((response.data.data || []).map((item: any) => rawDataToModpackData(item)))) + .catch(reject); + }); +} +export const rawDataToModpackData = (data: any): Modpack => ({ + id: data.id, + gameId: data.gameId, + name: data.name, + slug: data.slug, + links: data.links, + summary: data.summary, + status: data.status, + downloadCount: data.downloadCount, + isFeatured: data.isFeatured, + primaryCategoryId: data.primaryCategoryId, + categories: data.categories, + classId: data.classId, + authors: data.authors, + logo: data.logo, + screenshots: data.screenshots, + mainFileId: data.mainFileId, + latestFiles: data.latestFiles, + latestFileIndexes: data.latestFileIndexes, + latestEarlyAccessFileIndexes: data.latestEarlyAccessFileIndexes, + dateCreated: data.dateCreated, + dateModified: data.dateModified, + dateReleased: data.dateReleased, + allowModDistribution: data.allowModDistribution, + gamePopularityRank: data.gamePopularityRank, + isAvailable: data.isAvailable, + thumbsUpCount: data.thumbsUpCount, +}); + diff --git a/resources/scripts/components/server/modpacks/ModpackContainer.tsx b/resources/scripts/components/server/modpacks/ModpackContainer.tsx new file mode 100644 index 0000000..17f14ad --- /dev/null +++ b/resources/scripts/components/server/modpacks/ModpackContainer.tsx @@ -0,0 +1,54 @@ +import React, { useEffect, useState } from 'react'; +import ServerContentBlock from "@/components/elements/ServerContentBlock"; +import GreyRowBox from '@/components/elements/GreyRowBox'; +import ModpackItem from './ModpackItem'; +import { ServerContext } from '@/state/server'; +import { useDeepMemoize } from '@/plugins/useDeepMemoize'; +import { use } from 'i18next'; +import getModpacks from '@/api/server/modpacks/getModpacks'; +import FlashMessageRender from '@/components/FlashMessageRender'; +import tw from 'twin.macro'; +import databases from '@/state/server/databases'; +import Spinner from '@/components/elements/Spinner'; +import Fade from '@/components/elements/Fade'; +import { Modpack } from '@/api/server/modpacks/Modpack'; + +export default() => { + const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid); + const [loading, setLoading] = useState(true); + + const [modpacks, setModpacks] = useState([]); + + useEffect(() => { + setLoading(!modpacks.length); + + getModpacks(uuid) + .then((modpacks) => setModpacks(modpacks)) + }, []); + + return ( + + + {!modpacks.length && loading ? ( + + ) : ( + +
+ {modpacks.length > 0 ? ( + modpacks.map((modpack, index) => ( + + )) + ) : ( +

+ Couldn't fetch modpacks. +

+ )} +
+
+ )} +
+ ); +} diff --git a/resources/scripts/components/server/modpacks/ModpackItem.tsx b/resources/scripts/components/server/modpacks/ModpackItem.tsx new file mode 100644 index 0000000..975a296 --- /dev/null +++ b/resources/scripts/components/server/modpacks/ModpackItem.tsx @@ -0,0 +1,21 @@ +import { Modpack } from '@/api/server/modpacks/Modpack'; +import GreyRowBox from '@/components/elements/GreyRowBox'; +import TitledGreyBox from '@/components/elements/TitledGreyBox'; +import React from 'react'; + +interface Props { + modpack: Modpack; +} + +export default({modpack}: Props) => { + return ( +
+ + + +

{modpack.summary}

+
+
+
+ ); +} diff --git a/routes/api-client.php b/routes/api-client.php new file mode 100644 index 0000000..e0bdee1 --- /dev/null +++ b/routes/api-client.php @@ -0,0 +1,148 @@ +name('api:client.index'); +Route::get('/permissions', [Client\ClientController::class, 'permissions']); + +Route::prefix('/account')->middleware(AccountSubject::class)->group(function () { + Route::prefix('/')->withoutMiddleware(RequireTwoFactorAuthentication::class)->group(function () { + Route::get('/', [Client\AccountController::class, 'index'])->name('api:client.account'); + Route::get('/two-factor', [Client\TwoFactorController::class, 'index']); + Route::post('/two-factor', [Client\TwoFactorController::class, 'store']); + Route::delete('/two-factor', [Client\TwoFactorController::class, 'delete']); + }); + + Route::put('/email', [Client\AccountController::class, 'updateEmail'])->name('api:client.account.update-email'); + Route::put('/password', [Client\AccountController::class, 'updatePassword'])->name('api:client.account.update-password'); + + Route::get('/activity', Client\ActivityLogController::class)->name('api:client.account.activity'); + + Route::get('/api-keys', [Client\ApiKeyController::class, 'index']); + Route::post('/api-keys', [Client\ApiKeyController::class, 'store']); + Route::delete('/api-keys/{identifier}', [Client\ApiKeyController::class, 'delete']); + + Route::prefix('/ssh-keys')->group(function () { + Route::get('/', [Client\SSHKeyController::class, 'index']); + Route::post('/', [Client\SSHKeyController::class, 'store']); + Route::post('/remove', [Client\SSHKeyController::class, 'delete']); + }); +}); + +/* +|-------------------------------------------------------------------------- +| Client Control API +|-------------------------------------------------------------------------- +| +| Endpoint: /api/client/servers/{server} +| +*/ +Route::group([ + 'prefix' => '/servers/{server}', + 'middleware' => [ + ServerSubject::class, + AuthenticateServerAccess::class, + ResourceBelongsToServer::class, + ], +], function () { + Route::get('/', [Client\Servers\ServerController::class, 'index'])->name('api:client:server.view'); + Route::get('/websocket', Client\Servers\WebsocketController::class)->name('api:client:server.ws'); + Route::get('/resources', Client\Servers\ResourceUtilizationController::class)->name('api:client:server.resources'); + Route::get('/activity', Client\Servers\ActivityLogController::class)->name('api:client:server.activity'); + + Route::post('/command', [Client\Servers\CommandController::class, 'index']); + Route::post('/power', [Client\Servers\PowerController::class, 'index']); + + Route::group(['prefix' => '/modpacks'], function () { + Route::get('/', [Client\Servers\CurseForge\ModpackController::class, 'index']); + Route::get('/{modpack}', [Client\Servers\CurseForge\ModpackController::class, 'show']); + Route::get('/{modpack}/install', [Client\Servers\CurseForge\CurseForgeModpackController::class, 'install']); + }); + + Route::group(['prefix' => '/databases'], function () { + Route::get('/', [Client\Servers\DatabaseController::class, 'index']); + Route::post('/', [Client\Servers\DatabaseController::class, 'store']); + Route::post('/{database}/rotate-password', [Client\Servers\DatabaseController::class, 'rotatePassword']); + Route::delete('/{database}', [Client\Servers\DatabaseController::class, 'delete']); + }); + + Route::group(['prefix' => '/files'], function () { + Route::get('/list', [Client\Servers\FileController::class, 'directory']); + Route::get('/contents', [Client\Servers\FileController::class, 'contents']); + Route::get('/download', [Client\Servers\FileController::class, 'download']); + Route::put('/rename', [Client\Servers\FileController::class, 'rename']); + Route::post('/copy', [Client\Servers\FileController::class, 'copy']); + Route::post('/write', [Client\Servers\FileController::class, 'write']); + Route::post('/compress', [Client\Servers\FileController::class, 'compress']); + Route::post('/decompress', [Client\Servers\FileController::class, 'decompress']); + Route::post('/delete', [Client\Servers\FileController::class, 'delete']); + Route::post('/create-folder', [Client\Servers\FileController::class, 'create']); + Route::post('/chmod', [Client\Servers\FileController::class, 'chmod']); + Route::post('/pull', [Client\Servers\FileController::class, 'pull'])->middleware(['throttle:10,5']); + Route::get('/upload', Client\Servers\FileUploadController::class); + }); + + Route::group(['prefix' => '/schedules'], function () { + Route::get('/', [Client\Servers\ScheduleController::class, 'index']); + Route::post('/', [Client\Servers\ScheduleController::class, 'store']); + Route::get('/{schedule}', [Client\Servers\ScheduleController::class, 'view']); + Route::post('/{schedule}', [Client\Servers\ScheduleController::class, 'update']); + Route::post('/{schedule}/execute', [Client\Servers\ScheduleController::class, 'execute']); + Route::delete('/{schedule}', [Client\Servers\ScheduleController::class, 'delete']); + + Route::post('/{schedule}/tasks', [Client\Servers\ScheduleTaskController::class, 'store']); + Route::post('/{schedule}/tasks/{task}', [Client\Servers\ScheduleTaskController::class, 'update']); + Route::delete('/{schedule}/tasks/{task}', [Client\Servers\ScheduleTaskController::class, 'delete']); + }); + + Route::group(['prefix' => '/network'], function () { + Route::get('/allocations', [Client\Servers\NetworkAllocationController::class, 'index']); + Route::post('/allocations', [Client\Servers\NetworkAllocationController::class, 'store']); + Route::post('/allocations/{allocation}', [Client\Servers\NetworkAllocationController::class, 'update']); + Route::post('/allocations/{allocation}/primary', [Client\Servers\NetworkAllocationController::class, 'setPrimary']); + Route::delete('/allocations/{allocation}', [Client\Servers\NetworkAllocationController::class, 'delete']); + }); + + Route::group(['prefix' => '/users'], function () { + Route::get('/', [Client\Servers\SubuserController::class, 'index']); + Route::post('/', [Client\Servers\SubuserController::class, 'store']); + Route::get('/{user}', [Client\Servers\SubuserController::class, 'view']); + Route::post('/{user}', [Client\Servers\SubuserController::class, 'update']); + Route::delete('/{user}', [Client\Servers\SubuserController::class, 'delete']); + }); + + Route::group(['prefix' => '/backups'], function () { + Route::get('/', [Client\Servers\BackupController::class, 'index']); + Route::post('/', [Client\Servers\BackupController::class, 'store']); + Route::get('/{backup}', [Client\Servers\BackupController::class, 'view']); + Route::get('/{backup}/download', [Client\Servers\BackupController::class, 'download']); + Route::post('/{backup}/lock', [Client\Servers\BackupController::class, 'toggleLock']); + Route::post('/{backup}/restore', [Client\Servers\BackupController::class, 'restore']); + Route::delete('/{backup}', [Client\Servers\BackupController::class, 'delete']); + }); + + Route::group(['prefix' => '/startup'], function () { + Route::get('/', [Client\Servers\StartupController::class, 'index']); + Route::put('/variable', [Client\Servers\StartupController::class, 'update']); + }); + + Route::group(['prefix' => '/settings'], function () { + Route::post('/rename', [Client\Servers\SettingsController::class, 'rename']); + Route::post('/reinstall', [Client\Servers\SettingsController::class, 'reinstall']); + Route::put('/docker-image', [Client\Servers\SettingsController::class, 'dockerImage']); + }); +}); From e1ec05a30d18c838cede3577bdcf183c9680b5d2 Mon Sep 17 00:00:00 2001 From: Job Rapati Date: Fri, 22 Sep 2023 14:34:04 +0200 Subject: [PATCH 2/5] #5 - add pagination --- .../Servers/CurseForge/ModpackController.php | 10 ++- .../api/server/modpacks/getModpacks.ts | 22 +++-- .../components/elements/Pagination.tsx | 80 +++++++++++++++++++ .../server/modpacks/ModpackContainer.tsx | 57 +++++++++---- 4 files changed, 146 insertions(+), 23 deletions(-) create mode 100644 resources/scripts/components/elements/Pagination.tsx diff --git a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php index 7990c6b..064b07e 100644 --- a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php +++ b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php @@ -4,6 +4,7 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers\CurseForge; use GuzzleHttp\Client; +use Illuminate\Http\Request; use Nette\NotImplementedException; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; @@ -31,8 +32,13 @@ class ModpackController extends ClientApiController { $this->modpack_class_id = config('curseforge.minecraft_modpack_class_id'); } - public function index() { - $result = $this->http_client->get("mods/search?gameid=$this->minecraft_game_id&classid=$this->modpack_class_id&sortField=2"); + public function index(Request $request) { + $index = 0; + if($request->has('pageindex')) { + $index = $request->get('pageindex'); + } + + $result = $this->http_client->get("mods/search?gameid=$this->minecraft_game_id&classid=$this->modpack_class_id&sortField=2&sortorder=desc&index=$index"); if($result->getStatusCode() !== 200) { throw new DisplayException('Failed to fetch modpacks from CurseForge.'); diff --git a/resources/scripts/api/server/modpacks/getModpacks.ts b/resources/scripts/api/server/modpacks/getModpacks.ts index 59e9fda..dba853f 100644 --- a/resources/scripts/api/server/modpacks/getModpacks.ts +++ b/resources/scripts/api/server/modpacks/getModpacks.ts @@ -1,14 +1,26 @@ -import http from '@/api/http' +import http, { PaginationDataSet } from '@/api/http' import { Modpack } from './Modpack'; -export default (uuid: string): Promise => { +export default (uuid: string, pageIndex: number = 0): Promise => { return new Promise((resolve, reject) => { - http.get(`/api/client/servers/${uuid}/modpacks`) - .then((response) => - resolve((response.data.data || []).map((item: any) => rawDataToModpackData(item)))) + http.get(`/api/client/servers/${uuid}/modpacks?pageindex=${pageIndex}`) + .then((response) => { + + console.log(rawDataToModpackPaginationData(response.data.pagination), response.data.pagination); + resolve([(response.data.data || []).map((item: any) => rawDataToModpackData(item)), rawDataToModpackPaginationData(response.data.pagination)]) + }) .catch(reject); }); } + +export const rawDataToModpackPaginationData = (data: any): PaginationDataSet => ({ + total: data.totalCount, + count: data.resultCount, + perPage: data.pageSize, + currentPage: Math.ceil((data.index + data.pageSize) / data.pageSize) == 0 ? 1 : Math.ceil((data.index + data.pageSize) / data.pageSize), + totalPages: Math.ceil(data.totalCount / data.pageSize) + 1 +}); + export const rawDataToModpackData = (data: any): Modpack => ({ id: data.id, gameId: data.gameId, diff --git a/resources/scripts/components/elements/Pagination.tsx b/resources/scripts/components/elements/Pagination.tsx new file mode 100644 index 0000000..ca7de9a --- /dev/null +++ b/resources/scripts/components/elements/Pagination.tsx @@ -0,0 +1,80 @@ +import React from 'react'; +import { PaginatedResult } from '@/api/http'; +import tw from 'twin.macro'; +import styled from 'styled-components/macro'; +import Button from '@/components/elements/Button'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faAngleDoubleLeft, faAngleDoubleRight } from '@fortawesome/free-solid-svg-icons'; + +interface RenderFuncProps { + items: T[]; + isLastPage: boolean; + isFirstPage: boolean; +} + +interface Props { + data: PaginatedResult; + showGoToLast?: boolean; + showGoToFirst?: boolean; + onPageSelect: (page: number) => void; + children: (props: RenderFuncProps) => React.ReactNode; + paginationButtonsClassNames?: string; +} + +const Block = styled(Button)` + ${tw`p-0 w-10 h-10`} + + &:not(:last-of-type) { + ${tw`mr-2`}; + } +`; + +function Pagination({ data: { items, pagination }, onPageSelect, children, paginationButtonsClassNames}: Props) { + const isFirstPage = pagination.currentPage === 1; + const isLastPage = pagination.currentPage >= pagination.totalPages; + + const pages = []; + + // Start two spaces before the current page. If that puts us before the starting page default + // to the first page as the starting point. + const start = Math.max(pagination.currentPage - 2, 1); + const end = Math.min(pagination.totalPages, pagination.currentPage + 5); + + for (let i = start; i <= end; i++) { + pages.push(i); + } + + return ( + <> + {children({ items, isFirstPage, isLastPage })} +
+ {pages.length > 1 && ( +
+ {pages[0] > 1 && !isFirstPage && ( + onPageSelect(1)}> + + + )} + {pages.map((i) => ( + onPageSelect(i)} + > + {i} + + ))} + {pages[4] < pagination.totalPages && !isLastPage && ( + onPageSelect(pagination.totalPages)}> + + + )} +
+ )} +
+ + ); +} + +export default Pagination; diff --git a/resources/scripts/components/server/modpacks/ModpackContainer.tsx b/resources/scripts/components/server/modpacks/ModpackContainer.tsx index 17f14ad..2e1f5a2 100644 --- a/resources/scripts/components/server/modpacks/ModpackContainer.tsx +++ b/resources/scripts/components/server/modpacks/ModpackContainer.tsx @@ -12,40 +12,65 @@ import databases from '@/state/server/databases'; import Spinner from '@/components/elements/Spinner'; import Fade from '@/components/elements/Fade'; import { Modpack } from '@/api/server/modpacks/Modpack'; +import Pagination from '@/components/elements/Pagination'; +import { PaginatedResult } from '@/api/http'; export default() => { const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid); const [loading, setLoading] = useState(true); + const [page, setPage] = useState(1); - const [modpacks, setModpacks] = useState([]); + const [modpacks, setModpacks] = useState>(); + + const changePage = (newPage: number) => { + setPage(newPage); + setLoading(true); + + let pageIndex = 0; + if(modpacks?.pagination.perPage != undefined) { + pageIndex = (modpacks.pagination.perPage * newPage) - modpacks.pagination.perPage; + console.log(pageIndex, modpacks.pagination.perPage, newPage); + } + + getModpacks(uuid, pageIndex) + .then((modpacksResult) => { + setModpacks({items: modpacksResult[0], pagination: modpacksResult[1]}); + setLoading(false); + }) + } useEffect(() => { - setLoading(!modpacks.length); + setLoading(!modpacks?.items.length); getModpacks(uuid) - .then((modpacks) => setModpacks(modpacks)) + .then((modpacksResult) => { + setModpacks({items: modpacksResult[0], pagination: modpacksResult[1]}); + }); }, []); return ( - {!modpacks.length && loading ? ( + {(modpacks == undefined || !modpacks?.items.length) && loading ? ( ) : (
- {modpacks.length > 0 ? ( - modpacks.map((modpack, index) => ( - - )) - ) : ( -

- Couldn't fetch modpacks. -

- )} + } onPageSelect={changePage} paginationButtonsClassNames='col-span-3'> + {({ items }) => ( + items.length > 0 ? ( + modpacks?.items.map((modpack, index) => ( + + )) + ) : ( +

+ Couldn't fetch modpacks. +

+ ))} +
)} From 85d197a5264e58f06208bd428dc55b48f1eaa294 Mon Sep 17 00:00:00 2001 From: Job Rapati Date: Fri, 22 Sep 2023 16:12:32 +0200 Subject: [PATCH 3/5] #5 - add modloader type filter --- .../Servers/CurseForge/ModpackController.php | 8 +- .../api/server/modpacks/getModpacks.ts | 20 +++-- .../server/modpacks/ModpackContainer.tsx | 73 ++++++++++++++++--- 3 files changed, 83 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php index 064b07e..0091b4c 100644 --- a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php +++ b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php @@ -5,6 +5,7 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers\CurseForge; use GuzzleHttp\Client; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; use Nette\NotImplementedException; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; @@ -38,7 +39,12 @@ class ModpackController extends ClientApiController { $index = $request->get('pageindex'); } - $result = $this->http_client->get("mods/search?gameid=$this->minecraft_game_id&classid=$this->modpack_class_id&sortField=2&sortorder=desc&index=$index"); + $queryString = ''; + if($request->has('modloader') && $request->get('modloader') !== '0') { + $queryString .= '&modloadertype=' . $request->get('modloader'); + } + + $result = $this->http_client->get("mods/search?gameid=$this->minecraft_game_id&classid=$this->modpack_class_id&sortField=2&sortorder=desc&index=$index$queryString"); if($result->getStatusCode() !== 200) { throw new DisplayException('Failed to fetch modpacks from CurseForge.'); diff --git a/resources/scripts/api/server/modpacks/getModpacks.ts b/resources/scripts/api/server/modpacks/getModpacks.ts index dba853f..ca61a22 100644 --- a/resources/scripts/api/server/modpacks/getModpacks.ts +++ b/resources/scripts/api/server/modpacks/getModpacks.ts @@ -1,13 +1,21 @@ import http, { PaginationDataSet } from '@/api/http' import { Modpack } from './Modpack'; +import { ModpackSearchFilter } from '@/components/server/modpacks/ModpackContainer'; -export default (uuid: string, pageIndex: number = 0): Promise => { +export default (uuid: string, pageIndex: number = 0, filters: ModpackSearchFilter): Promise => { return new Promise((resolve, reject) => { - http.get(`/api/client/servers/${uuid}/modpacks?pageindex=${pageIndex}`) - .then((response) => { + let filterQuery = '&'; + if(filters.modloaderType) { + filterQuery += `modloader=${filters.modloaderType}`; + } else { + filterQuery += `modloader=0`; + } - console.log(rawDataToModpackPaginationData(response.data.pagination), response.data.pagination); - resolve([(response.data.data || []).map((item: any) => rawDataToModpackData(item)), rawDataToModpackPaginationData(response.data.pagination)]) + console.log(filterQuery); + + http.get(`/api/client/servers/${uuid}/modpacks?pageindex=${pageIndex}${filterQuery}`) + .then((response) => { + resolve([(response.data.data || []).map((item: any) => rawDataToModpackData(item)), rawDataToModpackPaginationData(response.data.pagination), {modloaderType: filters.modloaderType}]) }) .catch(reject); }); @@ -18,7 +26,7 @@ export const rawDataToModpackPaginationData = (data: any): PaginationDataSet => count: data.resultCount, perPage: data.pageSize, currentPage: Math.ceil((data.index + data.pageSize) / data.pageSize) == 0 ? 1 : Math.ceil((data.index + data.pageSize) / data.pageSize), - totalPages: Math.ceil(data.totalCount / data.pageSize) + 1 + totalPages: Math.ceil(data.totalCount / data.pageSize) }); export const rawDataToModpackData = (data: any): Modpack => ({ diff --git a/resources/scripts/components/server/modpacks/ModpackContainer.tsx b/resources/scripts/components/server/modpacks/ModpackContainer.tsx index 2e1f5a2..8fc83f6 100644 --- a/resources/scripts/components/server/modpacks/ModpackContainer.tsx +++ b/resources/scripts/components/server/modpacks/ModpackContainer.tsx @@ -1,24 +1,41 @@ -import React, { useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import ServerContentBlock from "@/components/elements/ServerContentBlock"; -import GreyRowBox from '@/components/elements/GreyRowBox'; import ModpackItem from './ModpackItem'; import { ServerContext } from '@/state/server'; -import { useDeepMemoize } from '@/plugins/useDeepMemoize'; -import { use } from 'i18next'; import getModpacks from '@/api/server/modpacks/getModpacks'; import FlashMessageRender from '@/components/FlashMessageRender'; import tw from 'twin.macro'; -import databases from '@/state/server/databases'; import Spinner from '@/components/elements/Spinner'; import Fade from '@/components/elements/Fade'; import { Modpack } from '@/api/server/modpacks/Modpack'; import Pagination from '@/components/elements/Pagination'; import { PaginatedResult } from '@/api/http'; +import GreyRowBox from '@/components/elements/GreyRowBox'; +import TitledGreyBox from '@/components/elements/TitledGreyBox'; +import { Dropdown } from '@/components/elements/dropdown'; +import Select from '@/components/elements/Select'; +import { set } from 'date-fns'; + +export enum ModloaderType { + Any, + Forge, + Cauldron, + LiteLoader, + Fabric, + Quilt +} + +export interface ModpackSearchFilter { + modloaderType: ModloaderType; +} export default() => { const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid); + const serverData = ServerContext.useStoreState((state) => state.server.data); const [loading, setLoading] = useState(true); const [page, setPage] = useState(1); + const [modloaderType, setModloaderType] = useState(ModloaderType.Any); + const [filters, setFilters] = useState({modloaderType: ModloaderType.Any}); const [modpacks, setModpacks] = useState>(); @@ -32,9 +49,10 @@ export default() => { console.log(pageIndex, modpacks.pagination.perPage, newPage); } - getModpacks(uuid, pageIndex) + getModpacks(uuid, pageIndex, filters) .then((modpacksResult) => { setModpacks({items: modpacksResult[0], pagination: modpacksResult[1]}); + setModloaderType(modpacksResult[2].modloaderType); setLoading(false); }) } @@ -42,11 +60,17 @@ export default() => { useEffect(() => { setLoading(!modpacks?.items.length); - getModpacks(uuid) - .then((modpacksResult) => { - setModpacks({items: modpacksResult[0], pagination: modpacksResult[1]}); - }); - }, []); + + changePage(1); + }, [modloaderType]); + + const updateFilterModloaderType = useCallback( + (e: React.ChangeEvent) => { + setFilters({modloaderType: parseInt(e.target.value)}); + setModloaderType(parseInt(e.target.value)); + }, + [modloaderType], + ); return ( @@ -56,6 +80,32 @@ export default() => { ) : (
+ + +
+ +
+
+
} onPageSelect={changePage} paginationButtonsClassNames='col-span-3'> {({ items }) => ( items.length > 0 ? ( @@ -77,3 +127,4 @@ export default() => { ); } + From 3bbf0b519949a9b192ba531bc06e0121764f3684 Mon Sep 17 00:00:00 2001 From: Job Rapati Date: Mon, 25 Sep 2023 16:40:44 +0200 Subject: [PATCH 4/5] #5 #8 #2 #9 #11 --- .../Servers/CurseForge/ModpackController.php | 11 + .../Client/Servers/FileController.php | 284 ++++++++++++++++++ .../scripts/api/server/files/getFileExists.ts | 11 + .../scripts/api/server/files/pullFile.ts | 23 ++ .../scripts/api/server/modpacks/Modpack.ts | 2 +- .../server/modpacks/getModpackDescription.ts | 14 + .../api/server/modpacks/getModpacks.ts | 2 - .../server/modpacks/ModpackItem.tsx | 126 +++++++- .../server/modpacks/ModpackModal.tsx | 16 + routes/api-client.php | 4 +- 10 files changed, 476 insertions(+), 17 deletions(-) create mode 100644 app/Http/Controllers/Client/Servers/FileController.php create mode 100644 resources/scripts/api/server/files/getFileExists.ts create mode 100644 resources/scripts/api/server/files/pullFile.ts create mode 100644 resources/scripts/api/server/modpacks/getModpackDescription.ts create mode 100644 resources/scripts/components/server/modpacks/ModpackModal.tsx diff --git a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php index 0091b4c..7ebdda7 100644 --- a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php +++ b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php @@ -53,6 +53,17 @@ class ModpackController extends ClientApiController { return $result->getBody()->getContents(); } + public function description(Request $request, $server, $modpack) { + + $result = $this->http_client->get("mods/$modpack/description"); + + if($result->getStatusCode() !== 200) { + throw new DisplayException('Failed to fetch modpack description from CurseForge.'); + } + + return $result->getBody()->getContents(); + } + public function show() { throw new NotImplementedException(); } diff --git a/app/Http/Controllers/Client/Servers/FileController.php b/app/Http/Controllers/Client/Servers/FileController.php new file mode 100644 index 0000000..0f04a74 --- /dev/null +++ b/app/Http/Controllers/Client/Servers/FileController.php @@ -0,0 +1,284 @@ +fileRepository + ->setServer($server) + ->getDirectory($request->get('directory') ?? '/'); + + return $this->fractal->collection($contents) + ->transformWith($this->getTransformer(FileObjectTransformer::class)) + ->toArray(); + } + + /** + * Return the contents of a specified file for the user. + * + * @throws \Throwable + */ + public function contents(GetFileContentsRequest $request, Server $server): Response + { + $response = $this->fileRepository->setServer($server)->getContent( + $request->get('file'), + config('pterodactyl.files.max_edit_size') + ); + + Activity::event('server:file.read')->property('file', $request->get('file'))->log(); + + return new Response($response, Response::HTTP_OK, ['Content-Type' => 'text/plain']); + } + + /** + * Returns true if file exists, false if it does not. + */ + public function exists(GetFileContentsRequest $request, Server $server): JsonResponse + { + try { + $response = $this->fileRepository->setServer($server)->getContent( + $request->get('file'), + config('pterodactyl.files.max_edit_size') + ); + + Activity::event('server:file.exists')->property('file', $request->get('file'))->log(); + + return new JsonResponse($response !== ''); + } catch (\Exception $exception) { + return new JsonResponse(false); + } + } + + /** + * Generates a one-time token with a link that the user can use to + * download a given file. + * + * @throws \Throwable + */ + public function download(GetFileContentsRequest $request, Server $server): array + { + $token = $this->jwtService + ->setExpiresAt(CarbonImmutable::now()->addMinutes(15)) + ->setUser($request->user()) + ->setClaims([ + 'file_path' => rawurldecode($request->get('file')), + 'server_uuid' => $server->uuid, + ]) + ->handle($server->node, $request->user()->id . $server->uuid); + + Activity::event('server:file.download')->property('file', $request->get('file'))->log(); + + return [ + 'object' => 'signed_url', + 'attributes' => [ + 'url' => sprintf( + '%s/download/file?token=%s', + $server->node->getConnectionAddress(), + $token->toString() + ), + ], + ]; + } + + /** + * Writes the contents of the specified file to the server. + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function write(WriteFileContentRequest $request, Server $server): JsonResponse + { + $this->fileRepository->setServer($server)->putContent($request->get('file'), $request->getContent()); + + Activity::event('server:file.write')->property('file', $request->get('file'))->log(); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } + + /** + * Creates a new folder on the server. + * + * @throws \Throwable + */ + public function create(CreateFolderRequest $request, Server $server): JsonResponse + { + $this->fileRepository + ->setServer($server) + ->createDirectory($request->input('name'), $request->input('root', '/')); + + Activity::event('server:file.create-directory') + ->property('name', $request->input('name')) + ->property('directory', $request->input('root')) + ->log(); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } + + /** + * Renames a file on the remote machine. + * + * @throws \Throwable + */ + public function rename(RenameFileRequest $request, Server $server): JsonResponse + { + $this->fileRepository + ->setServer($server) + ->renameFiles($request->input('root'), $request->input('files')); + + Activity::event('server:file.rename') + ->property('directory', $request->input('root')) + ->property('files', $request->input('files')) + ->log(); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } + + /** + * Copies a file on the server. + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function copy(CopyFileRequest $request, Server $server): JsonResponse + { + $this->fileRepository + ->setServer($server) + ->copyFile($request->input('location')); + + Activity::event('server:file.copy')->property('file', $request->input('location'))->log(); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } + + /** + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function compress(CompressFilesRequest $request, Server $server): array + { + $file = $this->fileRepository->setServer($server)->compressFiles( + $request->input('root'), + $request->input('files') + ); + + Activity::event('server:file.compress') + ->property('directory', $request->input('root')) + ->property('files', $request->input('files')) + ->log(); + + return $this->fractal->item($file) + ->transformWith($this->getTransformer(FileObjectTransformer::class)) + ->toArray(); + } + + /** + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function decompress(DecompressFilesRequest $request, Server $server): JsonResponse + { + set_time_limit(300); + + $this->fileRepository->setServer($server)->decompressFile( + $request->input('root'), + $request->input('file') + ); + + Activity::event('server:file.decompress') + ->property('directory', $request->input('root')) + ->property('files', $request->input('file')) + ->log(); + + return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); + } + + /** + * Deletes files or folders for the server in the given root directory. + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function delete(DeleteFileRequest $request, Server $server): JsonResponse + { + $this->fileRepository->setServer($server)->deleteFiles( + $request->input('root'), + $request->input('files') + ); + + Activity::event('server:file.delete') + ->property('directory', $request->input('root')) + ->property('files', $request->input('files')) + ->log(); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } + + /** + * Updates file permissions for file(s) in the given root directory. + * + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + */ + public function chmod(ChmodFilesRequest $request, Server $server): JsonResponse + { + $this->fileRepository->setServer($server)->chmodFiles( + $request->input('root'), + $request->input('files') + ); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } + + /** + * Requests that a file be downloaded from a remote location by Wings. + * + * @throws \Throwable + */ + public function pull(PullFileRequest $request, Server $server): JsonResponse + { + $this->fileRepository->setServer($server)->pull( + $request->input('url'), + $request->input('directory'), + $request->safe(['filename', 'use_header', 'foreground']) + ); + + Activity::event('server:file.pull') + ->property('directory', $request->input('directory')) + ->property('url', $request->input('url')) + ->log(); + + return new JsonResponse([], Response::HTTP_NO_CONTENT); + } +} diff --git a/resources/scripts/api/server/files/getFileExists.ts b/resources/scripts/api/server/files/getFileExists.ts new file mode 100644 index 0000000..3003692 --- /dev/null +++ b/resources/scripts/api/server/files/getFileExists.ts @@ -0,0 +1,11 @@ +import http from '@/api/http'; + +export default (server: string, file: string): Promise => { + return new Promise((resolve, reject) => { + http.get(`/api/client/servers/${server}/files/exists`, { + params: { file }, + }) + .then(({ data }) => resolve(data)) + .catch(reject); + }); +}; diff --git a/resources/scripts/api/server/files/pullFile.ts b/resources/scripts/api/server/files/pullFile.ts new file mode 100644 index 0000000..833bafd --- /dev/null +++ b/resources/scripts/api/server/files/pullFile.ts @@ -0,0 +1,23 @@ +import http from '@/api/http'; + +export default (server: string, pullFileRequest: PullFileRequest): Promise => { + return new Promise((resolve, reject) => { + http.post(`/api/client/servers/${server}/files/pull`, { + url: encodeURI(pullFileRequest.url), + directory: pullFileRequest.directory, + filename: pullFileRequest.filename, + use_header: pullFileRequest.use_header, + foreground: pullFileRequest.foreground, + }) + .then(({ data }) => resolve(data)) + .catch(reject); + }); +}; + +export interface PullFileRequest { + url: string; + directory: string; + filename: string; + use_header: boolean; + foreground: boolean; +} diff --git a/resources/scripts/api/server/modpacks/Modpack.ts b/resources/scripts/api/server/modpacks/Modpack.ts index 24b2f31..7fdc594 100644 --- a/resources/scripts/api/server/modpacks/Modpack.ts +++ b/resources/scripts/api/server/modpacks/Modpack.ts @@ -77,7 +77,7 @@ export interface File { fileLength: number; downloadCount: number; downloadUrl: string; - gameVersion: string[]; + gameVersions: string[]; sortableGameVersion: GameVersion[]; dependencies: any[]; alternateFileId: number; diff --git a/resources/scripts/api/server/modpacks/getModpackDescription.ts b/resources/scripts/api/server/modpacks/getModpackDescription.ts new file mode 100644 index 0000000..1fbf323 --- /dev/null +++ b/resources/scripts/api/server/modpacks/getModpackDescription.ts @@ -0,0 +1,14 @@ +import http from "@/api/http"; + +export default (uuid: string, id: number): Promise => { + return new Promise((resolve, reject) => { + + console.log(uuid, id); + + http.get(`/api/client/servers/${uuid}/modpacks/${id}/description`) + .then((response) => { + resolve(response.data.data) + }) + .catch(reject); + }); +} diff --git a/resources/scripts/api/server/modpacks/getModpacks.ts b/resources/scripts/api/server/modpacks/getModpacks.ts index ca61a22..ef0f81e 100644 --- a/resources/scripts/api/server/modpacks/getModpacks.ts +++ b/resources/scripts/api/server/modpacks/getModpacks.ts @@ -11,8 +11,6 @@ export default (uuid: string, pageIndex: number = 0, filters: ModpackSearchFilte filterQuery += `modloader=0`; } - console.log(filterQuery); - http.get(`/api/client/servers/${uuid}/modpacks?pageindex=${pageIndex}${filterQuery}`) .then((response) => { resolve([(response.data.data || []).map((item: any) => rawDataToModpackData(item)), rawDataToModpackPaginationData(response.data.pagination), {modloaderType: filters.modloaderType}]) diff --git a/resources/scripts/components/server/modpacks/ModpackItem.tsx b/resources/scripts/components/server/modpacks/ModpackItem.tsx index 975a296..fd1a7f3 100644 --- a/resources/scripts/components/server/modpacks/ModpackItem.tsx +++ b/resources/scripts/components/server/modpacks/ModpackItem.tsx @@ -1,21 +1,121 @@ -import { Modpack } from '@/api/server/modpacks/Modpack'; +import { File, Modpack } from '@/api/server/modpacks/Modpack'; import GreyRowBox from '@/components/elements/GreyRowBox'; import TitledGreyBox from '@/components/elements/TitledGreyBox'; import React from 'react'; +import ModpackModal from './ModpackModal'; +import Button from '@/components/elements/Button'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faDownload, faGamepad } from '@fortawesome/free-solid-svg-icons'; +import getModpackDescription from '@/api/server/modpacks/getModpackDescription'; +import { ServerContext } from '@/state/server'; +import { get } from 'http'; +import pullFile from '@/api/server/files/pullFile'; +import getFileExists from '@/api/server/files/getFileExists'; +import Spinner from '@/components/elements/Spinner'; +import decompressFiles from '@/api/server/files/decompressFiles'; interface Props { modpack: Modpack; } -export default({modpack}: Props) => { - return ( -
- - - -

{modpack.summary}

-
-
-
- ); -} +export default ({ modpack }: Props) => { + const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid); + const [visible, setVisible] = React.useState(false); + const [installVisible, setInstallVisible] = React.useState(false); + const [modpackDesc, setModpackDesc] = React.useState(''); + const [installing, setInstalling] = React.useState(false); + + const openModal = (): void => { + setVisible(true); + }; + + const openInstallModal = (): void => { + setInstallVisible(true); + }; + + const install = async (latestFile: File): Promise => { + setInstalling(true); + pullFile(uuid, { + url: latestFile.downloadUrl, + directory: '/home/container', + filename: 'modpack.zip', + use_header: true, + foreground: true, + }); + + let modpackDownloaded = await getFileExists(uuid, '/home/container/modpack.zip'); + while (!modpackDownloaded) { + modpackDownloaded = await getFileExists(uuid, '/home/container/modpack.zip'); + } + + decompressFiles(uuid, '/home/container/modpack', '/home/container/modpack.zip'); + + setInstalling(false); + }; + + const getDescription = (): void => { + getModpackDescription(uuid, modpack.id).then((description) => setModpackDesc(description)); + }; + + const openSite = (): void => { + throw new Error('Function not implemented.'); + }; + + return ( +
+ + + +

{modpack.summary}

+
+
+ + setVisible(false)} modpack={modpack}> +
+ +

{modpack.name}

+ +

{modpack.summary}

+ {modpack.screenshots.map((screenshot) => ( + + ))} +
+ + {modpack.downloadCount} +
+
+ +
+
+ + setInstallVisible(false)} modpack={modpack}> +
+ {installing ? ( +
+

Installing...

+ +
+ ) : ( + modpack.latestFiles.map((file) => ( +
+

{file.displayName}

+
+ + {file.gameVersions[0]} +
+ +
+ )))} +
+
+
+ ); +}; diff --git a/resources/scripts/components/server/modpacks/ModpackModal.tsx b/resources/scripts/components/server/modpacks/ModpackModal.tsx new file mode 100644 index 0000000..3b107a2 --- /dev/null +++ b/resources/scripts/components/server/modpacks/ModpackModal.tsx @@ -0,0 +1,16 @@ +import { Modpack } from "@/api/server/modpacks/Modpack"; +import PortaledModal, { ModalProps } from "@/components/elements/Modal"; +import React, { PropsWithChildren } from "react"; + +export interface ModpackModalProps extends ModalProps { + modpack: Modpack; +} + +export default(props: PropsWithChildren) => { + + return ( + + {props.children} + + ) +} diff --git a/routes/api-client.php b/routes/api-client.php index e0bdee1..5de80cd 100644 --- a/routes/api-client.php +++ b/routes/api-client.php @@ -70,7 +70,8 @@ Route::group([ Route::group(['prefix' => '/modpacks'], function () { Route::get('/', [Client\Servers\CurseForge\ModpackController::class, 'index']); Route::get('/{modpack}', [Client\Servers\CurseForge\ModpackController::class, 'show']); - Route::get('/{modpack}/install', [Client\Servers\CurseForge\CurseForgeModpackController::class, 'install']); + Route::get('/{modpack}/install', [Client\Servers\CurseForge\ModpackController::class, 'install']); + Route::get('/{modpack}/description', [Client\Servers\CurseForge\ModpackController::class, 'description']); }); Route::group(['prefix' => '/databases'], function () { @@ -83,6 +84,7 @@ Route::group([ Route::group(['prefix' => '/files'], function () { Route::get('/list', [Client\Servers\FileController::class, 'directory']); Route::get('/contents', [Client\Servers\FileController::class, 'contents']); + Route::get('/exists', [Client\Servers\FileController::class, 'exists']); Route::get('/download', [Client\Servers\FileController::class, 'download']); Route::put('/rename', [Client\Servers\FileController::class, 'rename']); Route::post('/copy', [Client\Servers\FileController::class, 'copy']); From 11a2f2f272fd13a560403209bbe7ab66d8630ffd Mon Sep 17 00:00:00 2001 From: Job Rapati Date: Wed, 27 Sep 2023 15:03:11 +0200 Subject: [PATCH 5/5] Installs actually work what --- .../Servers/CurseForge/ModpackController.php | 63 ++++++- app/Jobs/ModInstall.php | 76 +++++++++ app/config/curseforge.php | 7 + .../api/server/modpacks/installModpack.ts | 12 ++ .../server/modpacks/ModpackItem.tsx | 84 +++++---- .../server/modpacks/ModpackModal.tsx | 160 ++++++++++++++++-- 6 files changed, 352 insertions(+), 50 deletions(-) create mode 100644 app/Jobs/ModInstall.php create mode 100644 app/config/curseforge.php create mode 100644 resources/scripts/api/server/modpacks/installModpack.ts diff --git a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php index 7ebdda7..9230f3c 100644 --- a/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php +++ b/app/Http/Controllers/Client/Servers/CurseForge/ModpackController.php @@ -5,17 +5,21 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers\CurseForge; use GuzzleHttp\Client; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; use Nette\NotImplementedException; use Pterodactyl\Exceptions\DisplayException; +use Pterodactyl\Jobs\ModInstall; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; +use Pterodactyl\Repositories\Wings\DaemonFileRepository; class ModpackController extends ClientApiController { private $http_client; private $minecraft_game_id; private $modpack_class_id; + private $api_key; + private DaemonFileRepository $fileRepository; - public function __construct() { + + public function __construct(DaemonFileRepository $fileRepository) { parent::__construct(); if(!config('curseforge.api_key')) { @@ -29,8 +33,11 @@ class ModpackController extends ClientApiController { ] ]); + $this->api_key = config('curseforge.api_key'); $this->minecraft_game_id = config('curseforge.minecraft_game_id'); $this->modpack_class_id = config('curseforge.minecraft_modpack_class_id'); + + $this->fileRepository = $fileRepository; } public function index(Request $request) { @@ -68,7 +75,55 @@ class ModpackController extends ClientApiController { throw new NotImplementedException(); } - public function install() { - throw new NotImplementedException(); + public function install($server, $modId, $fileId) { + $this->fileRepository->setServer($server)->deleteFiles('/', ['mods']); + $this->fileRepository->setServer($server)->deleteFiles('/', ['uninstallable.txt']); + + $modpackFile = json_decode($this->getFileById($modId, $fileId)); + + $data = $modpackFile->data; + $modpackFileUrl = $this->traceUrl($data->downloadUrl); + + $this->fileRepository->setServer($server)->pull( + $modpackFileUrl, + '/', + ['filename' => 'modpack.zip', 'foreground' => true] + ); + + $this->fileRepository->setServer($server)->decompressFile('', 'modpack.zip'); + $modpackManifest = json_decode($this->fileRepository->setServer($server)->getContent('manifest.json')); + + $job = ModInstall::dispatch($modpackManifest, $server, $this->api_key); + + return response()->json([ + 'success' => true, + ]); + } + + private function traceUrl(string $url): string { + $httpCode = ''; + while (!str_contains($httpCode, '200 OK')) { + $headers = get_headers($url, 5); + $httpCode = $headers[0]; + if (array_key_exists('Location', $headers) && !empty($headers['Location'])) { + $url = $headers['Location']; + } else { + break; + } + } + + return $url; + } + + private function getModById(int $modId): string { + $result = $this->http_client->get("mods/$modId"); + + return $result->getBody()->getContents(); + } + + private function getFileById(int $modId, int $fileId): string { + $result = $this->http_client->get("mods/$modId/files/$fileId"); + + return $result->getBody()->getContents(); } } diff --git a/app/Jobs/ModInstall.php b/app/Jobs/ModInstall.php new file mode 100644 index 0000000..8f8977f --- /dev/null +++ b/app/Jobs/ModInstall.php @@ -0,0 +1,76 @@ +api_key = $api_key; + $this->modpackManifest = $modpackManifest; + $this->server = $server; + } + + public function handle(DaemonFileRepository $fileRepository) { + $this->http_client = new Client([ + 'base_uri' => 'https://api.curseforge.com/v1/', + 'headers' => [ + 'x-api-key' => $this->api_key, + ] + ]); + + $uninstallable = []; + + foreach($this->modpackManifest->files as $file) { + $modFile = json_decode($this->getFileById($file->projectID, $file->fileID)); + $modFileUrl = $this->traceUrl($modFile->data->downloadUrl); + + if($modFileUrl == null) { + $uninstallable[] = $modFile->data->fileName; + continue; + } + + $fileRepository->setServer($this->server)->pull( + $modFileUrl, + 'mods', + ['filename' => $modFile->data->fileName, 'foreground' => true] + ); + } + + $fileRepository->putContent('uninstallable.txt', print_r($uninstallable)); + $fileRepository->deleteFiles('/', ['modpack.zip', 'manifest.json', $this->modpackManifest->overrides]); + } + + private function getFileById(int $modId, int $fileId): string { + $result = $this->http_client->get("mods/$modId/files/$fileId"); + + return $result->getBody()->getContents(); + } + + private function traceUrl(string $url): string { + $httpCode = ''; + while (!str_contains($httpCode, '200 OK')) { + $headers = get_headers($url, 5); + $httpCode = $headers[0]; + if (array_key_exists('Location', $headers) && !empty($headers['Location'])) { + $url = $headers['Location']; + } else { + break; + } + } + + return $url; + } +} diff --git a/app/config/curseforge.php b/app/config/curseforge.php new file mode 100644 index 0000000..39a902f --- /dev/null +++ b/app/config/curseforge.php @@ -0,0 +1,7 @@ + env('CURSEFORGE_API_KEY'), + 'minecraft_game_id' => '432', + 'minecraft_modpack_class_id' => '4471', +]; diff --git a/resources/scripts/api/server/modpacks/installModpack.ts b/resources/scripts/api/server/modpacks/installModpack.ts new file mode 100644 index 0000000..726cd87 --- /dev/null +++ b/resources/scripts/api/server/modpacks/installModpack.ts @@ -0,0 +1,12 @@ +import http from "@/api/http"; + +export default (server: string, modpackId: number, fileId: number): Promise => { + return new Promise((resolve, reject) => { + http.get(`/api/client/servers/${server}/modpacks/${modpackId}/install/${fileId}`) + .then(({ data }) => { + console.log(data); + resolve(data) + }) + .catch(reject); + }); +} diff --git a/resources/scripts/components/server/modpacks/ModpackItem.tsx b/resources/scripts/components/server/modpacks/ModpackItem.tsx index fd1a7f3..2480fd0 100644 --- a/resources/scripts/components/server/modpacks/ModpackItem.tsx +++ b/resources/scripts/components/server/modpacks/ModpackItem.tsx @@ -8,20 +8,38 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faDownload, faGamepad } from '@fortawesome/free-solid-svg-icons'; import getModpackDescription from '@/api/server/modpacks/getModpackDescription'; import { ServerContext } from '@/state/server'; -import { get } from 'http'; -import pullFile from '@/api/server/files/pullFile'; -import getFileExists from '@/api/server/files/getFileExists'; import Spinner from '@/components/elements/Spinner'; -import decompressFiles from '@/api/server/files/decompressFiles'; +import installModpack from '@/api/server/modpacks/installModpack'; +import getFileExists from '@/api/server/files/getFileExists'; interface Props { modpack: Modpack; } +interface ModpackManifest { + author: string; + files: ModpackFile[] + manifestType: string; + manifestVersion: number; + minecraft: {}; + name: string; + overrides: string; + version: string; +} + +interface ModpackFile { + projectID: number; + fileID: number; + required: boolean; +} + +const timer = (ms: number) => new Promise(res => setTimeout(res, ms)) + export default ({ modpack }: Props) => { const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid); const [visible, setVisible] = React.useState(false); const [installVisible, setInstallVisible] = React.useState(false); + const [installed, setInstalled] = React.useState(false); const [modpackDesc, setModpackDesc] = React.useState(''); const [installing, setInstalling] = React.useState(false); @@ -35,22 +53,17 @@ export default ({ modpack }: Props) => { const install = async (latestFile: File): Promise => { setInstalling(true); - pullFile(uuid, { - url: latestFile.downloadUrl, - directory: '/home/container', - filename: 'modpack.zip', - use_header: true, - foreground: true, - }); - let modpackDownloaded = await getFileExists(uuid, '/home/container/modpack.zip'); - while (!modpackDownloaded) { - modpackDownloaded = await getFileExists(uuid, '/home/container/modpack.zip'); + await installModpack(uuid, modpack.id, latestFile.id); + + let uninstallableExists = false; + while (!uninstallableExists) { + uninstallableExists = await getFileExists(uuid, 'uninstallable.txt') as unknown as boolean; + await timer(6500); } - decompressFiles(uuid, '/home/container/modpack', '/home/container/modpack.zip'); - setInstalling(false); + setInstalled(true); }; const getDescription = (): void => { @@ -94,26 +107,31 @@ export default ({ modpack }: Props) => {
{installing ? (
-

Installing...

+

Installing... This can take a while, sit back, grab a drink and relax

+ ) : (<>{installed ? ( +

Modpack insatlled, if any mods couldn't be manually installed you can find them in the file 'uninstallable.txt', install them manually before playing.

) : ( - modpack.latestFiles.map((file) => ( -
-

{file.displayName}

-
- - {file.gameVersions[0]} -
- -
- )))} + modpack.latestFiles.map((file) => ( +
+

{file.displayName}

+
+ + {file.gameVersions[0]} +
+ +
+ )) + )} + + )}
diff --git a/resources/scripts/components/server/modpacks/ModpackModal.tsx b/resources/scripts/components/server/modpacks/ModpackModal.tsx index 3b107a2..e3e5bb4 100644 --- a/resources/scripts/components/server/modpacks/ModpackModal.tsx +++ b/resources/scripts/components/server/modpacks/ModpackModal.tsx @@ -1,16 +1,150 @@ -import { Modpack } from "@/api/server/modpacks/Modpack"; -import PortaledModal, { ModalProps } from "@/components/elements/Modal"; -import React, { PropsWithChildren } from "react"; +) => { +/* +|-------------------------------------------------------------------------- +| Client Control API +|-------------------------------------------------------------------------- +| +| Endpoint: /api/client +| +*/ +Route::get('/', [Client\ClientController::class, 'index'])->name('api:client.index'); +Route::get('/permissions', [Client\ClientController::class, 'permissions']); - return ( - - {props.children} - - ) -} +Route::prefix('/account')->middleware(AccountSubject::class)->group(function () { + Route::prefix('/')->withoutMiddleware(RequireTwoFactorAuthentication::class)->group(function () { + Route::get('/', [Client\AccountController::class, 'index'])->name('api:client.account'); + Route::get('/two-factor', [Client\TwoFactorController::class, 'index']); + Route::post('/two-factor', [Client\TwoFactorController::class, 'store']); + Route::delete('/two-factor', [Client\TwoFactorController::class, 'delete']); + }); + + Route::put('/email', [Client\AccountController::class, 'updateEmail'])->name('api:client.account.update-email'); + Route::put('/password', [Client\AccountController::class, 'updatePassword'])->name('api:client.account.update-password'); + + Route::get('/activity', Client\ActivityLogController::class)->name('api:client.account.activity'); + + Route::get('/api-keys', [Client\ApiKeyController::class, 'index']); + Route::post('/api-keys', [Client\ApiKeyController::class, 'store']); + Route::delete('/api-keys/{identifier}', [Client\ApiKeyController::class, 'delete']); + + Route::prefix('/ssh-keys')->group(function () { + Route::get('/', [Client\SSHKeyController::class, 'index']); + Route::post('/', [Client\SSHKeyController::class, 'store']); + Route::post('/remove', [Client\SSHKeyController::class, 'delete']); + }); +}); + +/* +|-------------------------------------------------------------------------- +| Client Control API +|-------------------------------------------------------------------------- +| +| Endpoint: /api/client/servers/{server} +| +*/ +Route::group([ + 'prefix' => '/servers/{server}', + 'middleware' => [ + ServerSubject::class, + AuthenticateServerAccess::class, + ResourceBelongsToServer::class, + ], +], function () { + Route::get('/', [Client\Servers\ServerController::class, 'index'])->name('api:client:server.view'); + Route::get('/websocket', Client\Servers\WebsocketController::class)->name('api:client:server.ws'); + Route::get('/resources', Client\Servers\ResourceUtilizationController::class)->name('api:client:server.resources'); + Route::get('/activity', Client\Servers\ActivityLogController::class)->name('api:client:server.activity'); + + Route::post('/command', [Client\Servers\CommandController::class, 'index']); + Route::post('/power', [Client\Servers\PowerController::class, 'index']); + + Route::group(['prefix' => '/modpacks'], function () { + Route::get('/', [Client\Servers\CurseForge\ModpackController::class, 'index']); + Route::get('/{modpack}', [Client\Servers\CurseForge\ModpackController::class, 'show']); + Route::get('/{modpack}/install/{file}', [Client\Servers\CurseForge\ModpackController::class, 'install']); + Route::get('/{modpack}/description', [Client\Servers\CurseForge\ModpackController::class, 'description']); + }); + + Route::group(['prefix' => '/databases'], function () { + Route::get('/', [Client\Servers\DatabaseController::class, 'index']); + Route::post('/', [Client\Servers\DatabaseController::class, 'store']); + Route::post('/{database}/rotate-password', [Client\Servers\DatabaseController::class, 'rotatePassword']); + Route::delete('/{database}', [Client\Servers\DatabaseController::class, 'delete']); + }); + + Route::group(['prefix' => '/files'], function () { + Route::get('/list', [Client\Servers\FileController::class, 'directory']); + Route::get('/contents', [Client\Servers\FileController::class, 'contents']); + Route::get('/exists', [Client\Servers\FileController::class, 'exists']); + Route::get('/download', [Client\Servers\FileController::class, 'download']); + Route::put('/rename', [Client\Servers\FileController::class, 'rename']); + Route::post('/copy', [Client\Servers\FileController::class, 'copy']); + Route::post('/write', [Client\Servers\FileController::class, 'write']); + Route::post('/compress', [Client\Servers\FileController::class, 'compress']); + Route::post('/decompress', [Client\Servers\FileController::class, 'decompress']); + Route::post('/delete', [Client\Servers\FileController::class, 'delete']); + Route::post('/create-folder', [Client\Servers\FileController::class, 'create']); + Route::post('/chmod', [Client\Servers\FileController::class, 'chmod']); + Route::post('/pull', [Client\Servers\FileController::class, 'pull'])->middleware(['throttle:10,5']); + Route::get('/upload', Client\Servers\FileUploadController::class); + }); + + Route::group(['prefix' => '/schedules'], function () { + Route::get('/', [Client\Servers\ScheduleController::class, 'index']); + Route::post('/', [Client\Servers\ScheduleController::class, 'store']); + Route::get('/{schedule}', [Client\Servers\ScheduleController::class, 'view']); + Route::post('/{schedule}', [Client\Servers\ScheduleController::class, 'update']); + Route::post('/{schedule}/execute', [Client\Servers\ScheduleController::class, 'execute']); + Route::delete('/{schedule}', [Client\Servers\ScheduleController::class, 'delete']); + + Route::post('/{schedule}/tasks', [Client\Servers\ScheduleTaskController::class, 'store']); + Route::post('/{schedule}/tasks/{task}', [Client\Servers\ScheduleTaskController::class, 'update']); + Route::delete('/{schedule}/tasks/{task}', [Client\Servers\ScheduleTaskController::class, 'delete']); + }); + + Route::group(['prefix' => '/network'], function () { + Route::get('/allocations', [Client\Servers\NetworkAllocationController::class, 'index']); + Route::post('/allocations', [Client\Servers\NetworkAllocationController::class, 'store']); + Route::post('/allocations/{allocation}', [Client\Servers\NetworkAllocationController::class, 'update']); + Route::post('/allocations/{allocation}/primary', [Client\Servers\NetworkAllocationController::class, 'setPrimary']); + Route::delete('/allocations/{allocation}', [Client\Servers\NetworkAllocationController::class, 'delete']); + }); + + Route::group(['prefix' => '/users'], function () { + Route::get('/', [Client\Servers\SubuserController::class, 'index']); + Route::post('/', [Client\Servers\SubuserController::class, 'store']); + Route::get('/{user}', [Client\Servers\SubuserController::class, 'view']); + Route::post('/{user}', [Client\Servers\SubuserController::class, 'update']); + Route::delete('/{user}', [Client\Servers\SubuserController::class, 'delete']); + }); + + Route::group(['prefix' => '/backups'], function () { + Route::get('/', [Client\Servers\BackupController::class, 'index']); + Route::post('/', [Client\Servers\BackupController::class, 'store']); + Route::get('/{backup}', [Client\Servers\BackupController::class, 'view']); + Route::get('/{backup}/download', [Client\Servers\BackupController::class, 'download']); + Route::post('/{backup}/lock', [Client\Servers\BackupController::class, 'toggleLock']); + Route::post('/{backup}/restore', [Client\Servers\BackupController::class, 'restore']); + Route::delete('/{backup}', [Client\Servers\BackupController::class, 'delete']); + }); + + Route::group(['prefix' => '/startup'], function () { + Route::get('/', [Client\Servers\StartupController::class, 'index']); + Route::put('/variable', [Client\Servers\StartupController::class, 'update']); + }); + + Route::group(['prefix' => '/settings'], function () { + Route::post('/rename', [Client\Servers\SettingsController::class, 'rename']); + Route::post('/reinstall', [Client\Servers\SettingsController::class, 'reinstall']); + Route::put('/docker-image', [Client\Servers\SettingsController::class, 'dockerImage']); + }); +});