IMImagemeter.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // IMImagemeter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/03/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import ZIPFoundation
  25. //MARK: - IMImagemeterCodable
  26. class IMImagemeterCodable: NSObject {
  27. @objc static let sharedInstance: IMImagemeterCodable = {
  28. let instance = IMImagemeterCodable()
  29. return instance
  30. }()
  31. struct imagemeterAnnotation: Codable {
  32. struct coordinates: Codable {
  33. let x: CGFloat
  34. let y: CGFloat
  35. }
  36. struct color: Codable {
  37. let rgba: [Int]?
  38. }
  39. struct end_pt: Codable {
  40. let end_pt: coordinates
  41. enum CodingKeys : String, CodingKey {
  42. case end_pt = "end-pt"
  43. }
  44. }
  45. struct style: Codable {
  46. let color: color?
  47. let line_width: CGFloat?
  48. let text_outline_width: CGFloat?
  49. let font_base_size: CGFloat?
  50. let font_magnification: CGFloat?
  51. enum CodingKeys : String, CodingKey {
  52. case color
  53. case line_width = "line-width"
  54. case text_outline_width = "text-outline-width"
  55. case font_base_size = "font-base-size"
  56. case font_magnification = "font-magnification"
  57. }
  58. }
  59. struct audio_recording: Codable {
  60. let recording_filename: String
  61. let recording_duration_msecs: Int
  62. enum CodingKeys : String, CodingKey {
  63. case recording_filename = "recording-filename"
  64. case recording_duration_msecs = "recording-duration-msecs"
  65. }
  66. }
  67. struct capture_timestamp: Codable {
  68. let year: Int
  69. let month: Int
  70. let day: Int
  71. let hour: Int
  72. let minutes: Int
  73. let seconds: Int
  74. }
  75. struct image: Codable {
  76. let title: String
  77. let filename: String
  78. let annotated_image_filename: String
  79. let rotation: Int
  80. enum CodingKeys : String, CodingKey {
  81. case title
  82. case filename
  83. case annotated_image_filename = "annotated-image-filename"
  84. case rotation
  85. }
  86. }
  87. struct export_image_cache: Codable {
  88. let width: CGFloat
  89. let height: CGFloat
  90. let file_format: String
  91. let with_hardware_antialiasing: Bool
  92. let with_watermark: Bool
  93. let with_image_title: Bool
  94. enum CodingKeys : String, CodingKey {
  95. case width
  96. case height
  97. case file_format = "file-format"
  98. case with_hardware_antialiasing = "with-hardware-antialiasing"
  99. case with_watermark = "with-watermark"
  100. case with_image_title = "with-image-title"
  101. }
  102. }
  103. struct elements: Codable {
  104. let id: Int
  105. let class_: String
  106. let center: coordinates?
  107. let width: CGFloat?
  108. let arrows: [end_pt]?
  109. let text: String?
  110. let audio_recording: audio_recording?
  111. let show_border: Bool?
  112. let show_arrows: Bool?
  113. let fill_background: Bool?
  114. let style: style?
  115. enum CodingKeys : String, CodingKey {
  116. case id
  117. case class_ = "class"
  118. case center
  119. case width
  120. case arrows
  121. case text
  122. case audio_recording = "audio-recording"
  123. case show_border = "show-border"
  124. case show_arrows = "show-arrows"
  125. case fill_background = "fill-background"
  126. case style
  127. }
  128. }
  129. struct thumbnails: Codable {
  130. let filename: String
  131. let width: CGFloat
  132. let height: CGFloat
  133. }
  134. let is_example_image: Bool
  135. let version: Int
  136. let capture_timestamp: capture_timestamp
  137. let image: image
  138. let export_image_cache: [export_image_cache]
  139. let elements: [elements]?
  140. let id: String
  141. let thumbnails: [thumbnails]
  142. let last_modification: Int
  143. enum CodingKeys : String, CodingKey {
  144. case is_example_image = "is-example-image"
  145. case version
  146. case capture_timestamp = "capture-timestamp"
  147. case image
  148. case export_image_cache = "export-image-cache"
  149. case elements
  150. case id
  151. case thumbnails
  152. case last_modification = "last-modification"
  153. }
  154. }
  155. func decoderAnnotetion(_ annotation: Data) -> imagemeterAnnotation? {
  156. let jsonDecoder = JSONDecoder.init()
  157. do {
  158. let decode = try jsonDecoder.decode(imagemeterAnnotation.self, from: annotation)
  159. return decode
  160. } catch let error {
  161. print("Serious internal error in decoding metadata ("+error.localizedDescription+")")
  162. return nil
  163. }
  164. }
  165. func convertCoordinate(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) -> (x: CGFloat, y: CGFloat) {
  166. let factor = sqrt(width * height / (1024*768))
  167. let factorX = factor * x + width/2
  168. let factorY = factor * y + height/2
  169. return(factorX, factorY)
  170. }
  171. }
  172. //MARK: - IMImagemeter
  173. class IMImagemeter: NSObject {
  174. static let sharedInstance: IMImagemeter = {
  175. let instance = IMImagemeter()
  176. return instance
  177. }()
  178. func getBundleDirectory(metadata: tableMetadata) -> (error: Bool, bundleDirectory: String, immPath: String) {
  179. var error = true
  180. var bundleDirectory = ""
  181. var immPath = ""
  182. let source = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageocId(metadata.ocId, fileNameView: metadata.fileNameView))
  183. if let archive = Archive(url: source, accessMode: .read) {
  184. archive.forEach({ (entry) in
  185. let pathComponents = (entry.path as NSString).pathComponents
  186. if pathComponents.count == 2 && (pathComponents.last! as NSString).pathExtension.lowercased() == "imm" {
  187. error = false
  188. bundleDirectory = CCUtility.getDirectoryProviderStorageocId(metadata.ocId) + "/" + pathComponents.first!
  189. immPath = CCUtility.getDirectoryProviderStorageocId(metadata.ocId) + "/" + entry.path
  190. }
  191. })
  192. }
  193. return(error, bundleDirectory, immPath)
  194. }
  195. }