Search in sources :

Example 1 with ResumableUploadSpec

use of org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec in project Signal-Android by WhisperSystems.

the class AttachmentUploadJob method onRun.

@Override
public void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    Data inputData = getInputData();
    ResumableUploadSpec resumableUploadSpec;
    if (forceV2) {
        Log.d(TAG, "Forcing utilization of V2");
        resumableUploadSpec = null;
    } else if (inputData != null && inputData.hasString(ResumableUploadSpecJob.KEY_RESUME_SPEC)) {
        Log.d(TAG, "Using attachments V3");
        resumableUploadSpec = ResumableUploadSpec.deserialize(inputData.getString(ResumableUploadSpecJob.KEY_RESUME_SPEC));
    } else {
        Log.d(TAG, "Using attachments V2");
        resumableUploadSpec = null;
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    AttachmentDatabase database = SignalDatabase.attachments();
    DatabaseAttachment databaseAttachment = database.getAttachment(attachmentId);
    if (databaseAttachment == null) {
        throw new InvalidAttachmentException("Cannot find the specified attachment.");
    }
    long timeSinceUpload = System.currentTimeMillis() - databaseAttachment.getUploadTimestamp();
    if (timeSinceUpload < UPLOAD_REUSE_THRESHOLD && !TextUtils.isEmpty(databaseAttachment.getLocation())) {
        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");
    }
    Log.i(TAG, "Uploading attachment for message " + databaseAttachment.getMmsId() + " with ID " + databaseAttachment.getAttachmentId());
    try (NotificationController notification = getNotificationForAttachment(databaseAttachment)) {
        SignalServiceAttachment localAttachment = getAttachmentFor(databaseAttachment, notification, resumableUploadSpec);
        SignalServiceAttachmentPointer remoteAttachment = messageSender.uploadAttachment(localAttachment.asStream());
        Attachment attachment = PointerAttachment.forPointer(Optional.of(remoteAttachment), null, databaseAttachment.getFastPreflightId()).get();
        database.updateAttachmentAfterUpload(databaseAttachment.getAttachmentId(), attachment, remoteAttachment.getUploadTimestamp());
    } 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;
        }
    }
}
Also used : NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) Data(org.thoughtcrime.securesms.jobmanager.Data) PointerAttachment(org.thoughtcrime.securesms.attachments.PointerAttachment) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) NonSuccessfulResumableUploadResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResumableUploadResponseCodeException) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) ResumableUploadSpec(org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec) NotificationController(org.thoughtcrime.securesms.service.NotificationController)

Example 2 with ResumableUploadSpec

use of org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec in project Signal-Android by WhisperSystems.

the class SignalServiceMessageSender method getResumableUploadSpec.

public ResumableUploadSpec getResumableUploadSpec() throws IOException {
    long start = System.currentTimeMillis();
    AttachmentV3UploadAttributes v3UploadAttributes = null;
    Log.d(TAG, "Using pipe to retrieve attachment upload attributes...");
    try {
        v3UploadAttributes = new AttachmentService.AttachmentAttributesResponseProcessor<>(attachmentService.getAttachmentV3UploadAttributes().blockingGet()).getResultOrThrow();
    } catch (WebSocketUnavailableException e) {
        Log.w(TAG, "[getResumableUploadSpec] Pipe unavailable, falling back... (" + e.getClass().getSimpleName() + ": " + e.getMessage() + ")");
    } catch (IOException e) {
        Log.w(TAG, "Failed to retrieve attachment upload attributes using pipe. Falling back...");
    }
    long webSocket = System.currentTimeMillis() - start;
    if (v3UploadAttributes == null) {
        Log.d(TAG, "Not using pipe to retrieve attachment upload attributes...");
        v3UploadAttributes = socket.getAttachmentV3UploadAttributes();
    }
    long rest = System.currentTimeMillis() - start;
    ResumableUploadSpec spec = socket.getResumableUploadSpec(v3UploadAttributes);
    long end = System.currentTimeMillis() - start;
    Log.d(TAG, "[getResumableUploadSpec] webSocket: " + webSocket + " rest: " + rest + " end: " + end);
    return spec;
}
Also used : AttachmentV3UploadAttributes(org.whispersystems.signalservice.internal.push.AttachmentV3UploadAttributes) ResumableUploadSpec(org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec) WebSocketUnavailableException(org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException) IOException(java.io.IOException)

Example 3 with ResumableUploadSpec

use of org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec in project Signal-Android by WhisperSystems.

the class ResumableUploadSpecJob method onRun.

@Override
protected void onRun() throws Exception {
    ResumableUploadSpec resumableUploadSpec = ApplicationDependencies.getSignalServiceMessageSender().getResumableUploadSpec();
    setOutputData(new Data.Builder().putString(KEY_RESUME_SPEC, resumableUploadSpec.serialize()).build());
}
Also used : ResumableUploadSpec(org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec) Data(org.thoughtcrime.securesms.jobmanager.Data)

Aggregations

ResumableUploadSpec (org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec)3 Data (org.thoughtcrime.securesms.jobmanager.Data)2 IOException (java.io.IOException)1 Attachment (org.thoughtcrime.securesms.attachments.Attachment)1 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)1 PointerAttachment (org.thoughtcrime.securesms.attachments.PointerAttachment)1 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)1 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)1 NotificationController (org.thoughtcrime.securesms.service.NotificationController)1 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)1 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)1 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)1 NonSuccessfulResumableUploadResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResumableUploadResponseCodeException)1 WebSocketUnavailableException (org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException)1 AttachmentV3UploadAttributes (org.whispersystems.signalservice.internal.push.AttachmentV3UploadAttributes)1