1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-19 00:04:53 +00:00

Make maximum upload file size configurable (#1417)

This commit is contained in:
khankul 2023-05-17 20:07:16 +07:00 committed by GitHub
parent 470f2b098d
commit e24b5f0888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 5 deletions

View File

@ -55,6 +55,7 @@ public abstract class AbstractComputerScreen<T extends AbstractComputerMenu> ext
protected final int sidebarYOffset; protected final int sidebarYOffset;
private long uploadNagDeadline = Long.MAX_VALUE; private long uploadNagDeadline = Long.MAX_VALUE;
private final int uploadMaxSize;
private final ItemStack displayStack; private final ItemStack displayStack;
public AbstractComputerScreen(T container, Inventory player, Component title, int sidebarYOffset) { public AbstractComputerScreen(T container, Inventory player, Component title, int sidebarYOffset) {
@ -62,6 +63,7 @@ public abstract class AbstractComputerScreen<T extends AbstractComputerMenu> ext
terminalData = container.getTerminal(); terminalData = container.getTerminal();
family = container.getFamily(); family = container.getFamily();
displayStack = container.getDisplayStack(); displayStack = container.getDisplayStack();
uploadMaxSize = container.getUploadMaxSize();
input = new ClientInputHandler(menu); input = new ClientInputHandler(menu);
this.sidebarYOffset = sidebarYOffset; this.sidebarYOffset = sidebarYOffset;
} }
@ -161,7 +163,7 @@ public abstract class AbstractComputerScreen<T extends AbstractComputerMenu> ext
try (var sbc = Files.newByteChannel(file)) { try (var sbc = Files.newByteChannel(file)) {
var fileSize = sbc.size(); var fileSize = sbc.size();
if (fileSize > UploadFileMessage.MAX_SIZE || (size += fileSize) >= UploadFileMessage.MAX_SIZE) { if (fileSize > uploadMaxSize || (size += fileSize) >= uploadMaxSize) {
alert(UploadResult.FAILED_TITLE, UploadResult.TOO_MUCH_MSG); alert(UploadResult.FAILED_TITLE, UploadResult.TOO_MUCH_MSG);
return; return;
} }

View File

@ -208,6 +208,7 @@ public final class LanguageProvider implements DataProvider {
// Config options // Config options
addConfigEntry(ConfigSpec.computerSpaceLimit, "Computer space limit (bytes)"); addConfigEntry(ConfigSpec.computerSpaceLimit, "Computer space limit (bytes)");
addConfigEntry(ConfigSpec.floppySpaceLimit, "Floppy Disk space limit (bytes)"); addConfigEntry(ConfigSpec.floppySpaceLimit, "Floppy Disk space limit (bytes)");
addConfigEntry(ConfigSpec.uploadMaxSize, "File upload size limit (bytes)");
addConfigEntry(ConfigSpec.maximumFilesOpen, "Maximum files open per computer"); addConfigEntry(ConfigSpec.maximumFilesOpen, "Maximum files open per computer");
addConfigEntry(ConfigSpec.disableLua51Features, "Disable Lua 5.1 features"); addConfigEntry(ConfigSpec.disableLua51Features, "Disable Lua 5.1 features");
addConfigEntry(ConfigSpec.defaultComputerSettings, "Default Computer settings"); addConfigEntry(ConfigSpec.defaultComputerSettings, "Default Computer settings");

View File

@ -5,6 +5,7 @@
package dan200.computercraft.shared.computer.inventory; package dan200.computercraft.shared.computer.inventory;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.config.Config;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.menu.ComputerMenu; import dan200.computercraft.shared.computer.menu.ComputerMenu;
@ -26,6 +27,7 @@ import java.util.function.Predicate;
public abstract class AbstractComputerMenu extends AbstractContainerMenu implements ComputerMenu { public abstract class AbstractComputerMenu extends AbstractContainerMenu implements ComputerMenu {
public static final int SIDEBAR_WIDTH = 17; public static final int SIDEBAR_WIDTH = 17;
private final int uploadMaxSize;
private final Predicate<Player> canUse; private final Predicate<Player> canUse;
private final ComputerFamily family; private final ComputerFamily family;
@ -52,6 +54,7 @@ public abstract class AbstractComputerMenu extends AbstractContainerMenu impleme
input = computer == null ? null : new ServerInputState<>(this); input = computer == null ? null : new ServerInputState<>(this);
terminal = containerData == null ? null : containerData.terminal().create(); terminal = containerData == null ? null : containerData.terminal().create();
displayStack = containerData == null ? ItemStack.EMPTY : containerData.displayStack(); displayStack = containerData == null ? ItemStack.EMPTY : containerData.displayStack();
uploadMaxSize = containerData == null ? Config.uploadMaxSize : containerData.uploadMaxSize();
} }
@Override @Override
@ -67,6 +70,10 @@ public abstract class AbstractComputerMenu extends AbstractContainerMenu impleme
return data.get(0) != 0; return data.get(0) != 0;
} }
public int getUploadMaxSize() {
return uploadMaxSize;
}
@Override @Override
public ServerComputer getComputer() { public ServerComputer getComputer() {
if (computer == null) throw new UnsupportedOperationException("Cannot access server computer on the client"); if (computer == null) throw new UnsupportedOperationException("Cannot access server computer on the client");

View File

@ -14,6 +14,7 @@ import dan200.computercraft.shared.peripheral.monitor.MonitorRenderer;
public final class Config { public final class Config {
public static int computerSpaceLimit = 1000 * 1000; public static int computerSpaceLimit = 1000 * 1000;
public static int floppySpaceLimit = 125 * 1000; public static int floppySpaceLimit = 125 * 1000;
public static int uploadMaxSize = 512 * 1024; // 512 KB
public static boolean commandRequireCreative = true; public static boolean commandRequireCreative = true;
public static boolean enableCommandBlock = false; public static boolean enableCommandBlock = false;

View File

@ -32,6 +32,7 @@ public final class ConfigSpec {
public static final ConfigFile.Value<String> defaultComputerSettings; public static final ConfigFile.Value<String> defaultComputerSettings;
public static final ConfigFile.Value<Boolean> logComputerErrors; public static final ConfigFile.Value<Boolean> logComputerErrors;
public static final ConfigFile.Value<Boolean> commandRequireCreative; public static final ConfigFile.Value<Boolean> commandRequireCreative;
public static final ConfigFile.Value<Integer> uploadMaxSize;
public static final ConfigFile.Value<Integer> computerThreads; public static final ConfigFile.Value<Integer> computerThreads;
public static final ConfigFile.Value<Integer> maxMainGlobalTime; public static final ConfigFile.Value<Integer> maxMainGlobalTime;
@ -96,6 +97,13 @@ public final class ConfigSpec {
.comment("The disk space limit for floppy disks, in bytes.") .comment("The disk space limit for floppy disks, in bytes.")
.define("floppy_space_limit", Config.floppySpaceLimit); .define("floppy_space_limit", Config.floppySpaceLimit);
uploadMaxSize = builder
.comment("""
The file upload size limit, in bytes. Must be in range of 1 KiB and 16 MiB.
Keep in mind that uploads are processed in a single tick - large files or
poor network performance can stall the networking thread. And mind the disk space!""")
.defineInRange("upload_max_size", Config.uploadMaxSize, 1024, 16 * 1024 * 1024);
maximumFilesOpen = builder maximumFilesOpen = builder
.comment("Set how many files a computer can have open at the same time. Set to 0 for unlimited.") .comment("Set how many files a computer can have open at the same time. Set to 0 for unlimited.")
.defineInRange("maximum_open_files", CoreConfig.maximumFilesOpen, 0, Integer.MAX_VALUE); .defineInRange("maximum_open_files", CoreConfig.maximumFilesOpen, 0, Integer.MAX_VALUE);
@ -334,6 +342,7 @@ public final class ConfigSpec {
// General // General
Config.computerSpaceLimit = computerSpaceLimit.get(); Config.computerSpaceLimit = computerSpaceLimit.get();
Config.floppySpaceLimit = floppySpaceLimit.get(); Config.floppySpaceLimit = floppySpaceLimit.get();
Config.uploadMaxSize = uploadMaxSize.get();
CoreConfig.maximumFilesOpen = maximumFilesOpen.get(); CoreConfig.maximumFilesOpen = maximumFilesOpen.get();
CoreConfig.disableLua51Features = disableLua51Features.get(); CoreConfig.disableLua51Features = disableLua51Features.get();
CoreConfig.defaultComputerSettings = defaultComputerSettings.get(); CoreConfig.defaultComputerSettings = defaultComputerSettings.get();

View File

@ -7,6 +7,7 @@ package dan200.computercraft.shared.network.container;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.terminal.TerminalState; import dan200.computercraft.shared.computer.terminal.TerminalState;
import dan200.computercraft.shared.config.Config;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -14,17 +15,20 @@ public class ComputerContainerData implements ContainerData {
private final ComputerFamily family; private final ComputerFamily family;
private final TerminalState terminal; private final TerminalState terminal;
private final ItemStack displayStack; private final ItemStack displayStack;
private final int uploadMaxSize;
public ComputerContainerData(ServerComputer computer, ItemStack displayStack) { public ComputerContainerData(ServerComputer computer, ItemStack displayStack) {
family = computer.getFamily(); family = computer.getFamily();
terminal = computer.getTerminalState(); terminal = computer.getTerminalState();
this.displayStack = displayStack; this.displayStack = displayStack;
uploadMaxSize = Config.uploadMaxSize;
} }
public ComputerContainerData(FriendlyByteBuf buf) { public ComputerContainerData(FriendlyByteBuf buf) {
family = buf.readEnum(ComputerFamily.class); family = buf.readEnum(ComputerFamily.class);
terminal = new TerminalState(buf); terminal = new TerminalState(buf);
displayStack = buf.readItem(); displayStack = buf.readItem();
uploadMaxSize = buf.readInt();
} }
@Override @Override
@ -32,6 +36,7 @@ public class ComputerContainerData implements ContainerData {
buf.writeEnum(family); buf.writeEnum(family);
terminal.write(buf); terminal.write(buf);
buf.writeItem(displayStack); buf.writeItem(displayStack);
buf.writeInt(uploadMaxSize);
} }
public ComputerFamily family() { public ComputerFamily family() {
@ -50,4 +55,8 @@ public class ComputerContainerData implements ContainerData {
public ItemStack displayStack() { public ItemStack displayStack() {
return displayStack; return displayStack;
} }
public int uploadMaxSize() {
return uploadMaxSize;
}
} }

View File

@ -8,6 +8,7 @@ import com.google.common.annotations.VisibleForTesting;
import dan200.computercraft.shared.computer.menu.ComputerMenu; import dan200.computercraft.shared.computer.menu.ComputerMenu;
import dan200.computercraft.shared.computer.upload.FileSlice; import dan200.computercraft.shared.computer.upload.FileSlice;
import dan200.computercraft.shared.computer.upload.FileUpload; import dan200.computercraft.shared.computer.upload.FileUpload;
import dan200.computercraft.shared.config.Config;
import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.DecoderException;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
@ -22,7 +23,6 @@ import java.util.function.Consumer;
import static dan200.computercraft.core.util.Nullability.assertNonNull; import static dan200.computercraft.core.util.Nullability.assertNonNull;
public class UploadFileMessage extends ComputerServerMessage { public class UploadFileMessage extends ComputerServerMessage {
public static final int MAX_SIZE = 512 * 1024;
static final int MAX_PACKET_SIZE = 30 * 1024; // Max packet size is 32767. static final int MAX_PACKET_SIZE = 30 * 1024; // Max packet size is 32767.
private static final int HEADER_SIZE = 16 + 1; // 16 bytes for the UUID, 4 for the flag. private static final int HEADER_SIZE = 16 + 1; // 16 bytes for the UUID, 4 for the flag.
@ -60,7 +60,7 @@ public class UploadFileMessage extends ComputerServerMessage {
for (var i = 0; i < nFiles; i++) { for (var i = 0; i < nFiles; i++) {
var name = buf.readUtf(MAX_FILE_NAME); var name = buf.readUtf(MAX_FILE_NAME);
var size = buf.readVarInt(); var size = buf.readVarInt();
if (size > MAX_SIZE || (totalSize += size) > MAX_SIZE) { if (size > Config.uploadMaxSize || (totalSize += size) > Config.uploadMaxSize) {
throw new DecoderException("Files are too large"); throw new DecoderException("Files are too large");
} }

View File

@ -5,6 +5,7 @@
package dan200.computercraft.shared.network.server; package dan200.computercraft.shared.network.server;
import dan200.computercraft.shared.computer.upload.FileUpload; import dan200.computercraft.shared.computer.upload.FileUpload;
import dan200.computercraft.shared.config.Config;
import dan200.computercraft.test.core.ArbitraryByteBuffer; import dan200.computercraft.test.core.ArbitraryByteBuffer;
import dan200.computercraft.test.shared.FakeContainer; import dan200.computercraft.test.shared.FakeContainer;
import dan200.computercraft.test.shared.WithMinecraft; import dan200.computercraft.test.shared.WithMinecraft;
@ -114,7 +115,7 @@ public class UploadFileMessageTest {
Arbitraries.strings().ofMinLength(1).ascii().ofMaxLength(MAX_FILE_NAME), Arbitraries.strings().ofMinLength(1).ascii().ofMaxLength(MAX_FILE_NAME),
Arbitraries.strings().ofMinLength(1).ofMaxLength(MAX_FILE_NAME / 4) Arbitraries.strings().ofMinLength(1).ofMaxLength(MAX_FILE_NAME / 4)
)), )),
ArbitraryByteBuffer.bytes().ofMaxSize(MAX_SIZE) ArbitraryByteBuffer.bytes().ofMaxSize(Config.uploadMaxSize)
).as(UploadFileMessageTest::file); ).as(UploadFileMessageTest::file);
} }
@ -122,7 +123,7 @@ public class UploadFileMessageTest {
Arbitrary<List<FileUpload>> fileUploads() { Arbitrary<List<FileUpload>> fileUploads() {
return fileUpload().list() return fileUpload().list()
.ofMinSize(1).ofMaxSize(MAX_FILES) .ofMinSize(1).ofMaxSize(MAX_FILES)
.filter(us -> us.stream().mapToInt(u -> u.getBytes().remaining()).sum() <= MAX_SIZE); .filter(us -> us.stream().mapToInt(u -> u.getBytes().remaining()).sum() <= Config.uploadMaxSize);
} }
private static FileUpload file(String name, ByteBuffer buffer) { private static FileUpload file(String name, ByteBuffer buffer) {

View File

@ -163,6 +163,8 @@
"gui.computercraft.config.turtle.normal_fuel_limit": "Turtle fuel limit", "gui.computercraft.config.turtle.normal_fuel_limit": "Turtle fuel limit",
"gui.computercraft.config.turtle.normal_fuel_limit.tooltip": "The fuel limit for Turtles.\nRange: > 0", "gui.computercraft.config.turtle.normal_fuel_limit.tooltip": "The fuel limit for Turtles.\nRange: > 0",
"gui.computercraft.config.turtle.tooltip": "Various options relating to turtles.", "gui.computercraft.config.turtle.tooltip": "Various options relating to turtles.",
"gui.computercraft.config.upload_max_size": "File upload size limit (bytes)",
"gui.computercraft.config.upload_max_size.tooltip": "The file upload size limit, in bytes. Must be in range of 1 KiB and 16 MiB.\nKeep in mind that uploads are processed in a single tick - large files or\npoor network performance can stall the networking thread. And mind the disk space!\nRange: 1024 ~ 16777216",
"gui.computercraft.config.upload_nag_delay": "Upload nag delay", "gui.computercraft.config.upload_nag_delay": "Upload nag delay",
"gui.computercraft.config.upload_nag_delay.tooltip": "The delay in seconds after which we'll notify about unhandled imports. Set to 0 to disable.\nRange: 0 ~ 60", "gui.computercraft.config.upload_nag_delay.tooltip": "The delay in seconds after which we'll notify about unhandled imports. Set to 0 to disable.\nRange: 0 ~ 60",
"gui.computercraft.pocket_computer_overlay": "Pocket computer open. Press ESC to close.", "gui.computercraft.pocket_computer_overlay": "Pocket computer open. Press ESC to close.",

View File

@ -163,6 +163,8 @@
"gui.computercraft.config.turtle.normal_fuel_limit": "Turtle fuel limit", "gui.computercraft.config.turtle.normal_fuel_limit": "Turtle fuel limit",
"gui.computercraft.config.turtle.normal_fuel_limit.tooltip": "The fuel limit for Turtles.\nRange: > 0", "gui.computercraft.config.turtle.normal_fuel_limit.tooltip": "The fuel limit for Turtles.\nRange: > 0",
"gui.computercraft.config.turtle.tooltip": "Various options relating to turtles.", "gui.computercraft.config.turtle.tooltip": "Various options relating to turtles.",
"gui.computercraft.config.upload_max_size": "File upload size limit (bytes)",
"gui.computercraft.config.upload_max_size.tooltip": "The file upload size limit, in bytes. Must be in range of 1 KiB and 16 MiB.\nKeep in mind that uploads are processed in a single tick - large files or\npoor network performance can stall the networking thread. And mind the disk space!\nRange: 1024 ~ 16777216",
"gui.computercraft.config.upload_nag_delay": "Upload nag delay", "gui.computercraft.config.upload_nag_delay": "Upload nag delay",
"gui.computercraft.config.upload_nag_delay.tooltip": "The delay in seconds after which we'll notify about unhandled imports. Set to 0 to disable.\nRange: 0 ~ 60", "gui.computercraft.config.upload_nag_delay.tooltip": "The delay in seconds after which we'll notify about unhandled imports. Set to 0 to disable.\nRange: 0 ~ 60",
"gui.computercraft.pocket_computer_overlay": "Pocket computer open. Press ESC to close.", "gui.computercraft.pocket_computer_overlay": "Pocket computer open. Press ESC to close.",