MediaPreviewItem.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 ThreemaFramework
  22. import CocoaLumberjackSwift
  23. protocol MediaPreviewItemProtocol {
  24. func getItem() -> Data?
  25. func getThumbnail(onCompletion: (UIImage) -> Void)
  26. func requestAsset()
  27. func getAccessiblityDescription() -> String?
  28. }
  29. @objc class MediaPreviewItem: NSObject {
  30. var thumbnail: UIImage?
  31. var itemUrl: URL?
  32. var uti: String?
  33. var originalAsset: DKAsset?
  34. var sendAsFile: Bool = false
  35. var caption: String?
  36. var filename: String?
  37. let thumbnailSemaphore = DispatchSemaphore(value: 0)
  38. let semaphore = DispatchSemaphore(value: 0)
  39. init(originalAsset: DKAsset) {
  40. self.originalAsset = originalAsset
  41. }
  42. init(itemUrl : URL) {
  43. self.itemUrl = itemUrl
  44. }
  45. func requestAsset() {
  46. // Do nothing - A general item might not have an asset
  47. }
  48. func getThumbnail(onCompletion: (UIImage) -> Void) {
  49. if self.thumbnail == nil {
  50. self.thumbnailSemaphore.wait()
  51. self.thumbnailSemaphore.signal()
  52. }
  53. guard let image = self.thumbnail else {
  54. return
  55. }
  56. onCompletion(image)
  57. }
  58. func getThumbnail() -> UIImage? {
  59. if self.thumbnail == nil {
  60. self.thumbnailSemaphore.wait()
  61. self.thumbnailSemaphore.signal()
  62. }
  63. guard let thumbnail = self.thumbnail else {
  64. return nil
  65. }
  66. return thumbnail
  67. }
  68. func getAccessiblityDescription() -> String? {
  69. return nil
  70. }
  71. func removeItem() {
  72. guard let url = self.itemUrl else {
  73. return
  74. }
  75. do {
  76. try FileManager.default.removeItem(at: url)
  77. } catch {
  78. DDLogError("Could not remove item because \(error.localizedDescription)")
  79. }
  80. }
  81. }