@@ -7,7 +7,6 @@ import com.google.protobuf.ByteString
77import org.session.libsession.messaging.sending_receiving.attachments.PointerAttachment
88import org.session.libsignal.messages.SignalServiceAttachmentPointer
99import org.session.protos.SessionProtos
10- import org.session.libsignal.utilities.guava.Optional
1110import java.io.File
1211import org.session.libsession.messaging.sending_receiving.attachments.Attachment as SignalAttachment
1312
@@ -28,69 +27,63 @@ class Attachment {
2827 val result = Attachment ()
2928
3029 // Note: For legacy Session Android clients this filename will be null and we'll synthesise an appropriate filename
31- // further down the stack
30+ // further down the stack in the Storage.persist
3231 result.filename = proto.fileName
3332
3433 fun inferContentType (): String {
35- val fileName = result.filename
34+ val fileName = result.filename.orEmpty()
3635 val fileExtension = File (fileName).extension
3736 val mimeTypeMap = MimeTypeMap .getSingleton()
3837 return mimeTypeMap.getMimeTypeFromExtension(fileExtension) ? : " application/octet-stream"
3938 }
4039 result.contentType = proto.contentType ? : inferContentType()
4140
42- // If we were given a null filename from a legacy client but we at least have a content type (i.e., mime type)
43- // then the best we can do is synthesise a filename based on the content type and when we received the file.
44- if (result.filename.isNullOrEmpty() && ! result.contentType.isNullOrEmpty()) {
45- Log .d(" " , " *** GOT an empty filename" )
46- // result.filename = generateFilenameFromReceivedTypeForLegacyClients(result.contentType!!)
47- // todo: Found this part with the code commented out... This 'if' is now doing nothing at all, is that normal? Shouldn't we indeed set a filename here or is that handled further down the line? (can't explore this now so I'm leaving a todo)
48- }
49-
5041 result.key = proto.key.toByteArray()
5142 result.digest = proto.digest.toByteArray()
52- val kind: Kind = if (proto.hasFlags() && proto.flags.and (SessionProtos .AttachmentPointer .Flags .VOICE_MESSAGE_VALUE ) > 0 ) {
53- Kind .VOICE_MESSAGE
54- } else {
55- Kind .GENERIC
56- }
57- result.kind = kind
43+
44+ result.kind =
45+ if (proto.hasFlags() && (proto.flags and SessionProtos .AttachmentPointer .Flags .VOICE_MESSAGE_VALUE ) > 0 ) {
46+ Kind .VOICE_MESSAGE
47+ } else {
48+ Kind .GENERIC
49+ }
50+
5851 result.caption = if (proto.hasCaption()) proto.caption else null
59- val size: Size = if (proto.hasWidth() && proto.width > 0 && proto.hasHeight() && proto.height > 0 ) {
60- Size (proto.width, proto.height)
61- } else {
62- Size (0 ,0 )
63- }
64- result.size = size
52+
53+ result.size =
54+ if (proto.hasWidth() && proto.width > 0 && proto.hasHeight() && proto.height > 0 ) {
55+ Size (proto.width, proto.height)
56+ } else {
57+ Size (0 , 0 )
58+ }
59+
6560 result.sizeInBytes = if (proto.size > 0 ) proto.size else null
6661 result.url = proto.url
6762
6863 return result
6964 }
7065
71- fun createAttachmentPointer (attachment : SignalServiceAttachmentPointer ): SessionProtos .AttachmentPointer ? {
66+ fun createAttachmentPointer (attachment : SignalServiceAttachmentPointer ): SessionProtos .AttachmentPointer {
7267 val builder = SessionProtos .AttachmentPointer .newBuilder()
73- .setContentType(attachment.contentType)
74- .setId(attachment.id.toString().toLongOrNull() ? : 0L )
75- .setKey(ByteString .copyFrom(attachment.key))
76- .setSize(attachment.size.get())
77- .setUrl(attachment.url)
78-
79- if (attachment.digest.isPresent) {
80- builder.setDigest(ByteString .copyFrom(attachment.digest.get()))
81- }
82-
68+ .setContentType(attachment.contentType)
69+ .setId(attachment.id)
70+ .setKey(ByteString .copyFrom(attachment.key))
71+ .setSize(attachment.size ? : 0 )
72+ .setUrl(attachment.url)
73+
74+ attachment.digest?.let { builder.setDigest(ByteString .copyFrom(it)) }
75+
8376 // Filenames are now mandatory for picked/shared files, Giphy GIFs, and captured photos.
8477 // The images associated with LinkPreviews don't have a "given name" so we'll use the
8578 // attachment ID as the filename. It's not possible to save these preview images or see
8679 // the filename, so what the filename IS isn't important, only that a filename exists.
8780 builder.fileName = attachment.filename ? : attachment.id.toString()
8881
89- if ( attachment.preview.isPresent) { builder.thumbnail = ByteString .copyFrom(attachment.preview.get()) }
90- if (attachment.width > 0 ) { builder.width = attachment.width }
91- if (attachment.height > 0 ) { builder.height = attachment.height }
92- if (attachment.voiceNote) { builder.flags = SessionProtos .AttachmentPointer .Flags .VOICE_MESSAGE_VALUE }
93- if ( attachment.caption.isPresent) { builder.caption = attachment.caption.get() }
82+ attachment.preview?. let { builder.thumbnail = ByteString .copyFrom(it) }
83+ if (attachment.width > 0 ) builder.width = attachment.width
84+ if (attachment.height > 0 ) builder.height = attachment.height
85+ if (attachment.voiceNote) builder.flags = SessionProtos .AttachmentPointer .Flags .VOICE_MESSAGE_VALUE
86+ attachment.caption?. let { builder.caption = it }
9487
9588 return builder.build()
9689 }
@@ -112,14 +105,31 @@ class Attachment {
112105
113106 fun toSignalAttachment (): SignalAttachment ? {
114107 if (! isValid()) return null
115- return PointerAttachment .forAttachment(( this ) )
108+ return PointerAttachment .forAttachment(this )
116109 }
117110
118111 fun toSignalPointer (): SignalServiceAttachmentPointer ? {
119112 if (! isValid()) return null
120- return SignalServiceAttachmentPointer (0 , contentType, key, Optional .fromNullable(sizeInBytes), null ,
121- size?.width ? : 0 , size?.height ? : 0 , Optional .fromNullable(digest), filename,
122- kind == Kind .VOICE_MESSAGE , Optional .fromNullable(caption), url)
123- }
124113
125- }
114+ // safe because isValid() checked them
115+ val ct = contentType!!
116+ val k = key!!
117+ val u = url!!
118+ val file = filename
119+
120+ return SignalServiceAttachmentPointer (
121+ id = 0L ,
122+ contentType = ct,
123+ key = k,
124+ size = sizeInBytes,
125+ preview = null ,
126+ width = size?.width ? : 0 ,
127+ height = size?.height ? : 0 ,
128+ digest = digest,
129+ filename = file ? : " " ,
130+ voiceNote = (kind == Kind .VOICE_MESSAGE ),
131+ caption = caption,
132+ url = u
133+ )
134+ }
135+ }
0 commit comments