2018-12-08 02:31:39 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.PointerAttachment;
|
2019-10-20 23:52:53 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2019-03-28 15:56:35 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
|
|
|
import org.thoughtcrime.securesms.mms.MediaStream;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
2019-10-20 23:52:53 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
2019-11-13 23:44:55 +00:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public class AttachmentUploadJob extends BaseJob implements InjectableType {
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "AttachmentUploadJob";
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
private static final String TAG = AttachmentUploadJob.class.getSimpleName();
|
|
|
|
|
2019-10-20 23:52:53 +00:00
|
|
|
private static final String KEY_ROW_ID = "row_id";
|
|
|
|
private static final String KEY_UNIQUE_ID = "unique_id";
|
|
|
|
private static final String KEY_DESTINATION = "destination";
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
private AttachmentId attachmentId;
|
2019-10-20 23:52:53 +00:00
|
|
|
private Address destination;
|
2018-12-08 02:31:39 +00:00
|
|
|
@Inject SignalServiceMessageSender messageSender;
|
|
|
|
|
2019-10-20 23:52:53 +00:00
|
|
|
public AttachmentUploadJob(AttachmentId attachmentId, Address destination) {
|
2019-03-28 15:56:35 +00:00
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
2019-10-21 21:41:57 +00:00
|
|
|
.setMaxAttempts(3)
|
2019-03-28 15:56:35 +00:00
|
|
|
.build(),
|
2019-10-20 23:52:53 +00:00
|
|
|
attachmentId, destination);
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 23:52:53 +00:00
|
|
|
private AttachmentUploadJob(@NonNull Job.Parameters parameters, @NonNull AttachmentId attachmentId, Address destination) {
|
2019-03-28 15:56:35 +00:00
|
|
|
super(parameters);
|
2018-12-08 02:31:39 +00:00
|
|
|
this.attachmentId = attachmentId;
|
2019-10-20 23:52:53 +00:00
|
|
|
this.destination = destination;
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putLong(KEY_ROW_ID, attachmentId.getRowId())
|
|
|
|
.putLong(KEY_UNIQUE_ID, attachmentId.getUniqueId())
|
2019-10-20 23:52:53 +00:00
|
|
|
.putString(KEY_DESTINATION, destination.serialize())
|
2019-03-28 15:56:35 +00:00
|
|
|
.build();
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws Exception {
|
|
|
|
AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
DatabaseAttachment databaseAttachment = database.getAttachment(attachmentId);
|
|
|
|
|
|
|
|
if (databaseAttachment == null) {
|
|
|
|
throw new IllegalStateException("Cannot find the specified attachment.");
|
|
|
|
}
|
2019-11-13 23:44:55 +00:00
|
|
|
|
|
|
|
// Only upload attachment if necessary
|
|
|
|
if (databaseAttachment.getUrl().isEmpty()) {
|
|
|
|
MediaConstraints mediaConstraints = MediaConstraints.getPushMediaConstraints();
|
|
|
|
Attachment scaledAttachment = scaleAndStripExif(database, mediaConstraints, databaseAttachment);
|
|
|
|
SignalServiceAttachment localAttachment = getAttachmentFor(scaledAttachment);
|
|
|
|
SignalServiceAttachmentPointer remoteAttachment = messageSender.uploadAttachment(localAttachment.asStream(), databaseAttachment.isSticker(), new SignalServiceAddress(destination.serialize()));
|
|
|
|
Attachment attachment = PointerAttachment.forPointer(Optional.of(remoteAttachment), null, databaseAttachment.getFastPreflightId()).get();
|
|
|
|
|
|
|
|
database.updateAttachmentAfterUpload(databaseAttachment.getAttachmentId(), attachment);
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public void onCanceled() { }
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
protected boolean onShouldRetry(@NonNull Exception exception) {
|
2019-04-17 14:21:30 +00:00
|
|
|
return exception instanceof IOException;
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 08:41:05 +00:00
|
|
|
private SignalServiceAttachment getAttachmentFor(Attachment attachment) {
|
2018-12-08 02:31:39 +00:00
|
|
|
try {
|
|
|
|
if (attachment.getDataUri() == null || attachment.getSize() == 0) throw new IOException("Assertion failed, outgoing attachment has no data!");
|
|
|
|
InputStream is = PartAuthority.getAttachmentStream(context, attachment.getDataUri());
|
|
|
|
return SignalServiceAttachment.newStreamBuilder()
|
|
|
|
.withStream(is)
|
|
|
|
.withContentType(attachment.getContentType())
|
|
|
|
.withLength(attachment.getSize())
|
|
|
|
.withFileName(attachment.getFileName())
|
|
|
|
.withVoiceNote(attachment.isVoiceNote())
|
|
|
|
.withWidth(attachment.getWidth())
|
|
|
|
.withHeight(attachment.getHeight())
|
|
|
|
.withCaption(attachment.getCaption())
|
|
|
|
.withListener((total, progress) -> EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress)))
|
|
|
|
.build();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, "Couldn't open attachment", ioe);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Attachment scaleAndStripExif(@NonNull AttachmentDatabase attachmentDatabase,
|
|
|
|
@NonNull MediaConstraints constraints,
|
|
|
|
@NonNull Attachment attachment)
|
|
|
|
throws UndeliverableMessageException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (constraints.isSatisfied(context, attachment)) {
|
|
|
|
if (MediaUtil.isJpeg(attachment)) {
|
|
|
|
MediaStream stripped = constraints.getResizedMedia(context, attachment);
|
|
|
|
return attachmentDatabase.updateAttachmentData(attachment, stripped);
|
|
|
|
} else {
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
} else if (constraints.canResize(attachment)) {
|
|
|
|
MediaStream resized = constraints.getResizedMedia(context, attachment);
|
|
|
|
return attachmentDatabase.updateAttachmentData(attachment, resized);
|
|
|
|
} else {
|
|
|
|
throw new UndeliverableMessageException("Size constraints could not be met!");
|
|
|
|
}
|
|
|
|
} catch (IOException | MmsException e) {
|
|
|
|
throw new UndeliverableMessageException(e);
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 15:56:35 +00:00
|
|
|
|
|
|
|
public static final class Factory implements Job.Factory<AttachmentUploadJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull AttachmentUploadJob create(@NonNull Parameters parameters, @NonNull org.thoughtcrime.securesms.jobmanager.Data data) {
|
2019-10-20 23:52:53 +00:00
|
|
|
return new AttachmentUploadJob(parameters, new AttachmentId(data.getLong(KEY_ROW_ID), data.getLong(KEY_UNIQUE_ID)), Address.fromSerialized(data.getString(KEY_DESTINATION)));
|
2019-03-28 15:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|