diff options
author | Vitaly Takmazov | 2016-11-11 14:19:23 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-11 14:19:23 +0300 |
commit | cd423cf3c3f64e072d6d67458e277329d4c38663 (patch) | |
tree | 7bc4e32f67aaf4a112b6bdfa7fdcdf5b44d7b327 /juick-xmpp-ft/src/main/java/com | |
parent | d47bdfaf13b3eda524f3c606b4bf7e60435cda26 (diff) |
xmpp-ft: post message when file received
Diffstat (limited to 'juick-xmpp-ft/src/main/java/com')
-rw-r--r-- | juick-xmpp-ft/src/main/java/com/juick/components/XMPPFTServer.java | 33 |
1 files changed, 29 insertions, 4 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 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<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(); + List<String> allowedTypes = new ArrayList<String>() {{ 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); + } } } |