WebCreateFileMessageRequest.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. import ThreemaFramework
  22. import CocoaLumberjackSwift
  23. public class WebCreateFileMessageRequest: WebAbstractMessage {
  24. var backgroundIdentifier: String?
  25. var type: String
  26. var id: String?
  27. var groupId: Data?
  28. var name: String
  29. var fileType: String
  30. var sendAsFile: Bool
  31. var size: Int
  32. var fileData: Data?
  33. var caption: String?
  34. var tmpError: String? = nil
  35. var baseMessage: BaseMessage? = nil
  36. var session: WCSession?
  37. override init(message:WebAbstractMessage) {
  38. type = message.args!["type"] as! String
  39. if type == "contact" {
  40. id = message.args!["id"] as? String
  41. } else {
  42. let idString = message.args!["id"] as! String
  43. groupId = idString.hexadecimal()
  44. }
  45. let data = message.data! as! [AnyHashable:Any?]
  46. name = data["name"] as! String
  47. fileType = data["fileType"] as! String
  48. sendAsFile = data["sendAsFile"] as! Bool
  49. if let tmpSize = data["size"] as? UInt8 {
  50. size = Int(tmpSize)
  51. }
  52. else if let tmpSize = data["size"] as? UInt16 {
  53. size = Int(tmpSize)
  54. }
  55. else if let tmpSize = data["size"] as? UInt32 {
  56. size = Int(tmpSize)
  57. }
  58. else if let tmpSize = data["size"] as? UInt64 {
  59. size = Int(tmpSize)
  60. }
  61. else {
  62. size = 0
  63. }
  64. self.fileData = data["data"] as? Data
  65. caption = data["caption"] as? String
  66. super.init(message: message)
  67. }
  68. init(message:WebAbstractMessage, session: WCSession) {
  69. type = message.args!["type"] as! String
  70. if type == "contact" {
  71. id = message.args!["id"] as? String
  72. } else {
  73. let idString = message.args!["id"] as! String
  74. groupId = idString.hexadecimal()
  75. }
  76. let data = message.data! as! [AnyHashable:Any?]
  77. name = data["name"] as! String
  78. fileType = data["fileType"] as! String
  79. sendAsFile = data["sendAsFile"] as! Bool
  80. if let tmpSize = data["size"] as? UInt8 {
  81. size = Int(tmpSize)
  82. }
  83. else if let tmpSize = data["size"] as? UInt16 {
  84. size = Int(tmpSize)
  85. }
  86. else if let tmpSize = data["size"] as? UInt32 {
  87. size = Int(tmpSize)
  88. }
  89. else if let tmpSize = data["size"] as? UInt64 {
  90. size = Int(tmpSize)
  91. }
  92. else {
  93. size = 0
  94. }
  95. self.fileData = data["data"] as? Data
  96. caption = data["caption"] as? String
  97. self.session = session
  98. super.init(message: message)
  99. }
  100. func sendMessage(completion: @escaping () -> ()) {
  101. var conversation: Conversation? = nil
  102. var entityManager: EntityManager?
  103. if self.type == "contact" {
  104. entityManager = EntityManager()
  105. let contact = entityManager?.entityFetcher.contact(forId: self.id)
  106. if contact == nil {
  107. self.baseMessage = nil
  108. tmpError = "internalError"
  109. completion()
  110. return
  111. }
  112. conversation = entityManager!.entityFetcher.conversation(for: contact)
  113. if conversation == nil {
  114. entityManager?.performSyncBlockAndSafe({
  115. conversation = entityManager!.entityCreator.conversation()
  116. conversation?.contact = contact
  117. })
  118. }
  119. } else {
  120. entityManager = EntityManager()
  121. conversation = entityManager?.entityFetcher.conversation(forGroupId: self.groupId)
  122. }
  123. if conversation != nil {
  124. if !PermissionChecker.init().canSend(in: conversation, entityManager: entityManager) {
  125. self.baseMessage = nil
  126. tmpError = "blocked"
  127. completion()
  128. return
  129. }
  130. if size > kMaxFileSize {
  131. self.baseMessage = nil
  132. tmpError = "fileTooLarge"
  133. completion()
  134. return
  135. }
  136. if fileData == nil {
  137. self.baseMessage = nil
  138. tmpError = "internalError"
  139. completion()
  140. return
  141. }
  142. backgroundIdentifier = BackgroundTaskManager.shared.counter(identifier: kAppSendingBackgroundTask)
  143. BackgroundTaskManager.shared.newBackgroundTask(key: backgroundIdentifier!, timeout: Int(kAppSendingBackgroundTaskTime)) {
  144. if !self.sendAsFile && (self.fileType == "image/jpeg" || self.fileType == "image/pjpeg" || self.fileType == "image/png" || self.fileType == "image/x-png" ) {
  145. let imageSender = ImageURLSenderItemCreator()
  146. guard let item = imageSender.senderItem(from: self.fileData!, uti: self.fileType) else {
  147. DDLogError("Could not create URLSenderItem from image")
  148. return
  149. }
  150. item.caption = self.caption
  151. let sender = FileMessageSender()
  152. sender.send(item, in: conversation, requestId: self.requestId)
  153. }
  154. else if !self.sendAsFile && ( self.fileType == "audio/m4a" || self.fileType == "audio/x-m4a" || self.fileType == "audio/mp4" ) {
  155. // let senderItem = URLSenderItem(data: self.fileData!, fileName: "audio.m4a", type: self.type, renderType: 1, sendAsFile: true)
  156. //
  157. // let fileSender = FileMessageSender()
  158. // fileSender.send(senderItem, in: conversation, requestId: self.requestId)
  159. let audioSender = AudioMessageSender.init()
  160. let tmpPath = NSTemporaryDirectory() + "audio.m4a"
  161. if FileManager.default.fileExists(atPath: tmpPath) {
  162. do {
  163. try FileManager.default.removeItem(atPath: tmpPath)
  164. }
  165. catch {
  166. print("can't delete file at path %@", tmpPath)
  167. }
  168. }
  169. FileManager.default.createFile(atPath: tmpPath, contents: self.fileData, attributes: nil)
  170. let audio = AVURLAsset.init(url: URL.init(fileURLWithPath: tmpPath))
  171. let duration = CMTimeGetSeconds(audio.duration)
  172. audioSender.start(withAudioData: self.fileData, duration: NSNumber(value: Float(duration)), in: conversation, requestId: self.requestId)
  173. if FileManager.default.fileExists(atPath: tmpPath) {
  174. do {
  175. try FileManager.default.removeItem(atPath: tmpPath)
  176. }
  177. catch {
  178. print("can't delete file at path %@", tmpPath)
  179. }
  180. }
  181. }
  182. else if !self.sendAsFile && ( self.fileType == "video/mp4" || self.fileType == "video/mpeg4" || self.fileType == "video/x-m4v" ) {
  183. let creator = VideoURLSenderItemCreator()
  184. guard let data = self.fileData else {
  185. return
  186. }
  187. guard let videoURL = VideoURLSenderItemCreator.writeToTemporaryDirectory(data: data) else {
  188. return
  189. }
  190. guard let senderItem = creator.senderItem(from: videoURL) else {
  191. return
  192. }
  193. let fileSender = FileMessageSender()
  194. fileSender.send(senderItem, in: conversation, requestId: self.requestId)
  195. do {
  196. try FileManager.default.removeItem(at: videoURL)
  197. } catch {
  198. DDLogError("Could not clear temporary directory \(error)")
  199. }
  200. }
  201. else {
  202. DispatchQueue.main.async {
  203. let blobSender = FileMessageSender.init()
  204. blobSender.fileNameFromWeb = self.name
  205. let item = URLSenderItem.init(data: self.fileData, fileName: blobSender.fileNameFromWeb, type: UTIConverter.uti(fromMimeType: self.fileType), renderType:0, sendAsFile: true)
  206. item?.caption = self.caption
  207. blobSender.send(item, in: conversation, requestId: self.requestId)
  208. }
  209. }
  210. completion()
  211. return
  212. }
  213. } else {
  214. self.baseMessage = nil
  215. tmpError = "internalError"
  216. completion()
  217. return
  218. }
  219. }
  220. }