Merge branch 'refactor' of https://github.com/RyanRory/loki-messenger-android into refactor
commit
e2e78ce5fb
@ -0,0 +1,52 @@
|
|||||||
|
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.libsignal.service.internal.push.SignalServiceProtos
|
||||||
|
import org.thoughtcrime.securesms.database.Database
|
||||||
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
||||||
|
import org.thoughtcrime.securesms.util.MediaUtil
|
||||||
|
|
||||||
|
class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), MessageDataProvider {
|
||||||
|
override fun getAttachment(uniqueID: String): DatabaseAttachmentDTO? {
|
||||||
|
|
||||||
|
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
|
||||||
|
val uniqueID = uniqueID.toLongOrNull() ?: return null
|
||||||
|
val attachmentID = AttachmentId(0, uniqueID)
|
||||||
|
val databaseAttachment = attachmentDatabase.getAttachment(attachmentID) ?: return null
|
||||||
|
|
||||||
|
return databaseAttachment.toDTO()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extension to DatabaseAttachment class
|
||||||
|
|
||||||
|
fun DatabaseAttachment.toDTO(): DatabaseAttachmentDTO {
|
||||||
|
var databaseAttachmentDTO = DatabaseAttachmentDTO()
|
||||||
|
databaseAttachmentDTO.contentType = this.contentType
|
||||||
|
databaseAttachmentDTO.fileName = this.fileName
|
||||||
|
databaseAttachmentDTO.caption = this.caption
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
databaseAttachmentDTO.url = this.url
|
||||||
|
|
||||||
|
if (this.shouldHaveImageSize()) {
|
||||||
|
databaseAttachmentDTO.shouldHaveImageSize = true
|
||||||
|
databaseAttachmentDTO.width = this.width
|
||||||
|
databaseAttachmentDTO.height = this.height
|
||||||
|
}
|
||||||
|
|
||||||
|
return databaseAttachmentDTO
|
||||||
|
}
|
||||||
|
|
||||||
|
fun DatabaseAttachment.shouldHaveImageSize(): Boolean {
|
||||||
|
return (MediaUtil.isVideo(this) || MediaUtil.isImage(this) || MediaUtil.isGif(this));
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package org.session.libsession.database
|
||||||
|
|
||||||
|
import org.session.libsession.database.dto.DatabaseAttachmentDTO
|
||||||
|
|
||||||
|
interface MessageDataProvider {
|
||||||
|
|
||||||
|
fun getAttachment(uniqueID: String): DatabaseAttachmentDTO?
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
package org.session.libsession.database.dto
|
||||||
|
|
||||||
|
import android.util.Size
|
||||||
|
import com.google.protobuf.ByteString
|
||||||
|
import org.session.libsignal.service.internal.push.SignalServiceProtos
|
||||||
|
import kotlin.math.round
|
||||||
|
|
||||||
|
class DatabaseAttachmentDTO {
|
||||||
|
var contentType: String? = null
|
||||||
|
|
||||||
|
var fileName: String? = null
|
||||||
|
|
||||||
|
var url: String? = null
|
||||||
|
|
||||||
|
var caption: String? = null
|
||||||
|
|
||||||
|
var size: Int = 0
|
||||||
|
|
||||||
|
var key: ByteString? = null
|
||||||
|
|
||||||
|
var digest: ByteString? = null
|
||||||
|
|
||||||
|
var flags: Int = 0
|
||||||
|
|
||||||
|
var width: Int = 0
|
||||||
|
|
||||||
|
var height: Int = 0
|
||||||
|
|
||||||
|
val isVoiceNote: Boolean = false
|
||||||
|
|
||||||
|
var shouldHaveImageSize: Boolean = false
|
||||||
|
|
||||||
|
val isUploaded: Boolean = false
|
||||||
|
|
||||||
|
fun toProto(): SignalServiceProtos.AttachmentPointer? {
|
||||||
|
val builder = org.session.libsignal.service.internal.push.SignalServiceProtos.AttachmentPointer.newBuilder()
|
||||||
|
builder.contentType = this.contentType
|
||||||
|
|
||||||
|
if (!this.fileName.isNullOrEmpty()) {
|
||||||
|
builder.fileName = this.fileName
|
||||||
|
}
|
||||||
|
if (!this.caption.isNullOrEmpty()) {
|
||||||
|
builder.caption = this.caption
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.size = this.size
|
||||||
|
builder.key = this.key
|
||||||
|
builder.digest = this.digest
|
||||||
|
builder.flags = if (this.isVoiceNote) org.session.libsignal.service.internal.push.SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE.number else 0
|
||||||
|
|
||||||
|
//TODO I did copy the behavior of iOS below, not sure if that's relevant here...
|
||||||
|
if (this.shouldHaveImageSize) {
|
||||||
|
if (this.width < kotlin.Int.MAX_VALUE && this.height < kotlin.Int.MAX_VALUE) {
|
||||||
|
val imageSize: Size = Size(this.width, this.height)
|
||||||
|
val imageWidth = round(imageSize.width.toDouble())
|
||||||
|
val imageHeight = round(imageSize.height.toDouble())
|
||||||
|
if (imageWidth > 0 && imageHeight > 0) {
|
||||||
|
builder.width = imageWidth.toInt()
|
||||||
|
builder.height = imageHeight.toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.url = this.url
|
||||||
|
|
||||||
|
try {
|
||||||
|
return builder.build()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue