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 allowedTypes = new ArrayList() {{ 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); } }); } }