Search in sources :

Example 1 with AttachmentDatabase

use of org.thoughtcrime.securesms.database.AttachmentDatabase in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method retrieveAttachment.

private void retrieveAttachment(MasterSecret masterSecret, long messageId, final AttachmentId attachmentId, final Attachment attachment) throws IOException {
    AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
    File attachmentFile = null;
    try {
        attachmentFile = createTempFile();
        SignalServiceAttachmentPointer pointer = createAttachmentPointer(masterSecret, attachment);
        InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, MAX_ATTACHMENT_SIZE, new ProgressListener() {

            @Override
            public void onAttachmentProgress(long total, long progress) {
                EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress));
            }
        });
        database.insertAttachmentsForPlaceholder(masterSecret, messageId, attachmentId, stream);
    } catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException e) {
        Log.w(TAG, e);
        markFailed(messageId, attachmentId);
    } finally {
        if (attachmentFile != null)
            attachmentFile.delete();
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStream(java.io.InputStream) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) PartProgressEvent(org.thoughtcrime.securesms.events.PartProgressEvent) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) MmsException(ws.com.google.android.mms.MmsException) ProgressListener(org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener) File(java.io.File)

Example 2 with AttachmentDatabase

use of org.thoughtcrime.securesms.database.AttachmentDatabase in project Signal-Android by signalapp.

the class SendJob method scaleAttachments.

protected List<Attachment> scaleAttachments(@NonNull MediaConstraints constraints, @NonNull List<Attachment> attachments) throws UndeliverableMessageException {
    AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
    List<Attachment> results = new LinkedList<>();
    for (Attachment attachment : attachments) {
        try {
            if (constraints.isSatisfied(context, attachment)) {
                results.add(attachment);
            } else if (constraints.canResize(attachment)) {
                MediaStream resized = constraints.getResizedMedia(context, attachment);
                results.add(attachmentDatabase.updateAttachmentData(attachment, resized));
            } else {
                throw new UndeliverableMessageException("Size constraints could not be met!");
            }
        } catch (IOException | MmsException e) {
            throw new UndeliverableMessageException(e);
        }
    }
    return results;
}
Also used : MmsException(org.thoughtcrime.securesms.mms.MmsException) MediaStream(org.thoughtcrime.securesms.mms.MediaStream) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) Attachment(org.thoughtcrime.securesms.attachments.Attachment) IOException(java.io.IOException) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) LinkedList(java.util.LinkedList)

Example 3 with AttachmentDatabase

use of org.thoughtcrime.securesms.database.AttachmentDatabase in project Signal-Android by signalapp.

the class PartDataSource method open.

@Override
public long open(DataSpec dataSpec) throws IOException {
    this.uri = dataSpec.uri;
    AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
    PartUriParser partUri = new PartUriParser(uri);
    Attachment attachment = attachmentDatabase.getAttachment(partUri.getPartId());
    if (attachment == null)
        throw new IOException("Attachment not found");
    this.inputSteam = attachmentDatabase.getAttachmentStream(partUri.getPartId(), dataSpec.position);
    if (listener != null) {
        listener.onTransferStart(this, dataSpec);
    }
    if (attachment.getSize() - dataSpec.position <= 0)
        throw new EOFException("No more data");
    return attachment.getSize() - dataSpec.position;
}
Also used : PartUriParser(org.thoughtcrime.securesms.mms.PartUriParser) EOFException(java.io.EOFException) Attachment(org.thoughtcrime.securesms.attachments.Attachment) IOException(java.io.IOException) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase)

Example 4 with AttachmentDatabase

use of org.thoughtcrime.securesms.database.AttachmentDatabase in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method doWork.

public void doWork() throws IOException, RetryLaterException {
    Log.i(TAG, "onRun() messageId: " + messageId + "  partRowId: " + partRowId + "  partUniqueId: " + partUniqueId + "  manual: " + manual);
    final AttachmentDatabase database = SignalDatabase.attachments();
    final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
    final DatabaseAttachment attachment = database.getAttachment(attachmentId);
    if (attachment == null) {
        Log.w(TAG, "attachment no longer exists.");
        return;
    }
    if (!attachment.isInProgress()) {
        Log.w(TAG, "Attachment was already downloaded.");
        return;
    }
    if (!manual && !AttachmentUtil.isAutoDownloadPermitted(context, attachment)) {
        Log.w(TAG, "Attachment can't be auto downloaded...");
        database.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_PENDING);
        return;
    }
    Log.i(TAG, "Downloading push part " + attachmentId);
    database.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_STARTED);
    if (attachment.getCdnNumber() != ReleaseChannel.CDN_NUMBER) {
        retrieveAttachment(messageId, attachmentId, attachment);
    } else {
        retrieveUrlAttachment(messageId, attachmentId, attachment);
    }
}
Also used : DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId)

Example 5 with AttachmentDatabase

use of org.thoughtcrime.securesms.database.AttachmentDatabase in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method markFailed.

private void markFailed(long messageId, AttachmentId attachmentId) {
    try {
        AttachmentDatabase database = SignalDatabase.attachments();
        database.setTransferProgressFailed(attachmentId, messageId);
    } catch (MmsException e) {
        Log.w(TAG, e);
    }
}
Also used : MmsException(org.thoughtcrime.securesms.mms.MmsException) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase)

Aggregations

AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)23 Attachment (org.thoughtcrime.securesms.attachments.Attachment)10 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)10 MmsException (org.thoughtcrime.securesms.mms.MmsException)9 IOException (java.io.IOException)8 AttachmentId (org.thoughtcrime.securesms.attachments.AttachmentId)6 File (java.io.File)4 LinkedList (java.util.LinkedList)4 PartProgressEvent (org.thoughtcrime.securesms.events.PartProgressEvent)4 UndeliverableMessageException (org.thoughtcrime.securesms.transport.UndeliverableMessageException)4 NonNull (androidx.annotation.NonNull)3 InputStream (java.io.InputStream)3 Job (org.thoughtcrime.securesms.jobmanager.Job)3 MediaStream (org.thoughtcrime.securesms.mms.MediaStream)3 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)3 Nullable (androidx.annotation.Nullable)2 Stream (com.annimon.stream.Stream)2 EOFException (java.io.EOFException)2 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)2 AttachmentCompressionJob (org.thoughtcrime.securesms.jobs.AttachmentCompressionJob)2