Search in sources :

Example 51 with MmsException

use of org.thoughtcrime.securesms.mms.MmsException in project Signal-Android by signalapp.

the class AttachmentDatabase method copyAttachmentData.

public void copyAttachmentData(@NonNull AttachmentId sourceId, @NonNull AttachmentId destinationId) throws MmsException {
    DatabaseAttachment sourceAttachment = getAttachment(sourceId);
    if (sourceAttachment == null) {
        throw new MmsException("Cannot find attachment for source!");
    }
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    DataInfo sourceDataInfo = getAttachmentDataFileInfo(sourceId, DATA);
    if (sourceDataInfo == null) {
        throw new MmsException("No attachment data found for source!");
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(DATA, sourceDataInfo.file.getAbsolutePath());
    contentValues.put(DATA_HASH, sourceDataInfo.hash);
    contentValues.put(SIZE, sourceDataInfo.length);
    contentValues.put(DATA_RANDOM, sourceDataInfo.random);
    contentValues.put(TRANSFER_STATE, sourceAttachment.getTransferState());
    contentValues.put(CDN_NUMBER, sourceAttachment.getCdnNumber());
    contentValues.put(CONTENT_LOCATION, sourceAttachment.getLocation());
    contentValues.put(DIGEST, sourceAttachment.getDigest());
    contentValues.put(CONTENT_DISPOSITION, sourceAttachment.getKey());
    contentValues.put(NAME, sourceAttachment.getRelay());
    contentValues.put(SIZE, sourceAttachment.getSize());
    contentValues.put(FAST_PREFLIGHT_ID, sourceAttachment.getFastPreflightId());
    contentValues.put(WIDTH, sourceAttachment.getWidth());
    contentValues.put(HEIGHT, sourceAttachment.getHeight());
    contentValues.put(CONTENT_TYPE, sourceAttachment.getContentType());
    contentValues.put(VISUAL_HASH, getVisualHashStringOrNull(sourceAttachment));
    database.update(TABLE_NAME, contentValues, PART_ID_WHERE, destinationId.toStrings());
}
Also used : ContentValues(android.content.ContentValues) MmsException(org.thoughtcrime.securesms.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment)

Example 52 with MmsException

use of org.thoughtcrime.securesms.mms.MmsException in project Signal-Android by signalapp.

the class AttachmentDatabase method setAttachmentData.

@NonNull
private DataInfo setAttachmentData(@NonNull File destination, @NonNull InputStream in, @Nullable AttachmentId attachmentId) throws MmsException {
    try {
        File tempFile = newFile();
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        DigestInputStream digestInputStream = new DigestInputStream(in, messageDigest);
        Pair<byte[], OutputStream> out = ModernEncryptingPartOutputStream.createFor(attachmentSecret, tempFile, false);
        long length = StreamUtil.copy(digestInputStream, out.second);
        String hash = Base64.encodeBytes(digestInputStream.getMessageDigest().digest());
        if (!tempFile.renameTo(destination)) {
            Log.w(TAG, "Couldn't rename " + tempFile.getPath() + " to " + destination.getPath());
            tempFile.delete();
            throw new IllegalStateException("Couldn't rename " + tempFile.getPath() + " to " + destination.getPath());
        }
        SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
        Optional<DataInfo> sharedDataInfo = findDuplicateDataFileInfo(database, hash, attachmentId);
        if (sharedDataInfo.isPresent()) {
            Log.i(TAG, "[setAttachmentData] Duplicate data file found! " + sharedDataInfo.get().file.getAbsolutePath());
            if (!destination.equals(sharedDataInfo.get().file) && destination.delete()) {
                Log.i(TAG, "[setAttachmentData] Deleted original file. " + destination);
            }
            return sharedDataInfo.get();
        } else {
            Log.i(TAG, "[setAttachmentData] No matching attachment data found. " + destination.getAbsolutePath());
        }
        return new DataInfo(destination, length, out.first, hash);
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new MmsException(e);
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) ModernEncryptingPartOutputStream(org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MmsException(org.thoughtcrime.securesms.mms.MmsException) MessageDigest(java.security.MessageDigest) File(java.io.File) NonNull(androidx.annotation.NonNull)

Example 53 with MmsException

use of org.thoughtcrime.securesms.mms.MmsException in project Signal-Android by signalapp.

the class AttachmentDatabase method insertAttachmentForPreUpload.

@NonNull
public DatabaseAttachment insertAttachmentForPreUpload(@NonNull Attachment attachment) throws MmsException {
    Map<Attachment, AttachmentId> result = insertAttachmentsForMessage(PREUPLOAD_MESSAGE_ID, Collections.singletonList(attachment), Collections.emptyList());
    if (result.values().isEmpty()) {
        throw new MmsException("Bad attachment result!");
    }
    DatabaseAttachment databaseAttachment = getAttachment(result.values().iterator().next());
    if (databaseAttachment == null) {
        throw new MmsException("Failed to retrieve attachment we just inserted!");
    }
    return databaseAttachment;
}
Also used : MmsException(org.thoughtcrime.securesms.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId) NonNull(androidx.annotation.NonNull)

Example 54 with MmsException

use of org.thoughtcrime.securesms.mms.MmsException in project Signal-Android by signalapp.

the class MessageSender method preUploadPushAttachment.

/**
 * @return A result if the attachment was enqueued, or null if it failed to enqueue or shouldn't
 *         be enqueued (like in the case of a local self-send).
 */
@Nullable
public static PreUploadResult preUploadPushAttachment(@NonNull Context context, @NonNull Attachment attachment, @Nullable Recipient recipient) {
    if (isLocalSelfSend(context, recipient, false)) {
        return null;
    }
    Log.i(TAG, "Pre-uploading attachment for " + (recipient != null ? recipient.getId() : "null"));
    try {
        AttachmentDatabase attachmentDatabase = SignalDatabase.attachments();
        DatabaseAttachment databaseAttachment = attachmentDatabase.insertAttachmentForPreUpload(attachment);
        Job compressionJob = AttachmentCompressionJob.fromAttachment(databaseAttachment, false, -1);
        Job resumableUploadSpecJob = new ResumableUploadSpecJob();
        Job uploadJob = new AttachmentUploadJob(databaseAttachment.getAttachmentId());
        ApplicationDependencies.getJobManager().startChain(compressionJob).then(resumableUploadSpecJob).then(uploadJob).enqueue();
        return new PreUploadResult(databaseAttachment.getAttachmentId(), Arrays.asList(compressionJob.getId(), resumableUploadSpecJob.getId(), uploadJob.getId()));
    } catch (MmsException e) {
        Log.w(TAG, "preUploadPushAttachment() - Failed to upload!", e);
        return null;
    }
}
Also used : MmsException(org.thoughtcrime.securesms.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) ResumableUploadSpecJob(org.thoughtcrime.securesms.jobs.ResumableUploadSpecJob) AttachmentMarkUploadedJob(org.thoughtcrime.securesms.jobs.AttachmentMarkUploadedJob) ReactionSendJob(org.thoughtcrime.securesms.jobs.ReactionSendJob) PushMediaSendJob(org.thoughtcrime.securesms.jobs.PushMediaSendJob) Job(org.thoughtcrime.securesms.jobmanager.Job) ResumableUploadSpecJob(org.thoughtcrime.securesms.jobs.ResumableUploadSpecJob) PushGroupSendJob(org.thoughtcrime.securesms.jobs.PushGroupSendJob) AttachmentCompressionJob(org.thoughtcrime.securesms.jobs.AttachmentCompressionJob) ProfileKeySendJob(org.thoughtcrime.securesms.jobs.ProfileKeySendJob) RemoteDeleteSendJob(org.thoughtcrime.securesms.jobs.RemoteDeleteSendJob) MmsSendJob(org.thoughtcrime.securesms.jobs.MmsSendJob) AttachmentUploadJob(org.thoughtcrime.securesms.jobs.AttachmentUploadJob) PushTextSendJob(org.thoughtcrime.securesms.jobs.PushTextSendJob) AttachmentCopyJob(org.thoughtcrime.securesms.jobs.AttachmentCopyJob) SmsSendJob(org.thoughtcrime.securesms.jobs.SmsSendJob) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) AttachmentUploadJob(org.thoughtcrime.securesms.jobs.AttachmentUploadJob) Nullable(androidx.annotation.Nullable)

Example 55 with MmsException

use of org.thoughtcrime.securesms.mms.MmsException in project Signal-Android by signalapp.

the class AttachmentCompressionJob method compress.

private void compress(@NonNull AttachmentDatabase attachmentDatabase, @NonNull MediaConstraints constraints, @NonNull DatabaseAttachment attachment) throws UndeliverableMessageException {
    try {
        if (attachment.isSticker()) {
            Log.d(TAG, "Sticker, not compressing.");
        } else if (MediaUtil.isVideo(attachment)) {
            Log.i(TAG, "Compressing video.");
            attachment = transcodeVideoIfNeededToDatabase(context, attachmentDatabase, attachment, constraints, EventBus.getDefault(), this::isCanceled);
            if (!constraints.isSatisfied(context, attachment)) {
                throw new UndeliverableMessageException("Size constraints could not be met on video!");
            }
        } else if (constraints.canResize(attachment)) {
            Log.i(TAG, "Compressing image.");
            MediaStream converted = compressImage(context, attachment, constraints);
            attachmentDatabase.updateAttachmentData(attachment, converted, false);
            attachmentDatabase.markAttachmentAsTransformed(attachmentId);
        } else if (constraints.isSatisfied(context, attachment)) {
            Log.i(TAG, "Not compressing.");
            attachmentDatabase.markAttachmentAsTransformed(attachmentId);
        } else {
            throw new UndeliverableMessageException("Size constraints could not be met!");
        }
    } catch (IOException | MmsException e) {
        throw new UndeliverableMessageException(e);
    }
}
Also used : MmsException(org.thoughtcrime.securesms.mms.MmsException) MediaStream(org.thoughtcrime.securesms.mms.MediaStream) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) IOException(java.io.IOException)

Aggregations

MmsException (org.thoughtcrime.securesms.mms.MmsException)61 IOException (java.io.IOException)26 Recipient (org.thoughtcrime.securesms.recipients.Recipient)26 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)24 NonNull (androidx.annotation.NonNull)20 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)20 OutgoingMediaMessage (org.thoughtcrime.securesms.mms.OutgoingMediaMessage)19 Nullable (androidx.annotation.Nullable)18 Attachment (org.thoughtcrime.securesms.attachments.Attachment)18 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)15 List (java.util.List)14 MessageId (org.thoughtcrime.securesms.database.model.MessageId)14 Context (android.content.Context)12 WorkerThread (androidx.annotation.WorkerThread)12 Stream (com.annimon.stream.Stream)12 LinkedList (java.util.LinkedList)12 Log (org.signal.core.util.logging.Log)12 AttachmentId (org.thoughtcrime.securesms.attachments.AttachmentId)12 NoSuchMessageException (org.thoughtcrime.securesms.database.NoSuchMessageException)12 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)12