WebBlobResponse.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. class WebBlobResponse: WebAbstractMessage {
  22. var id: String
  23. var messageId: String
  24. var type: String
  25. var baseMessage: BaseMessage
  26. init(request: WebBlobRequest, imageMessage: ImageMessage) {
  27. id = request.id
  28. type = request.type
  29. messageId = request.messageId.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
  30. baseMessage = imageMessage
  31. let tmpArgs:[AnyHashable:Any?] = ["id": id, "messageId": messageId, "type": type]
  32. let tmpAck = WebAbstractMessageAcknowledgement.init(request.requestId, false, nil)
  33. super.init(messageType: "response", messageSubType: "blob", requestId: nil, ack: tmpAck, args: tmpArgs, data: nil)
  34. }
  35. init(request: WebBlobRequest, videoMessage: VideoMessage) {
  36. id = request.id
  37. type = request.type
  38. messageId = request.messageId.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
  39. baseMessage = videoMessage
  40. let tmpArgs:[AnyHashable:Any?] = ["id": id, "messageId": messageId, "type": type]
  41. let tmpAck = WebAbstractMessageAcknowledgement.init(request.requestId, false, nil)
  42. super.init(messageType: "response", messageSubType: "blob", requestId: nil, ack: tmpAck, args: tmpArgs, data: nil)
  43. }
  44. init(request: WebBlobRequest, audioMessage: AudioMessage) {
  45. id = request.id
  46. type = request.type
  47. messageId = request.messageId.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
  48. baseMessage = audioMessage
  49. let tmpArgs:[AnyHashable:Any?] = ["id": id, "messageId": messageId, "type": type]
  50. let tmpAck = WebAbstractMessageAcknowledgement.init(request.requestId, false, nil)
  51. super.init(messageType: "response", messageSubType: "blob", requestId: nil, ack: tmpAck, args: tmpArgs, data: nil)
  52. }
  53. init(request: WebBlobRequest, fileMessage: FileMessage) {
  54. id = request.id
  55. type = request.type
  56. messageId = request.messageId.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
  57. baseMessage = fileMessage
  58. let tmpArgs:[AnyHashable:Any?] = ["id": id, "messageId": messageId, "type": type]
  59. let tmpAck = WebAbstractMessageAcknowledgement.init(request.requestId, false, nil)
  60. super.init(messageType: "response", messageSubType: "blob", requestId: nil, ack: tmpAck, args: tmpArgs, data: nil)
  61. }
  62. func addImage(completion: (()->())?) {
  63. let imageMessage = baseMessage as! ImageMessage
  64. if imageMessage.image == nil {
  65. let loader = ImageMessageLoader.init()
  66. DispatchQueue.main.sync {
  67. loader.start(with: imageMessage, onCompletion: { (loadedMessage) in
  68. let webBlob = WebBlob.init(imageMessage: loadedMessage as! ImageMessage)
  69. if webBlob.blob != nil {
  70. self.data = webBlob.objectDict()
  71. self.ack?.success = true
  72. } else {
  73. self.ack?.success = false
  74. self.ack?.error = "internalError"
  75. self.args?.updateValue(self.ack?.error, forKey: "error")
  76. }
  77. self.args?.updateValue(self.ack?.success, forKey: "success")
  78. completion!()
  79. }, onError: { (theError) in
  80. self.ack?.success = false
  81. self.ack?.error = "blobDownloadFailed"
  82. self.args?.updateValue(self.ack?.success, forKey: "success")
  83. self.args?.updateValue(self.ack?.error, forKey: "error")
  84. completion!()
  85. })
  86. }
  87. } else {
  88. let webBlob = WebBlob.init(imageMessage: imageMessage)
  89. if webBlob.blob != nil {
  90. self.data = webBlob.objectDict()
  91. self.ack?.success = true
  92. } else {
  93. self.ack?.success = false
  94. self.ack?.error = "internalError"
  95. self.args?.updateValue(self.ack?.error, forKey: "error")
  96. }
  97. self.args?.updateValue(self.ack?.success, forKey: "success")
  98. completion!()
  99. }
  100. }
  101. func addVideo(completion: (()->())?) {
  102. DispatchQueue.main.sync {
  103. let videoMessage = self.baseMessage as! VideoMessage
  104. if videoMessage.video == nil {
  105. let loader = VideoMessageLoader.init()
  106. loader.start(with: videoMessage, onCompletion: { (loadedMessage) in
  107. let webBlob = WebBlob.init(videoMessage: loadedMessage as! VideoMessage)
  108. if webBlob.blob != nil {
  109. self.data = webBlob.objectDict()
  110. self.ack?.success = true
  111. } else {
  112. self.ack?.success = false
  113. self.ack?.error = "internalError"
  114. self.args?.updateValue(self.ack?.error, forKey: "error")
  115. }
  116. self.args?.updateValue(self.ack?.success, forKey: "success")
  117. completion!()
  118. }, onError: { (theError) in
  119. self.ack?.success = false
  120. self.ack?.error = "blobDownloadFailed"
  121. self.args?.updateValue(self.ack?.success, forKey: "success")
  122. self.args?.updateValue(self.ack?.error, forKey: "error")
  123. completion!()
  124. })
  125. } else {
  126. let webBlob = WebBlob.init(videoMessage: videoMessage)
  127. if webBlob.blob != nil {
  128. self.data = webBlob.objectDict()
  129. self.ack?.success = true
  130. } else {
  131. self.ack?.success = false
  132. self.ack?.error = "internalError"
  133. self.args?.updateValue(self.ack?.error, forKey: "error")
  134. }
  135. self.args?.updateValue(self.ack?.success, forKey: "success")
  136. completion!()
  137. }
  138. }
  139. }
  140. func addAudio(completion: (()->())?) {
  141. let audioMessage = baseMessage as! AudioMessage
  142. if audioMessage.audio == nil {
  143. DispatchQueue.main.sync {
  144. let loader = BlobMessageLoader.init()
  145. loader.start(with: audioMessage, onCompletion: { (loadedMessage) in
  146. let webBlob = WebBlob.init(audioMessage: loadedMessage as! AudioMessage)
  147. if webBlob.blob != nil {
  148. self.data = webBlob.objectDict()
  149. self.ack?.success = true
  150. } else {
  151. self.ack?.success = false
  152. self.ack?.error = "internalError"
  153. self.args?.updateValue(self.ack?.error, forKey: "error")
  154. }
  155. self.args?.updateValue(self.ack?.success, forKey: "success")
  156. completion!()
  157. }, onError: { (theError) in
  158. self.ack?.success = false
  159. self.ack?.error = "blobDownloadFailed"
  160. self.args?.updateValue(self.ack?.success, forKey: "success")
  161. self.args?.updateValue(self.ack?.error, forKey: "error")
  162. completion!()
  163. })
  164. }
  165. } else {
  166. let webBlob = WebBlob.init(audioMessage: audioMessage)
  167. if webBlob.blob != nil {
  168. self.data = webBlob.objectDict()
  169. self.ack?.success = true
  170. } else {
  171. self.ack?.success = false
  172. self.ack?.error = "internalError"
  173. self.args?.updateValue(self.ack?.error, forKey: "error")
  174. }
  175. self.args?.updateValue(self.ack?.success, forKey: "success")
  176. completion!()
  177. }
  178. }
  179. func addFile(completion: (()->())?) {
  180. let fileMessage = baseMessage as! FileMessage
  181. if fileMessage.data == nil {
  182. DispatchQueue.main.sync {
  183. let loader = BlobMessageLoader.init()
  184. loader.start(with: fileMessage, onCompletion: { (loadedMessage) in
  185. let webBlob = WebBlob.init(fileMessage: loadedMessage as! FileMessage)
  186. if webBlob.blob != nil {
  187. self.data = webBlob.objectDict()
  188. self.ack?.success = true
  189. } else {
  190. self.ack?.success = false
  191. self.ack?.error = "internalError"
  192. self.args?.updateValue(self.ack?.error, forKey: "error")
  193. }
  194. self.args?.updateValue(self.ack?.success, forKey: "success")
  195. completion!()
  196. }, onError: { (theError) in
  197. self.ack?.success = false
  198. self.ack?.error = "blobDownloadFailed"
  199. self.args?.updateValue(self.ack?.success, forKey: "success")
  200. self.args?.updateValue(self.ack?.error, forKey: "error")
  201. completion!()
  202. })
  203. }
  204. } else {
  205. let webBlob = WebBlob.init(fileMessage: fileMessage)
  206. if webBlob.blob != nil {
  207. self.data = webBlob.objectDict()
  208. self.ack?.success = true
  209. } else {
  210. self.ack?.success = false
  211. self.ack?.error = "internalError"
  212. self.args?.updateValue(self.ack?.error, forKey: "error")
  213. }
  214. self.args?.updateValue(self.ack?.success, forKey: "success")
  215. completion!()
  216. }
  217. }
  218. }