Added Delete functionality

This commit is contained in:
TheHomecraft 2025-10-21 00:20:16 +02:00
parent 98d9623184
commit 6a80a92f82
3 changed files with 83 additions and 13 deletions

View File

@ -32,13 +32,31 @@ public class Main {
} }
} }
public static void main(String[] args) { public static void main(String[] args) throws Exception {
if (args.length < 5) { Scanner scanner = new Scanner(System.in);
System.out.println("Useage: TPkgCreator.jar pack <repoName> <packageName> <repoOwner> <pathToFiles> "); if (args.length == 0) {
System.out.println("Usage: TPkgCreator.jar pack <repoName> <packageName> <repoOwner> <pathToFiles>");
System.out.println("Usage: TPkgCreator.jar delete <repoName>");
args = scanner.nextLine().split(" ");
}
if (args.length < 1) {
System.out.println("Usage: TPkgCreator.jar pack <repoName> <packageName> <repoOwner> <pathToFiles>");
System.out.println("Usage: TPkgCreator.jar delete <repoName>");
return; return;
} }
if (args[0].equals("pack")) { if (args[0].equals("delete")) {
new SendSocketMessage().send(args[1], "delete");
try {
new S3Uploader().deleteRemoteDirectory(args[1]);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("Successfully deleted "+args[1]);
System.exit(0);
}
if (args[0].equals("pack") && args.length >= 5) {
if (!new File(args[4]).exists() && !args[4].equals("5")) { if (!new File(args[4]).exists() && !args[4].equals("5")) {
System.out.println(args[4] + " does not exist"); System.out.println(args[4] + " does not exist");
return; return;
@ -67,7 +85,7 @@ public class Main {
preJson.put("numOfFiles", String.valueOf(numOfFiles)); preJson.put("numOfFiles", String.valueOf(numOfFiles));
preJson.put("timeCreated", String.valueOf(Date.from(Instant.now()))); preJson.put("timeCreated", String.valueOf(Date.from(Instant.now())));
preJson.put("lastTimeChanged", String.valueOf(Date.from(Instant.now()))); preJson.put("lastTimeChanged", String.valueOf(Date.from(Instant.now())));
System.out.println(fileList.toString()); //System.out.println(fileList.toString());
try { try {
preJson.put("fileList", new ObjectMapper().writeValueAsString(fileList)); preJson.put("fileList", new ObjectMapper().writeValueAsString(fileList));
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
@ -82,14 +100,13 @@ public class Main {
try { try {
System.out.println("Sending packet creation Command!"); System.out.println("Sending packet creation Command!");
new SendSocketMessage().send(new ObjectMapper().writeValueAsString(preJson)); new SendSocketMessage().send(new ObjectMapper().writeValueAsString(preJson), "create");
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
System.exit(0);
} }
} }

View File

@ -1,12 +1,18 @@
package de.thehomecraft.S3; package de.thehomecraft.S3;
import io.minio.MinioClient; import io.minio.*;
import io.minio.PutObjectArgs; import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class S3Uploader { public class S3Uploader {
public void uploadDirectory(File baseDir, File currentDir) throws Exception { public void uploadDirectory(File baseDir, File currentDir) throws Exception {
MinioClient client = MinioClient.builder() MinioClient client = MinioClient.builder()
@ -35,4 +41,51 @@ public class S3Uploader {
} }
} }
} }
public void deleteRemoteDirectory(String repoName) throws Exception {
String bucketName = "repofiles";
MinioClient client = MinioClient.builder()
.endpoint("http://192.168.178.24:9000")
.credentials(new CredProvid().s3AccessKey, new CredProvid().s3SecretKey)
.build();
System.out.println("Deleting "+repoName);
String prefix = repoName+"/";
// List all objects under the given prefix
Iterable<Result<Item>> results = client.listObjects(
ListObjectsArgs.builder()
.bucket(bucketName)
.prefix(prefix)
.recursive(true)
.build()
);
boolean foundAny = false;
for (Result<Item> result : results) {
Item item = result.get();
String objectName = item.objectName();
try {
client.removeObject(
RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build()
);
System.out.println("Deleted: " + objectName);
foundAny = true;
} catch (Exception e) {
System.err.println("Failed to delete: " + objectName + " (" + e.getMessage() + ")");
}
}
if (!foundAny) {
System.out.println("No objects found under prefix: " + prefix);
} else {
System.out.println("Finished deleting remote directory: " + prefix);
}
}
} }

View File

@ -7,10 +7,10 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
public class SendSocketMessage { public class SendSocketMessage {
public void send(String text){ public void send(String text, String endpoint){
URI serverUri; URI serverUri;
try { try {
serverUri = new URI("ws://192.168.178.24:7000/repo/create"); serverUri = new URI("ws://192.168.178.24:7000/repo/"+endpoint);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -27,7 +27,7 @@ public class SendSocketMessage {
@Override @Override
public void onClose(int i, String s, boolean b) { public void onClose(int i, String s, boolean b) {
System.exit(0);
} }
@Override @Override