Skip to content

Commit d8edc83

Browse files
committed
Basic upload checks
1 parent 711ae5d commit d8edc83

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/main/java/sam/packer/PackerConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class PackerConfig {
88
private static final Path CONFIG_PATH = Path.of("config", "packer.properties");
99
public static String server_address = "localhost";
1010
public static int port = 8081;
11+
public static int max_upload_bytes = 15000000;
1112

1213
public static void load() {
1314
File configFile = CONFIG_PATH.toFile();
@@ -16,8 +17,9 @@ public static void load() {
1617
if (configFile.exists()) {
1718
try (InputStream is = new FileInputStream(configFile)) {
1819
props.load(is);
19-
server_address = props.getProperty("server-address", "localhost");
20-
port = Integer.parseInt(props.getProperty("port", "8081"));
20+
server_address = props.getProperty("server-address", server_address);
21+
port = Integer.parseInt(props.getProperty("port", String.valueOf(port)));
22+
max_upload_bytes = Integer.parseInt(props.getProperty("max-upload-bytes", String.valueOf(max_upload_bytes)));
2123
} catch (IOException e) {
2224
Packer.LOGGER.error("Failed to load config: {}", e.getMessage());
2325
}
@@ -31,6 +33,7 @@ public static void save() {
3133
Properties props = new Properties();
3234
props.setProperty("server-address", server_address);
3335
props.setProperty("port", String.valueOf(port));
36+
props.setProperty("max_upload_bytes", String.valueOf(max_upload_bytes));
3437
props.store(os, "Packer Configuration\nSet server-address to your Public IP or Domain.\n");
3538
} catch (IOException e) {
3639
Packer.LOGGER.error("Failed to save config: {}", e.getMessage());

src/main/java/sam/packer/PackerManager.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.security.MessageDigest;
3333
import java.security.NoSuchAlgorithmException;
3434
import java.security.SecureRandom;
35+
import java.util.Arrays;
3536
import java.util.Optional;
3637
import java.util.UUID;
3738
import java.util.function.Supplier;
@@ -47,21 +48,37 @@ public enum AssetType {
4748
public String subdir() {
4849
return "textures";
4950
}
51+
52+
@Override
53+
public boolean is_valid_extension(String extension) {
54+
return extension.equals("png") || extension.equals("mcmeta");
55+
}
5056
},
5157
model{
5258
@Override
5359
public String subdir() {
5460
return "models";
5561
}
62+
63+
@Override
64+
public boolean is_valid_extension(String extension) {
65+
return extension.equals("json");
66+
}
5667
},
5768
item_definition {
5869
@Override
5970
public String subdir() {
6071
return "items";
6172
}
73+
74+
@Override
75+
public boolean is_valid_extension(String extension) {
76+
return extension.equals("json");
77+
}
6278
};
6379

6480
public abstract String subdir();
81+
public abstract boolean is_valid_extension(String extension);
6582

6683
public static AssetType from_string(String type) {
6784
switch (type) {
@@ -123,6 +140,14 @@ public PackerManager(DedicatedServer server) {
123140
}
124141

125142
public void upload_file(UUID uploader, AssetType type, String name, byte[] contents) throws IOException {
143+
// Check contents
144+
if(contents.length > PackerConfig.max_upload_bytes) throw new IOException("File too big. Limit: "+PackerConfig.max_upload_bytes+" Bytes");
145+
146+
var split_name = name.split("\\.");
147+
var ext = split_name[split_name.length-1];
148+
if(!type.is_valid_extension(ext)) throw new IOException("Invalid extension for upload type!");
149+
150+
// Preform upload
126151
Path user_path = assets_path.resolve(uploader.toString());
127152
if(!Files.exists(user_path)) { Files.createDirectory(user_path); }
128153
Path asset_path = user_path.resolve(type.subdir());
@@ -173,8 +198,6 @@ public void rezip_pack() throws IOException{
173198

174199
String url = "http://"+PackerConfig.server_address + ":" + PackerConfig.port + "/api/pack.zip";
175200
String hash = getSHA1(zip_path.toFile());
176-
Packer.LOGGER.info(url);
177-
Packer.LOGGER.info(hash);
178201
packinfo = new MinecraftServer.ServerResourcePackInfo(UUID.randomUUID(), url,
179202
hash, true, Component.literal("Please accept the Packer resource pack!."));
180203
}
@@ -285,7 +308,7 @@ public static void register_command(CommandDispatcher<CommandSourceStack> dispat
285308
return 0;
286309
}
287310
stack.set(DataComponents.ITEM_MODEL, stack.getItem().components().get(DataComponents.ITEM_MODEL));
288-
311+
289312
return 1;
290313
}));
291314
node.then(model_node);

0 commit comments

Comments
 (0)