@ -1,7 +1,9 @@
package org.session.libsession.messaging.jobs
package org.session.libsession.messaging.jobs
import okhttp3.HttpUrl
import org.session.libsession.messaging.MessagingConfiguration
import org.session.libsession.messaging.MessagingConfiguration
import org.session.libsession.messaging.fileserver.FileServerAPI
import org.session.libsession.messaging.fileserver.FileServerAPI
import org.session.libsession.messaging.opengroups.OpenGroupAPIV2
import org.session.libsession.messaging.sending_receiving.attachments.AttachmentState
import org.session.libsession.messaging.sending_receiving.attachments.AttachmentState
import org.session.libsession.messaging.utilities.DotNetAPI
import org.session.libsession.messaging.utilities.DotNetAPI
import org.session.libsignal.service.api.crypto.AttachmentCipherInputStream
import org.session.libsignal.service.api.crypto.AttachmentCipherInputStream
@ -10,7 +12,7 @@ import org.session.libsignal.utilities.logging.Log
import java.io.File
import java.io.File
import java.io.FileInputStream
import java.io.FileInputStream
class AttachmentDownloadJob ( val attachmentID : Long , val databaseMessageID : Long ) : Job {
class AttachmentDownloadJob ( val attachmentID : Long , val databaseMessageID : Long ) : Job {
override var delegate : JobDelegate ? = null
override var delegate : JobDelegate ? = null
override var id : String ? = null
override var id : String ? = null
@ -25,6 +27,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
// Settings
// Settings
override val maxFailureCount : Int = 20
override val maxFailureCount : Int = 20
companion object {
companion object {
val KEY : String = " AttachmentDownloadJob "
val KEY : String = " AttachmentDownloadJob "
@ -35,7 +38,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
override fun execute ( ) {
override fun execute ( ) {
val handleFailure : ( java . lang . Exception ) -> Unit = { exception ->
val handleFailure : ( java . lang . Exception ) -> Unit = { exception ->
if ( exception is Error && exception == Error . NoAttachment ) {
if ( exception is Error && exception == Error . NoAttachment ) {
MessagingConfiguration . shared . messageDataProvider . setAttachmentState ( AttachmentState . FAILED , attachmentID , databaseMessageID )
MessagingConfiguration . shared . messageDataProvider . setAttachmentState ( AttachmentState . FAILED , attachmentID , databaseMessageID )
this . handlePermanentFailure ( exception )
this . handlePermanentFailure ( exception )
} else if ( exception is DotNetAPI . Error && exception == DotNetAPI . Error . ParsingFailed ) {
} else if ( exception is DotNetAPI . Error && exception == DotNetAPI . Error . ParsingFailed ) {
@ -49,28 +52,31 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
}
}
try {
try {
val messageDataProvider = MessagingConfiguration . shared . messageDataProvider
val messageDataProvider = MessagingConfiguration . shared . messageDataProvider
val attachment = messageDataProvider . getDatabaseAttachment ( attachmentID ) ?: return handleFailure ( Error . NoAttachment )
val attachment = messageDataProvider . getDatabaseAttachment ( attachmentID )
?: return handleFailure ( Error . NoAttachment )
messageDataProvider . setAttachmentState ( AttachmentState . STARTED , attachmentID , this . databaseMessageID )
messageDataProvider . setAttachmentState ( AttachmentState . STARTED , attachmentID , this . databaseMessageID )
val tempFile = createTempFile ( )
val tempFile = createTempFile ( )
val threadId = MessagingConfiguration . shared . storage . getThreadIdForMms ( databaseMessageID )
val threadId = MessagingConfiguration . shared . storage . getThreadIdForMms ( databaseMessageID )
val openGroupV2 = MessagingConfiguration . shared . storage . getV2OpenGroup ( threadId . toString ( ) )
val openGroupV2 = MessagingConfiguration . shared . storage . getV2OpenGroup ( threadId . toString ( ) )
val isOpenGroupV2 = false
val stream = if ( openGroupV2 == null ) {
if ( !is OpenGroupV2 ) {
FileServerAPI . shared . downloadFile ( tempFile , attachment . url , MAX _ATTACHMENT _SIZE , null )
FileServerAPI . shared . downloadFile ( tempFile , attachment . url , MAX _ATTACHMENT _SIZE , null )
// DECRYPTION
// DECRYPTION
// Assume we're retrieving an attachment for an open group server if the digest is not set
// Assume we're retrieving an attachment for an open group server if the digest is not set
val stream = if ( attachment . digest ?. size ?: 0 == 0 || attachment . key . isNullOrEmpty ( ) ) FileInputStream ( tempFile )
if ( attachment . digest ?. size ?: 0 == 0 || attachment . key . isNullOrEmpty ( ) ) FileInputStream ( tempFile )
else AttachmentCipherInputStream . createForAttachment ( tempFile , attachment . size , Base64 . decode ( attachment . key ) , attachment . digest )
else AttachmentCipherInputStream . createForAttachment ( tempFile , attachment . size , Base64 . decode ( attachment . key ) , attachment . digest )
messageDataProvider . insertAttachment ( databaseMessageID , attachment . attachmentId , stream )
} else {
} else {
// val bytes = OpenGroupAPIV2.download()
val url = HttpUrl . parse ( attachment . url ) !!
val fileId = url . pathSegments ( ) . last ( )
OpenGroupAPIV2 . download ( fileId . toLong ( ) , openGroupV2 . room , openGroupV2 . server ) . get ( ) . let {
tempFile . writeBytes ( it )
}
FileInputStream ( tempFile )
}
}
messageDataProvider . insertAttachment ( databaseMessageID , attachment . attachmentId , stream )
tempFile . delete ( )
tempFile . delete ( )
handleSuccess ( )
handleSuccess ( )
} catch ( e : Exception ) {
} catch ( e : Exception ) {
@ -109,7 +115,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
return KEY
return KEY
}
}
class Factory : Job . Factory < AttachmentDownloadJob > {
class Factory : Job . Factory < AttachmentDownloadJob > {
override fun create ( data : Data ) : AttachmentDownloadJob {
override fun create ( data : Data ) : AttachmentDownloadJob {
return AttachmentDownloadJob ( data . getLong ( KEY _ATTACHMENT _ID ) , data . getLong ( KEY _TS _INCOMING _MESSAGE _ID ) )
return AttachmentDownloadJob ( data . getLong ( KEY _ATTACHMENT _ID ) , data . getLong ( KEY _TS _INCOMING _MESSAGE _ID ) )
}
}