support downloading multiple clipboards at once

This commit is contained in:
Jesse Boyd 2018-04-12 20:00:04 +10:00
parent 79fe27ab3e
commit 7968cdc232
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
3 changed files with 100 additions and 36 deletions

View File

@ -0,0 +1,16 @@
package com.boydti.fawe.object.io;
import java.io.IOException;
import java.io.OutputStream;
public class NonClosableOutputStream extends AbstractDelegateOutputStream {
public NonClosableOutputStream(OutputStream os) {
super(os);
}
@Override
public void close() throws IOException {
// Do nothing
}
}

View File

@ -473,7 +473,7 @@ public class MainUtil {
};
writeTask.value = nonClosable;
writeTask.run();
output.flush();
nonClosable.flush();
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
}

View File

@ -25,7 +25,10 @@ import com.boydti.fawe.config.BBC;
import com.boydti.fawe.config.Settings;
import com.boydti.fawe.object.FaweLimit;
import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.object.RunnableVal;
import com.boydti.fawe.object.clipboard.MultiClipboardHolder;
import com.boydti.fawe.object.clipboard.ReadOnlyClipboard;
import com.boydti.fawe.object.clipboard.URIClipboardHolder;
import com.boydti.fawe.object.clipboard.WorldCutClipboard;
import com.boydti.fawe.object.exception.FaweException;
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
@ -59,9 +62,17 @@ import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.command.binding.Switch;
import com.sk89q.worldedit.util.command.parametric.Optional;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static com.sk89q.minecraft.util.commands.Logging.LogMode.PLACEMENT;
@ -270,7 +281,45 @@ public class ClipboardCommands extends MethodCommands {
BBC.CLIPBOARD_INVALID_FORMAT.send(player, formatName);
return;
}
BBC.GENERATING_LINK.send(player, formatName);
ClipboardHolder holder = session.getClipboard();
URL url;
if (holder instanceof MultiClipboardHolder) {
MultiClipboardHolder multi = (MultiClipboardHolder) holder;
Set<File> files = new HashSet<>();
Set<URI> invalid = new HashSet<>();
for (ClipboardHolder cur : multi.getHolders()) {
if (cur instanceof URIClipboardHolder) {
URIClipboardHolder uriHolder = (URIClipboardHolder) cur;
URI uri = uriHolder.getUri();
File file = new File(uri.getPath());
if (file.exists() && file.isFile()) {
files.add(file);
System.out.println("Valid " + file);
} else if (!uri.getPath().isEmpty()) {
System.out.println("Invalid " + uri);
invalid.add(uri);
}
}
}
url = MainUtil.upload(null, null, "zip", new RunnableVal<OutputStream>() {
@Override
public void run(OutputStream out) {
try (ZipOutputStream zos = new ZipOutputStream(out)) {
for (File file : files) {
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
Files.copy(file.toPath(), zos);
zos.closeEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} else {
Clipboard clipboard = holder.getClipboard();
final Transform transform = holder.getTransform();
final Clipboard target;
@ -283,8 +332,6 @@ public class ClipboardCommands extends MethodCommands {
} else {
target = clipboard;
}
BBC.GENERATING_LINK.send(player, formatName);
URL url;
switch (format) {
case PNG:
try {
@ -309,6 +356,7 @@ public class ClipboardCommands extends MethodCommands {
url = null;
break;
}
}
if (url == null) {
BBC.GENERATING_LINK_FAILED.send(player);
} else {