Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package com.mattmalec.pterodactyl4j.client.entities;

import com.mattmalec.pterodactyl4j.PteroAction;
import com.mattmalec.pterodactyl4j.client.managers.CompressAction;
import com.mattmalec.pterodactyl4j.client.managers.DeleteAction;
import com.mattmalec.pterodactyl4j.client.managers.RenameAction;
import com.mattmalec.pterodactyl4j.client.managers.UploadFileAction;
import com.mattmalec.pterodactyl4j.client.managers.*;

import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -67,5 +65,5 @@ default Optional<Directory> getDirectoryByName(String name) {

CompressAction compress();

PteroAction<Void> decompress(File compressedFile);
DecompressAction decompress(File compressedFile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mattmalec.pterodactyl4j.client.entities;

import com.mattmalec.pterodactyl4j.PteroAction;
import com.mattmalec.pterodactyl4j.client.managers.DecompressAction;

public interface File extends GenericFile {

Expand All @@ -28,5 +29,5 @@ public interface File extends GenericFile {

PteroAction<Void> copy();

PteroAction<Void> decompress();
DecompressAction decompress();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package com.mattmalec.pterodactyl4j.client.entities.impl;

import com.mattmalec.pterodactyl4j.client.entities.ClientServer;
import com.mattmalec.pterodactyl4j.client.entities.Directory;
import com.mattmalec.pterodactyl4j.client.entities.File;
import com.mattmalec.pterodactyl4j.client.entities.GenericFile;
import com.mattmalec.pterodactyl4j.client.managers.CompressAction;
import com.mattmalec.pterodactyl4j.requests.PteroActionImpl;
import com.mattmalec.pterodactyl4j.requests.Route;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import okhttp3.RequestBody;
Expand All @@ -32,13 +34,15 @@
public class CompressActionImpl extends PteroActionImpl<File> implements CompressAction {

private final List<GenericFile> files;
private Directory rootDirectory;

public CompressActionImpl(ClientServer server, PteroClientImpl impl) {
super(
impl.getP4J(),
Route.Files.COMPRESS_FILES.compile(server.getIdentifier()),
(response, request) -> new FileImpl(response.getObject(), "/", server));
this.files = new ArrayList<>();
setHandler((response, request) -> new FileImpl(response.getObject(), (rootDirectory == null ? "/" : rootDirectory.getPath()), server));
}

@Override
Expand All @@ -47,6 +51,12 @@ public CompressAction addFile(GenericFile file) {
return this;
}

@Override
public CompressAction addFiles(Collection<GenericFile> files) {
this.files.addAll(files);
return this;
}

@Override
public CompressAction addFiles(GenericFile file, GenericFile... files) {
this.files.add(file);
Expand All @@ -56,6 +66,12 @@ public CompressAction addFiles(GenericFile file, GenericFile... files) {
return this;
}

@Override
public CompressAction setRoot(Directory rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}

@Override
public CompressAction clearFiles() {
files.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,64 @@
package com.mattmalec.pterodactyl4j.client.entities.impl;

import com.mattmalec.pterodactyl4j.client.entities.ClientServer;
import com.mattmalec.pterodactyl4j.client.entities.Directory;
import com.mattmalec.pterodactyl4j.client.entities.File;
import com.mattmalec.pterodactyl4j.client.managers.DecompressAction;
import com.mattmalec.pterodactyl4j.exceptions.IllegalActionException;
import com.mattmalec.pterodactyl4j.requests.PteroActionImpl;
import com.mattmalec.pterodactyl4j.requests.Route;
import com.mattmalec.pterodactyl4j.utils.Checks;
import okhttp3.RequestBody;
import org.json.JSONObject;

public class DecompressActionImpl extends PteroActionImpl<Void> {
public class DecompressActionImpl extends PteroActionImpl<Void> implements DecompressAction {

private final File compressedFile;

private Directory rootDirectory;

public DecompressActionImpl(ClientServer server, File compressedFile, PteroClientImpl impl) {
super(impl.getP4J(), Route.Files.DECOMPRESS_FILE.compile(server.getIdentifier()));
this.compressedFile = compressedFile;
}

@Override
public DecompressAction setRoot(Directory rootDirectory) {
this.rootDirectory = rootDirectory;
return this;
}

@Override
protected RequestBody finalizeData() {
Checks.notNull(compressedFile, "Compressed File");

JSONObject json = new JSONObject().put("file", compressedFile.getPath()).put("root", "/");
if(rootDirectory == null) {
String filePath = compressedFile.getPath();

if(filePath.endsWith("/")) {
filePath = filePath.substring(0, filePath.length() - 1);
}

int lastSlashIndex = filePath.lastIndexOf('/');
String parentDir;

if(lastSlashIndex == -1) {
parentDir = "/";
filePath = compressedFile.getPath();
} else {
parentDir = filePath.substring(0, lastSlashIndex);
filePath = filePath.substring(lastSlashIndex + 1);
}

JSONObject json = new JSONObject().put("root", parentDir).put("file", filePath);
return getRequestBody(json);
}
String rootPath = rootDirectory.getPath() + "/";
String filePath = compressedFile.getPath();
if(!filePath.startsWith(rootPath)) {
throw new IllegalActionException("compressed file isn't within the given root directory");
}
JSONObject json = new JSONObject().put("root", rootDirectory.getPath()).put("file", filePath.substring(rootPath.length()));
return getRequestBody(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import com.mattmalec.pterodactyl4j.client.entities.Directory;
import com.mattmalec.pterodactyl4j.client.entities.File;
import com.mattmalec.pterodactyl4j.client.entities.GenericFile;
import com.mattmalec.pterodactyl4j.client.managers.CompressAction;
import com.mattmalec.pterodactyl4j.client.managers.DeleteAction;
import com.mattmalec.pterodactyl4j.client.managers.RenameAction;
import com.mattmalec.pterodactyl4j.client.managers.UploadFileAction;
import com.mattmalec.pterodactyl4j.client.managers.*;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -78,7 +76,7 @@ public CompressAction compress() {
}

@Override
public PteroAction<Void> decompress(File compressedFile) {
public DecompressAction decompress(File compressedFile) {
return server.getFileManager().decompress(compressedFile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.mattmalec.pterodactyl4j.client.entities.ClientServer;
import com.mattmalec.pterodactyl4j.client.entities.DownloadableFile;
import com.mattmalec.pterodactyl4j.client.entities.File;
import com.mattmalec.pterodactyl4j.client.managers.DecompressAction;
import com.mattmalec.pterodactyl4j.client.managers.FileManager;
import org.json.JSONObject;

Expand Down Expand Up @@ -53,7 +54,7 @@ public PteroAction<Void> copy() {
}

@Override
public PteroAction<Void> decompress() {
public DecompressAction decompress() {
return fileManager.decompress(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CompressAction compress() {
}

@Override
public PteroAction<Void> decompress(File compressedFile) {
public DecompressAction decompress(File compressedFile) {
return new DecompressActionImpl(server, compressedFile, impl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
package com.mattmalec.pterodactyl4j.client.managers;

import com.mattmalec.pterodactyl4j.PteroAction;
import com.mattmalec.pterodactyl4j.client.entities.Directory;
import com.mattmalec.pterodactyl4j.client.entities.File;
import com.mattmalec.pterodactyl4j.client.entities.GenericFile;

import java.util.Collection;
public interface CompressAction extends PteroAction<File> {

CompressAction addFile(GenericFile file);

CompressAction addFiles(Collection<GenericFile> files);

CompressAction addFiles(GenericFile file, GenericFile... files);

CompressAction setRoot(Directory rootDirectory);

CompressAction clearFiles();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mattmalec.pterodactyl4j.client.managers;

import com.mattmalec.pterodactyl4j.PteroAction;
import com.mattmalec.pterodactyl4j.client.entities.Directory;

public interface DecompressAction extends PteroAction<Void> {
DecompressAction setRoot(Directory rootDirectory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface FileManager {

CompressAction compress();

PteroAction<Void> decompress(File compressedFile);
DecompressAction decompress(File compressedFile);

DeleteAction delete();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PteroActionImpl<T> implements PteroAction<T> {
private final Route.CompiledRoute route;
private final RequestBody data;
private long deadline = 0;
private final BiFunction<Response, Request<T>, T> handler;
private BiFunction<Response, Request<T>, T> handler;

public static <T> DeferredPteroAction<T> onExecute(P4J api, Supplier<? extends T> supplier) {
return new DeferredPteroAction<>(api, supplier);
Expand Down Expand Up @@ -136,6 +136,10 @@ public void handleResponse(Response response, Request<T> request) {
else request.setOnFailure(response);
}

public void setHandler(BiFunction<Response, Request<T>, T> handler) {
this.handler = handler;
}

public void handleSuccess(Response response, Request<T> request) {
if (response.isEmpty() || handler == null) request.onSuccess(null);
else request.onSuccess(handler.apply(response, request));
Expand Down