package de.thedevstack.conversationsplus.services.filetransfer.httpupload; import android.util.Pair; import java.io.FileNotFoundException; import java.io.InputStream; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.DownloadableFile; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.services.AbstractConnectionManager; import de.thedevstack.conversationsplus.services.FileTransferService; import de.thedevstack.conversationsplus.services.filetransfer.AbstractFileTransferService; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.XmppSendUtil; import de.thedevstack.conversationsplus.xmpp.httpupload.HttpUpload; import de.thedevstack.conversationsplus.xmpp.httpupload.HttpUploadRequestSlotPacketGenerator; import de.thedevstack.conversationsplus.xmpp.jid.Jid; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; /** * */ public class HttpUploadFileTransferService extends AbstractFileTransferService implements FileTransferService { public HttpUploadFileTransferService() { } /** * Transfers a file for the corresponding message. * * @param message the message containing the file to transfer * @return true if the file transfer was started successfully, false otherwise */ @Override public boolean transferFile(Message message) { return this.transferFile(message, false); } /** * Transfers a file for the corresponding message. * * @param message the message containing the file to transfer * @param delay whether the message is delayed or not * @return true if the file transfer was started successfully, false otherwise */ @Override public boolean transferFile(Message message, boolean delay) { Logging.d("httpupload", "Starting to upload file"); boolean started = false; try { final HttpFileTransferEntity entity = new HttpFileTransferEntity(message, delay); this.addStatusListenerToEntity(entity); entity.startAttempt(); Account account = message.getConversation().getAccount(); DownloadableFile file = entity.getFile(); Pair inputStreamAndExpectedSize = AbstractConnectionManager.createInputStream(file, true); entity.setFileInputStream(inputStreamAndExpectedSize.first); file.setExpectedSize(inputStreamAndExpectedSize.second); Logging.d("httpupload", "Requesting upload slot for file upload"); Jid host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); MessageUtil.markMessage(message, Message.STATUS_UNSEND); Logging.d("httpupload", "Upload slot for file upload requested"); started = true; } catch (FileNotFoundException e) { Logging.e("httpupload", "Could not find file, exception message: " + e.getMessage()); } return started; } /** * Checks whether a message can be sent using this service or not. * * @param message the message to be checked * @return true if the message can be processed, false otherwise */ @Override public boolean accept(Message message) { return null != message && null != message.getConversation() && null != message.getConversation().getAccount() && message.getConversation().getAccount().httpUploadAvailable(FileBackend.getFile(message, false).getSize()); } }