From cd423cf3c3f64e072d6d67458e277329d4c38663 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 11 Nov 2016 14:19:23 +0300 Subject: xmpp-ft: post message when file received --- juick-xmpp-ft/build.gradle | 1 + .../java/com/juick/components/XMPPFTServer.java | 33 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) (limited to 'juick-xmpp-ft') diff --git a/juick-xmpp-ft/build.gradle b/juick-xmpp-ft/build.gradle index 508075f3..cb19cbb3 100644 --- a/juick-xmpp-ft/build.gradle +++ b/juick-xmpp-ft/build.gradle @@ -18,6 +18,7 @@ dependencies { compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}" compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}" compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${jacksonVersion}" + compile 'commons-io:commons-io:2.5' compile 'javax.inject:javax.inject:1' compile 'org.apache.httpcomponents:httpclient:4.5.2' compile 'org.apache.commons:commons-dbcp2:2.1.1' 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 index bf8e0298..e8b9861c 100644 --- a/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java +++ b/juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java @@ -1,14 +1,22 @@ package com.juick.components; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.component.accept.ExternalComponent; import rocks.xmpp.extensions.filetransfer.FileTransfer; import rocks.xmpp.extensions.filetransfer.FileTransferManager; +import rocks.xmpp.extensions.oob.model.x.OobX; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -25,17 +33,29 @@ public class XMPPFTServer { 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"); + String juickJid = env.getProperty("xmppbot_jid", "juick@juick.com/Juick"); FileTransferManager fileTransferManager = component.getManager(FileTransferManager.class); fileTransferManager.addFileTransferOfferListener(e -> { try { - String mime = e.getMimeType(); - List allowedTypes = new ArrayList() {{ add("image/png"); add("image/jpeg"); }}; - if (allowedTypes.contains(mime)) { - FileTransfer ft = e.accept(Paths.get(tmpDir, e.getName())).get(); + List allowedTypes = new ArrayList() {{ add("png"); add("jpg"); }}; + if (allowedTypes.contains(FilenameUtils.getExtension(e.getName()))) { + Path filePath = Paths.get(tmpDir, e.getName()); + FileTransfer ft = e.accept(filePath).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"); + Message msg = new Message(); + msg.setType(Message.Type.CHAT); + msg.setFrom(e.getInitiator()); + msg.setTo(Jid.of(juickJid)); + msg.setBody(e.getDescription()); + try { + msg.addExtension(new OobX(new URL(filePath.toString()), "!!!!Juick!!")); + } catch (MalformedURLException e1) { + e1.printStackTrace(); + } + component.sendMessage(msg); } }); ft.transfer(); @@ -47,5 +67,10 @@ public class XMPPFTServer { logger.error("ft error", e1); } }); + try { + component.connect(); + } catch (XmppException e) { + logger.error("ft initialization error", e); + } } } -- cgit v1.2.3