URLSender.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 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 CocoaLumberjackSwift
  22. @objc public class URLSender : NSObject {
  23. /// Sends the file from url as a file message
  24. /// - Parameters:
  25. /// - url: A local url pointing to a file
  26. /// - asFile: Whether the file should be sent as is. Can be true if the photo/video has previously been converted
  27. /// or if the user has explicitly chosen to send this file to be rendered as a file
  28. /// - caption: The caption displayed below the file
  29. /// - conversation: The conversation to which the file should be sent
  30. @objc public static func sendUrl(_ url : URL, asFile : Bool, caption : String?, conversation : Conversation) {
  31. let senderItem : URLSenderItem?
  32. if asFile {
  33. let mimeType = UTIConverter.mimeType(fromUTI: UTIConverter.uti(forFileURL: url))
  34. senderItem = URLSenderItem.init(url: url, type: mimeType, renderType: 0, sendAsFile: true)
  35. } else {
  36. senderItem = URLSenderItemCreator.getSenderItem(for: url)
  37. }
  38. if caption != nil {
  39. senderItem?.caption = caption
  40. }
  41. if senderItem == nil {
  42. DDLogError("Could not create sender item")
  43. return
  44. }
  45. let sender = FileMessageSender()
  46. sender.send(senderItem, in: conversation)
  47. }
  48. }