aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-11 13:23:07 +0300
committerGravatar Vitaly Takmazov2016-11-11 13:23:07 +0300
commitd47bdfaf13b3eda524f3c606b4bf7e60435cda26 (patch)
tree7c5de8792afd74f66dccefc758a77f4afdc4331f /juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java
parent99efc3e6edbb7877091124336d6e115da0af67b1 (diff)
initial filetransfer bot
Diffstat (limited to 'juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java')
-rw-r--r--juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java51
1 files changed, 51 insertions, 0 deletions
diff --git a/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java b/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java
new file mode 100644
index 00000000..bf8e0298
--- /dev/null
+++ b/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java
@@ -0,0 +1,51 @@
+package com.juick.components;
+
+import org.apache.commons.lang3.math.NumberUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
+import rocks.xmpp.extensions.component.accept.ExternalComponent;
+import rocks.xmpp.extensions.filetransfer.FileTransfer;
+import rocks.xmpp.extensions.filetransfer.FileTransferManager;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * Created by vitalyster on 11.11.2016.
+ */
+public class XMPPFTServer {
+ private static final Logger logger = LoggerFactory.getLogger(XMPPFTServer.class);
+
+ public XMPPFTServer(Environment env) {
+ ExternalComponent component = ExternalComponent.create(env.getProperty("component_name", "files"),
+ env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"),
+ NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347));
+ String tmpDir = env.getProperty("upload_tmp_dir", "/tmp");
+ FileTransferManager fileTransferManager = component.getManager(FileTransferManager.class);
+ fileTransferManager.addFileTransferOfferListener(e -> {
+ try {
+ String mime = e.getMimeType();
+ List<String> allowedTypes = new ArrayList<String>() {{ add("image/png"); add("image/jpeg"); }};
+ if (allowedTypes.contains(mime)) {
+ FileTransfer ft = e.accept(Paths.get(tmpDir, e.getName())).get();
+ ft.addFileTransferStatusListener(st -> {
+ logger.debug(String.format("%s: received %d of %d", e.getName(), st.getBytesTransferred(), e.getSize()));
+ if (ft.isDone()) {
+ logger.info("transfer completed");
+ }
+ });
+ ft.transfer();
+ logger.info("transfer started");
+ } else {
+ e.reject();
+ }
+ } catch (IOException | InterruptedException | ExecutionException e1) {
+ logger.error("ft error", e1);
+ }
+ });
+ }
+}