URLSenderItemCreator.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 URLSenderItemCreator : NSObject {
  23. @objc public static func getSenderItem(for url : URL, maxSize : String?) -> URLSenderItem? {
  24. if !URLSenderItemCreator.validate(url: url) {
  25. return nil
  26. }
  27. let uti = UTIConverter.uti(forFileURL: url)
  28. if uti == nil {
  29. DDLogError("URLSender: unsupported url: \(url)")
  30. return nil
  31. }
  32. var item: URLSenderItem?
  33. if UTIConverter.conforms(toMovieType: uti) {
  34. let creator = VideoURLSenderItemCreator()
  35. item = creator.senderItem(from: url)
  36. } else if UTIConverter.conforms(toImageType: uti) {
  37. let creator : ImageURLSenderItemCreator
  38. if (maxSize != nil) {
  39. creator = ImageURLSenderItemCreator(with: maxSize!, forceSize: false)
  40. } else {
  41. creator = ImageURLSenderItemCreator()
  42. }
  43. item = creator.senderItem(from: url)
  44. } else {
  45. item = URLSenderItem(url: url, type: uti, renderType: NSNumber(value: 0), sendAsFile: true)
  46. }
  47. return item
  48. }
  49. /// Will return a sender item for the file at url. The file will be transcoded or scaled and its metadata will be removed
  50. /// if it conforms to isMovieMimeType or isImageMimeType.
  51. /// - Parameter url: The url at which the file is stored
  52. /// - Returns: An url sender item if one can be created
  53. @objc public static func getSenderItem(for url : URL) -> URLSenderItem? {
  54. return URLSenderItemCreator.getSenderItem(for: url, maxSize: nil)
  55. }
  56. private static func validate(url : URL) -> Bool {
  57. guard let scheme = url.scheme else {
  58. return false
  59. }
  60. if (scheme != "file") || !FileManager.default.fileExists(atPath: url.relativePath) {
  61. return false
  62. }
  63. return true
  64. }
  65. }