2018-12-08 02:31:39 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2019-10-17 12:26:08 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.os.Build;
|
2020-04-30 18:04:53 +00:00
|
|
|
import android.text.TextUtils;
|
2019-10-17 12:26:08 +00:00
|
|
|
|
2019-06-05 19:47:14 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-06-27 16:18:52 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
2020-12-04 23:31:58 +00:00
|
|
|
import org.signal.core.util.logging.Log;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2018-12-08 02:31:39 +00:00
|
|
|
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-17 12:26:08 +00:00
|
|
|
import org.thoughtcrime.securesms.blurhash.BlurHashEncoder;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2021-11-18 17:36:52 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SignalDatabase;
|
2019-07-15 15:12:26 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2018-12-08 02:31:39 +00:00
|
|
|
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.mms.PartAuthority;
|
2021-05-25 16:21:13 +00:00
|
|
|
import org.thoughtcrime.securesms.net.NotPushRegisteredException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.service.GenericForegroundService;
|
|
|
|
import org.thoughtcrime.securesms.service.NotificationController;
|
2019-10-17 12:26:08 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
2022-07-13 15:48:17 +00:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
|
2021-08-26 13:24:20 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResumableUploadResponseCodeException;
|
2020-04-16 20:06:18 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.ResumeLocationInvalidException;
|
|
|
|
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2020-09-15 13:27:34 +00:00
|
|
|
import java.util.Objects;
|
2022-03-14 19:49:46 +00:00
|
|
|
import java.util.Optional;
|
2018-12-08 02:31:39 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
/**
|
|
|
|
* Uploads an attachment without alteration.
|
|
|
|
* <p>
|
|
|
|
* Queue {@link AttachmentCompressionJob} before to compress.
|
|
|
|
*/
|
|
|
|
public final class AttachmentUploadJob extends BaseJob {
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
public static final String KEY = "AttachmentUploadJobV2";
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private static final String TAG = Log.tag(AttachmentUploadJob.class);
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2020-03-28 00:36:06 +00:00
|
|
|
private static final long UPLOAD_REUSE_THRESHOLD = TimeUnit.DAYS.toMillis(3);
|
|
|
|
|
2020-04-16 20:06:18 +00:00
|
|
|
private static final String KEY_ROW_ID = "row_id";
|
|
|
|
private static final String KEY_UNIQUE_ID = "unique_id";
|
2021-08-26 13:24:20 +00:00
|
|
|
private static final String KEY_FORCE_V2 = "force_v2";
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-06-27 16:18:52 +00:00
|
|
|
/**
|
|
|
|
* Foreground notification shows while uploading attachments above this.
|
|
|
|
*/
|
|
|
|
private static final int FOREGROUND_LIMIT = 10 * 1024 * 1024;
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
private final AttachmentId attachmentId;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2021-08-26 13:24:20 +00:00
|
|
|
private boolean forceV2;
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
public AttachmentUploadJob(AttachmentId attachmentId) {
|
2019-03-28 15:56:35 +00:00
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(Parameters.UNLIMITED)
|
|
|
|
.build(),
|
2021-08-26 13:24:20 +00:00
|
|
|
attachmentId,
|
|
|
|
false);
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 13:24:20 +00:00
|
|
|
private AttachmentUploadJob(@NonNull Job.Parameters parameters, @NonNull AttachmentId attachmentId, boolean forceV2) {
|
2019-03-28 15:56:35 +00:00
|
|
|
super(parameters);
|
2018-12-08 02:31:39 +00:00
|
|
|
this.attachmentId = attachmentId;
|
2021-08-26 13:24:20 +00:00
|
|
|
this.forceV2 = forceV2;
|
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())
|
2021-08-26 13:24:20 +00:00
|
|
|
.putBoolean(KEY_FORCE_V2, forceV2)
|
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
|
|
|
}
|
|
|
|
|
2020-12-20 06:04:03 +00:00
|
|
|
@Override
|
|
|
|
protected boolean shouldTrace() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:31:39 +00:00
|
|
|
@Override
|
|
|
|
public void onRun() throws Exception {
|
2021-05-25 16:21:13 +00:00
|
|
|
if (!Recipient.self().isRegistered()) {
|
|
|
|
throw new NotPushRegisteredException();
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:02:57 +00:00
|
|
|
Data inputData = getInputData();
|
2020-04-16 20:06:18 +00:00
|
|
|
|
2020-07-21 19:02:57 +00:00
|
|
|
ResumableUploadSpec resumableUploadSpec;
|
|
|
|
|
2021-08-26 13:24:20 +00:00
|
|
|
if (forceV2) {
|
|
|
|
Log.d(TAG, "Forcing utilization of V2");
|
|
|
|
resumableUploadSpec = null;
|
|
|
|
} else if (inputData != null && inputData.hasString(ResumableUploadSpecJob.KEY_RESUME_SPEC)) {
|
2020-07-21 19:02:57 +00:00
|
|
|
Log.d(TAG, "Using attachments V3");
|
2020-04-16 20:06:18 +00:00
|
|
|
resumableUploadSpec = ResumableUploadSpec.deserialize(inputData.getString(ResumableUploadSpecJob.KEY_RESUME_SPEC));
|
|
|
|
} else {
|
2020-07-21 19:02:57 +00:00
|
|
|
Log.d(TAG, "Using attachments V2");
|
2020-04-16 20:06:18 +00:00
|
|
|
resumableUploadSpec = null;
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:12:26 +00:00
|
|
|
SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
|
2021-11-18 17:36:52 +00:00
|
|
|
AttachmentDatabase database = SignalDatabase.attachments();
|
2019-07-15 15:12:26 +00:00
|
|
|
DatabaseAttachment databaseAttachment = database.getAttachment(attachmentId);
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
if (databaseAttachment == null) {
|
2019-08-02 20:31:10 +00:00
|
|
|
throw new InvalidAttachmentException("Cannot find the specified attachment.");
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 00:36:06 +00:00
|
|
|
long timeSinceUpload = System.currentTimeMillis() - databaseAttachment.getUploadTimestamp();
|
2020-04-30 18:04:53 +00:00
|
|
|
if (timeSinceUpload < UPLOAD_REUSE_THRESHOLD && !TextUtils.isEmpty(databaseAttachment.getLocation())) {
|
2020-03-28 00:36:06 +00:00
|
|
|
Log.i(TAG, "We can re-use an already-uploaded file. It was uploaded " + timeSinceUpload + " ms ago. Skipping.");
|
|
|
|
return;
|
|
|
|
} else if (databaseAttachment.getUploadTimestamp() > 0) {
|
|
|
|
Log.i(TAG, "This file was previously-uploaded, but too long ago to be re-used. Age: " + timeSinceUpload + " ms");
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:52:55 +00:00
|
|
|
Log.i(TAG, "Uploading attachment for message " + databaseAttachment.getMmsId() + " with ID " + databaseAttachment.getAttachmentId());
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
try (NotificationController notification = getNotificationForAttachment(databaseAttachment)) {
|
2022-07-13 15:48:17 +00:00
|
|
|
try (SignalServiceAttachmentStream localAttachment = getAttachmentFor(databaseAttachment, notification, resumableUploadSpec)) {
|
|
|
|
SignalServiceAttachmentPointer remoteAttachment = messageSender.uploadAttachment(localAttachment);
|
|
|
|
Attachment attachment = PointerAttachment.forPointer(Optional.of(remoteAttachment), null, databaseAttachment.getFastPreflightId()).get();
|
2019-06-27 16:18:52 +00:00
|
|
|
|
2022-07-13 15:48:17 +00:00
|
|
|
database.updateAttachmentAfterUpload(databaseAttachment.getAttachmentId(), attachment, remoteAttachment.getUploadTimestamp());
|
|
|
|
}
|
2021-08-26 13:24:20 +00:00
|
|
|
} catch (NonSuccessfulResumableUploadResponseCodeException e) {
|
|
|
|
if (e.getCode() == 400) {
|
|
|
|
Log.w(TAG, "Failed to upload due to a 400 when getting resumable upload information. Downgrading to attachments v2", e);
|
|
|
|
forceV2 = true;
|
|
|
|
}
|
2019-06-27 16:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-06-27 16:18:52 +00:00
|
|
|
private @Nullable NotificationController getNotificationForAttachment(@NonNull Attachment attachment) {
|
|
|
|
if (attachment.getSize() >= FOREGROUND_LIMIT) {
|
2022-10-12 13:48:40 +00:00
|
|
|
try {
|
|
|
|
return ForegroundUtil.requireForegroundTask(context, context.getString(R.string.AttachmentUploadJob_uploading_media));
|
|
|
|
} catch (GenericForegroundService.UnableToStartException e) {
|
|
|
|
Log.w(TAG, "Unable to start foreground service", e);
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-27 16:18:52 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-08 20:56:51 +00:00
|
|
|
public void onFailure() {
|
|
|
|
if (isCanceled()) {
|
2021-11-18 17:36:52 +00:00
|
|
|
SignalDatabase.attachments().deleteAttachment(attachmentId);
|
2020-01-08 20:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
protected boolean onShouldRetry(@NonNull Exception exception) {
|
2020-04-16 20:06:18 +00:00
|
|
|
if (exception instanceof ResumeLocationInvalidException) return false;
|
|
|
|
|
2021-05-25 16:21:13 +00:00
|
|
|
return exception instanceof IOException && !(exception instanceof NotPushRegisteredException);
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 15:48:17 +00:00
|
|
|
private @NonNull SignalServiceAttachmentStream getAttachmentFor(Attachment attachment, @Nullable NotificationController notification, @Nullable ResumableUploadSpec resumableUploadSpec) throws InvalidAttachmentException {
|
|
|
|
if (attachment.getUri() == null || attachment.getSize() == 0) {
|
|
|
|
throw new InvalidAttachmentException(new IOException("Assertion failed, outgoing attachment has no data!"));
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:31:39 +00:00
|
|
|
try {
|
2020-09-15 13:27:34 +00:00
|
|
|
InputStream is = PartAuthority.getAttachmentStream(context, attachment.getUri());
|
2019-10-17 12:26:08 +00:00
|
|
|
SignalServiceAttachment.Builder builder = SignalServiceAttachment.newStreamBuilder()
|
|
|
|
.withStream(is)
|
|
|
|
.withContentType(attachment.getContentType())
|
|
|
|
.withLength(attachment.getSize())
|
|
|
|
.withFileName(attachment.getFileName())
|
|
|
|
.withVoiceNote(attachment.isVoiceNote())
|
2020-07-06 21:13:08 +00:00
|
|
|
.withBorderless(attachment.isBorderless())
|
2021-04-14 19:44:03 +00:00
|
|
|
.withGif(attachment.isVideoGif())
|
2019-10-17 12:26:08 +00:00
|
|
|
.withWidth(attachment.getWidth())
|
|
|
|
.withHeight(attachment.getHeight())
|
2020-03-28 00:36:06 +00:00
|
|
|
.withUploadTimestamp(System.currentTimeMillis())
|
2019-10-17 12:26:08 +00:00
|
|
|
.withCaption(attachment.getCaption())
|
2020-01-08 20:56:51 +00:00
|
|
|
.withCancelationSignal(this::isCanceled)
|
2020-04-16 20:06:18 +00:00
|
|
|
.withResumableUploadSpec(resumableUploadSpec)
|
2019-10-17 12:26:08 +00:00
|
|
|
.withListener((total, progress) -> {
|
|
|
|
EventBus.getDefault().postSticky(new PartProgressEvent(attachment, PartProgressEvent.Type.NETWORK, total, progress));
|
|
|
|
if (notification != null) {
|
|
|
|
notification.setProgress(total, progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (MediaUtil.isImageType(attachment.getContentType())) {
|
|
|
|
return builder.withBlurHash(getImageBlurHash(attachment)).build();
|
|
|
|
} else if (MediaUtil.isVideoType(attachment.getContentType())) {
|
|
|
|
return builder.withBlurHash(getVideoBlurHash(attachment)).build();
|
|
|
|
} else {
|
|
|
|
return builder.build();
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
} catch (IOException ioe) {
|
2019-08-02 20:31:10 +00:00
|
|
|
throw new InvalidAttachmentException(ioe);
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 12:26:08 +00:00
|
|
|
private @Nullable String getImageBlurHash(@NonNull Attachment attachment) throws IOException {
|
|
|
|
if (attachment.getBlurHash() != null) return attachment.getBlurHash().getHash();
|
2020-09-15 13:27:34 +00:00
|
|
|
if (attachment.getUri() == null) return null;
|
2019-10-17 12:26:08 +00:00
|
|
|
|
2022-07-13 15:48:17 +00:00
|
|
|
try (InputStream inputStream = PartAuthority.getAttachmentStream(context, attachment.getUri())) {
|
|
|
|
return BlurHashEncoder.encode(inputStream);
|
|
|
|
}
|
2019-10-17 12:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private @Nullable String getVideoBlurHash(@NonNull Attachment attachment) throws IOException {
|
2020-09-15 13:27:34 +00:00
|
|
|
if (attachment.getBlurHash() != null) {
|
|
|
|
return attachment.getBlurHash().getHash();
|
2019-10-17 12:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < 23) {
|
|
|
|
Log.w(TAG, "Video thumbnails not supported...");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:27:34 +00:00
|
|
|
Bitmap bitmap = MediaUtil.getVideoThumbnail(context, Objects.requireNonNull(attachment.getUri()), 1000);
|
2019-10-17 12:26:08 +00:00
|
|
|
|
2020-09-15 13:27:34 +00:00
|
|
|
if (bitmap != null) {
|
|
|
|
Bitmap thumb = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
|
|
|
|
bitmap.recycle();
|
2019-10-17 12:26:08 +00:00
|
|
|
|
2020-09-15 13:27:34 +00:00
|
|
|
Log.i(TAG, "Generated video thumbnail...");
|
|
|
|
String hash = BlurHashEncoder.encode(thumb);
|
|
|
|
thumb.recycle();
|
2019-10-17 12:26:08 +00:00
|
|
|
|
2020-09-15 13:27:34 +00:00
|
|
|
return hash;
|
|
|
|
} else {
|
|
|
|
return null;
|
2019-10-17 12:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:31:10 +00:00
|
|
|
private class InvalidAttachmentException extends Exception {
|
|
|
|
InvalidAttachmentException(String message) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidAttachmentException(Exception e) {
|
|
|
|
super(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) {
|
2021-08-26 13:24:20 +00:00
|
|
|
return new AttachmentUploadJob(parameters, new AttachmentId(data.getLong(KEY_ROW_ID), data.getLong(KEY_UNIQUE_ID)), data.getBooleanOrDefault(KEY_FORCE_V2, false));
|
2019-03-28 15:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|