|
|
|
@ -2,27 +2,35 @@ package org.thoughtcrime.securesms.attachments
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import com.google.protobuf.ByteString
|
|
|
|
|
import org.session.libsession.database.dto.DatabaseAttachmentDTO
|
|
|
|
|
import org.session.libsession.database.MessageDataProvider
|
|
|
|
|
import org.session.libsession.database.dto.AttachmentState
|
|
|
|
|
import org.session.libsignal.service.internal.push.SignalServiceProtos
|
|
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.SessionServiceAttachmentPointer
|
|
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.SessionServiceAttachmentStream
|
|
|
|
|
import org.session.libsignal.libsignal.util.guava.Optional
|
|
|
|
|
import org.thoughtcrime.securesms.database.Database
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
|
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase
|
|
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
|
|
|
|
import org.thoughtcrime.securesms.jobs.AttachmentUploadJob
|
|
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority
|
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil
|
|
|
|
|
|
|
|
|
|
class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), MessageDataProvider {
|
|
|
|
|
override fun getAttachment(attachmentId: Long): DatabaseAttachmentDTO? {
|
|
|
|
|
|
|
|
|
|
override fun getAttachmentStream(attachmentId: Long): SessionServiceAttachmentStream? {
|
|
|
|
|
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
|
|
|
|
|
val databaseAttachment = attachmentDatabase.getAttachment(AttachmentId(attachmentId, 0)) ?: return null
|
|
|
|
|
return databaseAttachment.toAttachmentStream(context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getAttachmentPointer(attachmentId: Long): SessionServiceAttachmentPointer? {
|
|
|
|
|
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
|
|
|
|
|
val databaseAttachment = attachmentDatabase.getAttachment(AttachmentId(attachmentId, 0)) ?: return null
|
|
|
|
|
return databaseAttachment.toDTO()
|
|
|
|
|
return databaseAttachment.toAttachmentPointer()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun setAttachmentState(attachmentState: AttachmentState, attachment: DatabaseAttachmentDTO, messageID: Long) {
|
|
|
|
|
override fun setAttachmentState(attachmentState: AttachmentState, attachmentId: Long, messageID: Long) {
|
|
|
|
|
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
|
|
|
|
|
attachmentDatabase.setTransferState(messageID, AttachmentId(attachment.attachmentId, 0), attachmentState.value)
|
|
|
|
|
attachmentDatabase.setTransferState(messageID, AttachmentId(attachmentId, 0), attachmentState.value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@ -38,29 +46,26 @@ class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extension to DatabaseAttachment class
|
|
|
|
|
|
|
|
|
|
fun DatabaseAttachment.toDTO(): DatabaseAttachmentDTO {
|
|
|
|
|
var databaseAttachmentDTO = DatabaseAttachmentDTO()
|
|
|
|
|
databaseAttachmentDTO.attachmentId = this.attachmentId.rowId
|
|
|
|
|
databaseAttachmentDTO.contentType = this.contentType
|
|
|
|
|
databaseAttachmentDTO.fileName = this.fileName
|
|
|
|
|
databaseAttachmentDTO.caption = this.caption
|
|
|
|
|
fun DatabaseAttachment.toAttachmentPointer(): SessionServiceAttachmentPointer {
|
|
|
|
|
return SessionServiceAttachmentPointer(attachmentId.rowId, contentType, key?.toByteArray(), Optional.fromNullable(size.toInt()), Optional.absent(), width, height, Optional.fromNullable(digest), Optional.fromNullable(fileName), isVoiceNote, Optional.fromNullable(caption), url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
databaseAttachmentDTO.size = this.size.toInt()
|
|
|
|
|
databaseAttachmentDTO.key = ByteString.copyFrom(this.key?.toByteArray())
|
|
|
|
|
databaseAttachmentDTO.digest = ByteString.copyFrom(this.digest)
|
|
|
|
|
databaseAttachmentDTO.flags = if (this.isVoiceNote) SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE.number else 0
|
|
|
|
|
fun DatabaseAttachment.toAttachmentStream(context: Context): SessionServiceAttachmentStream {
|
|
|
|
|
val stream = PartAuthority.getAttachmentStream(context, this.dataUri!!)
|
|
|
|
|
var attachmentStream = SessionServiceAttachmentStream(stream, this.contentType, this.size, Optional.fromNullable(this.fileName), this.isVoiceNote, Optional.absent(), this.width, this.height, Optional.fromNullable(this.caption), null)
|
|
|
|
|
attachmentStream.attachmentId = this.attachmentId.rowId
|
|
|
|
|
attachmentStream.isAudio = MediaUtil.isAudio(this)
|
|
|
|
|
attachmentStream.isGif = MediaUtil.isGif(this)
|
|
|
|
|
attachmentStream.isVideo = MediaUtil.isVideo(this)
|
|
|
|
|
attachmentStream.isImage = MediaUtil.isImage(this)
|
|
|
|
|
|
|
|
|
|
databaseAttachmentDTO.url = this.url
|
|
|
|
|
attachmentStream.key = ByteString.copyFrom(this.key?.toByteArray())
|
|
|
|
|
attachmentStream.digest = this.digest
|
|
|
|
|
//attachmentStream.flags = if (this.isVoiceNote) SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE.number else 0
|
|
|
|
|
|
|
|
|
|
if (this.shouldHaveImageSize()) {
|
|
|
|
|
databaseAttachmentDTO.shouldHaveImageSize = true
|
|
|
|
|
databaseAttachmentDTO.width = this.width
|
|
|
|
|
databaseAttachmentDTO.height = this.height
|
|
|
|
|
}
|
|
|
|
|
attachmentStream.url = this.url
|
|
|
|
|
|
|
|
|
|
return databaseAttachmentDTO
|
|
|
|
|
return attachmentStream
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun DatabaseAttachment.shouldHaveImageSize(): Boolean {
|
|
|
|
|