2015-06-28 09:19:07 +00:00
|
|
|
package eu.siacs.conversations.http;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
2021-03-21 19:45:26 +00:00
|
|
|
import com.google.common.util.concurrent.FutureCallback;
|
|
|
|
import com.google.common.util.concurrent.Futures;
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
|
|
|
import com.google.common.util.concurrent.MoreExecutors;
|
|
|
|
|
|
|
|
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
|
2021-03-19 13:57:15 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-02-16 10:14:16 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2021-03-21 19:45:26 +00:00
|
|
|
import java.util.concurrent.Future;
|
2015-07-10 12:14:45 +00:00
|
|
|
|
2015-06-28 09:19:07 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.DownloadableFile;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2015-07-20 12:26:29 +00:00
|
|
|
import eu.siacs.conversations.entities.Transferable;
|
2015-07-31 23:19:16 +00:00
|
|
|
import eu.siacs.conversations.services.AbstractConnectionManager;
|
2015-06-28 09:19:07 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2015-06-29 00:04:58 +00:00
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2021-03-19 13:57:15 +00:00
|
|
|
import okhttp3.Call;
|
|
|
|
import okhttp3.Callback;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
|
|
|
public class HttpUploadConnection implements Transferable, AbstractConnectionManager.ProgressListener {
|
|
|
|
|
|
|
|
static final List<String> WHITE_LISTED_HEADERS = Arrays.asList(
|
|
|
|
"Authorization",
|
|
|
|
"Cookie",
|
|
|
|
"Expires"
|
|
|
|
);
|
|
|
|
|
|
|
|
private final HttpConnectionManager mHttpConnectionManager;
|
|
|
|
private final XmppConnectionService mXmppConnectionService;
|
|
|
|
private final Method method;
|
|
|
|
private boolean delayed = false;
|
|
|
|
private DownloadableFile file;
|
|
|
|
private final Message message;
|
|
|
|
private SlotRequester.Slot slot;
|
|
|
|
private byte[] key = null;
|
|
|
|
|
|
|
|
private long transmitted = 0;
|
|
|
|
private Call mostRecentCall;
|
2021-03-21 19:45:26 +00:00
|
|
|
private ListenableFuture<SlotRequester.Slot> slotFuture;
|
2021-03-19 13:57:15 +00:00
|
|
|
|
|
|
|
public HttpUploadConnection(Message message, Method method, HttpConnectionManager httpConnectionManager) {
|
|
|
|
this.message = message;
|
|
|
|
this.method = method;
|
|
|
|
this.mHttpConnectionManager = httpConnectionManager;
|
|
|
|
this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean start() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getStatus() {
|
|
|
|
return STATUS_UPLOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-05-17 13:51:21 +00:00
|
|
|
public Long getFileSize() {
|
|
|
|
return file == null ? null : file.getExpectedSize();
|
2021-03-19 13:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getProgress() {
|
|
|
|
if (file == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (int) ((((double) transmitted) / file.getExpectedSize()) * 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void cancel() {
|
2021-03-21 19:45:26 +00:00
|
|
|
final ListenableFuture<SlotRequester.Slot> slotFuture = this.slotFuture;
|
|
|
|
if (slotFuture != null && !slotFuture.isDone()) {
|
2021-04-09 13:49:33 +00:00
|
|
|
if (slotFuture.cancel(true)) {
|
|
|
|
Log.d(Config.LOGTAG,"cancelled slot requester");
|
|
|
|
}
|
2021-03-21 19:45:26 +00:00
|
|
|
}
|
2021-03-19 13:57:15 +00:00
|
|
|
final Call call = this.mostRecentCall;
|
|
|
|
if (call != null && !call.isCanceled()) {
|
|
|
|
call.cancel();
|
2021-04-09 13:49:33 +00:00
|
|
|
Log.d(Config.LOGTAG,"cancelled HTTP request");
|
2021-03-19 13:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fail(String errorMessage) {
|
|
|
|
finish();
|
|
|
|
final Call call = this.mostRecentCall;
|
2021-03-21 19:45:26 +00:00
|
|
|
final Future<SlotRequester.Slot> slotFuture = this.slotFuture;
|
|
|
|
final boolean cancelled = (call != null && call.isCanceled()) || (slotFuture != null && slotFuture.isCancelled());
|
2021-03-19 13:57:15 +00:00
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void finish() {
|
|
|
|
mHttpConnectionManager.finishUploadConnection(this);
|
|
|
|
message.setTransferable(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init(boolean delay) {
|
|
|
|
final Account account = message.getConversation().getAccount();
|
|
|
|
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
|
2021-04-09 13:49:33 +00:00
|
|
|
final String mime;
|
2021-03-19 13:57:15 +00:00
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
|
2021-04-09 13:49:33 +00:00
|
|
|
mime = "application/pgp-encrypted";
|
2021-03-19 13:57:15 +00:00
|
|
|
} else {
|
2021-04-09 13:49:33 +00:00
|
|
|
mime = this.file.getMimeType();
|
2021-03-19 13:57:15 +00:00
|
|
|
}
|
|
|
|
final long originalFileSize = file.getSize();
|
|
|
|
this.delayed = delay;
|
|
|
|
if (Config.ENCRYPT_ON_HTTP_UPLOADED
|
|
|
|
|| message.getEncryption() == Message.ENCRYPTION_AXOLOTL
|
|
|
|
|| message.getEncryption() == Message.ENCRYPTION_OTR) {
|
|
|
|
this.key = new byte[44];
|
|
|
|
mXmppConnectionService.getRNG().nextBytes(this.key);
|
|
|
|
this.file.setKeyAndIv(this.key);
|
|
|
|
}
|
|
|
|
this.file.setExpectedSize(originalFileSize + (file.getKey() != null ? 16 : 0));
|
|
|
|
message.resetFileParams();
|
2021-03-21 19:45:26 +00:00
|
|
|
this.slotFuture = new SlotRequester(mXmppConnectionService).request(method, account, file, mime);
|
|
|
|
Futures.addCallback(this.slotFuture, new FutureCallback<SlotRequester.Slot>() {
|
2021-03-19 13:57:15 +00:00
|
|
|
@Override
|
2021-03-21 19:45:26 +00:00
|
|
|
public void onSuccess(@NullableDecl SlotRequester.Slot result) {
|
|
|
|
HttpUploadConnection.this.slot = result;
|
2021-04-09 13:49:33 +00:00
|
|
|
try {
|
|
|
|
HttpUploadConnection.this.upload();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
2021-03-19 13:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-21 19:45:26 +00:00
|
|
|
public void onFailure(@NotNull final Throwable throwable) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to request slot", throwable);
|
|
|
|
fail(throwable.getMessage());
|
2021-03-19 13:57:15 +00:00
|
|
|
}
|
2021-03-21 19:45:26 +00:00
|
|
|
}, MoreExecutors.directExecutor());
|
2021-03-19 13:57:15 +00:00
|
|
|
message.setTransferable(this);
|
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void upload() {
|
|
|
|
final OkHttpClient client = mHttpConnectionManager.buildHttpClient(
|
|
|
|
slot.put,
|
|
|
|
message.getConversation().getAccount(),
|
2021-04-18 13:46:37 +00:00
|
|
|
0,
|
2021-03-19 13:57:15 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
final RequestBody requestBody = AbstractConnectionManager.requestBody(file, this);
|
|
|
|
final Request request = new Request.Builder()
|
|
|
|
.url(slot.put)
|
|
|
|
.put(requestBody)
|
|
|
|
.headers(slot.headers)
|
|
|
|
.build();
|
|
|
|
Log.d(Config.LOGTAG, "uploading file to " + slot.put);
|
|
|
|
this.mostRecentCall = client.newCall(request);
|
|
|
|
this.mostRecentCall.enqueue(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NotNull Call call, IOException e) {
|
|
|
|
Log.d(Config.LOGTAG, "http upload failed", e);
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-09 13:49:33 +00:00
|
|
|
public void onResponse(@NotNull Call call, @NotNull Response response) {
|
2021-03-19 13:57:15 +00:00
|
|
|
final int code = response.code();
|
|
|
|
if (code == 200 || code == 201) {
|
|
|
|
Log.d(Config.LOGTAG, "finished uploading file");
|
|
|
|
final String get;
|
|
|
|
if (key != null) {
|
|
|
|
get = AesGcmURL.toAesGcmUrl(slot.get.newBuilder().fragment(CryptoHelper.bytesToHex(key)).build());
|
|
|
|
} else {
|
|
|
|
get = slot.get.toString();
|
|
|
|
}
|
|
|
|
mXmppConnectionService.getFileBackend().updateFileParams(message, get);
|
|
|
|
mXmppConnectionService.getFileBackend().updateMediaScanner(file);
|
|
|
|
finish();
|
|
|
|
if (!message.isPrivateMessage()) {
|
|
|
|
message.setCounterpart(message.getConversation().getJid().asBareJid());
|
|
|
|
}
|
|
|
|
mXmppConnectionService.resendMessage(message, delayed);
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, "http upload failed because response code was " + code);
|
|
|
|
fail("http upload failed because response code was " + code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message getMessage() {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onProgress(final long progress) {
|
|
|
|
this.transmitted = progress;
|
|
|
|
mHttpConnectionManager.updateConversationUi(false);
|
|
|
|
}
|
|
|
|
}
|