NCUtility+Image.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // NCUtility+Image.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/11/23.
  6. // Copyright © 2023 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 UIKit
  24. import NextcloudKit
  25. import PDFKit
  26. import Accelerate
  27. import CoreMedia
  28. import Photos
  29. import SVGKit
  30. extension NCUtility {
  31. func loadImage(named imageName: String, colors: [UIColor]? = nil, size: CGFloat? = nil, useTypeIconFile: Bool = false, account: String? = nil) -> UIImage {
  32. var image: UIImage?
  33. if useTypeIconFile {
  34. switch imageName {
  35. case NKCommon.TypeIconFile.audio.rawValue: image = UIImage(systemName: "waveform", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  36. case NKCommon.TypeIconFile.code.rawValue: image = UIImage(systemName: "ellipsis.curlybraces", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  37. case NKCommon.TypeIconFile.compress.rawValue: image = UIImage(systemName: "doc.zipper", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  38. case NKCommon.TypeIconFile.directory.rawValue: image = UIImage(named: "folder")! .image(color: NCBrandColor.shared.getElement(account: account), size: UIScreen.main.bounds.width / 2)
  39. case NKCommon.TypeIconFile.document.rawValue: image = UIImage(systemName: "doc.richtext", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.documentIconColor]))
  40. case NKCommon.TypeIconFile.image.rawValue: image = UIImage(systemName: "photo", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  41. case NKCommon.TypeIconFile.movie.rawValue: image = UIImage(systemName: "video", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  42. case NKCommon.TypeIconFile.pdf.rawValue: image = UIImage(named: "file_pdf")!
  43. case NKCommon.TypeIconFile.ppt.rawValue: image = UIImage(systemName: "play.rectangle", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.presentationIconColor]))
  44. case NKCommon.TypeIconFile.txt.rawValue: image = UIImage(systemName: "doc.text", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  45. case NKCommon.TypeIconFile.url.rawValue: image = UIImage(systemName: "network", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  46. case NKCommon.TypeIconFile.xls.rawValue: image = UIImage(systemName: "tablecells", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.spreadsheetIconColor]))
  47. default: image = UIImage(systemName: "doc", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [NCBrandColor.shared.iconImageColor2]))
  48. }
  49. }
  50. if let image { return image }
  51. // SF IMAGE
  52. if let colors {
  53. image = UIImage(systemName: imageName, withConfiguration: UIImage.SymbolConfiguration(weight: .light))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: colors))
  54. } else {
  55. image = UIImage(systemName: imageName, withConfiguration: UIImage.SymbolConfiguration(weight: .light))
  56. }
  57. if let image { return image }
  58. // IMAGES
  59. if let color = colors?.first, let size {
  60. image = UIImage(named: imageName)?.image(color: color, size: size)
  61. } else if let color = colors?.first, size == nil {
  62. image = UIImage(named: imageName)?.image(color: color, size: 50)
  63. } else if colors == nil, size == nil {
  64. image = UIImage(named: imageName)?.resizeImage(size: CGSize(width: 50, height: 50))
  65. } else if colors == nil, let size {
  66. image = UIImage(named: imageName)?.resizeImage(size: CGSize(width: size, height: size))
  67. }
  68. if let image { return image }
  69. // NO IMAGES FOUND
  70. if let color = colors?.first, let size {
  71. return UIImage(systemName: "doc")!.image(color: color, size: size)
  72. } else {
  73. return UIImage(systemName: "doc")!
  74. }
  75. }
  76. func loadUserImage(for user: String, displayName: String?, urlBase: String) -> UIImage {
  77. let fileName = NCSession.shared.getFileName(urlBase: urlBase, user: user)
  78. let localFilePath = utilityFileSystem.directoryUserData + "/" + fileName
  79. if var localImage = UIImage(contentsOfFile: localFilePath) {
  80. let rect = CGRect(x: 0, y: 0, width: 30, height: 30)
  81. UIGraphicsBeginImageContextWithOptions(rect.size, false, 3.0)
  82. UIBezierPath(roundedRect: rect, cornerRadius: rect.size.height).addClip()
  83. localImage.draw(in: rect)
  84. localImage = UIGraphicsGetImageFromCurrentImageContext() ?? localImage
  85. UIGraphicsEndImageContext()
  86. return localImage
  87. } else if let image = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName).image {
  88. return image
  89. } else if let displayName = displayName, !displayName.isEmpty, let avatarImg = createAvatar(displayName: displayName, size: 30) {
  90. return avatarImg
  91. } else {
  92. return loadImage(named: "person.crop.circle", colors: [NCBrandColor.shared.iconImageColor])
  93. }
  94. }
  95. func imageFromVideo(url: URL, at time: TimeInterval) -> UIImage? {
  96. let asset = AVURLAsset(url: url)
  97. let assetIG = AVAssetImageGenerator(asset: asset)
  98. assetIG.appliesPreferredTrackTransform = true
  99. assetIG.apertureMode = AVAssetImageGenerator.ApertureMode.encodedPixels
  100. let cmTime = CMTime(seconds: time, preferredTimescale: 60)
  101. let thumbnailImageRef: CGImage
  102. do {
  103. thumbnailImageRef = try assetIG.copyCGImage(at: cmTime, actualTime: nil)
  104. } catch let error {
  105. print("Error: \(error)")
  106. return nil
  107. }
  108. return UIImage(cgImage: thumbnailImageRef)
  109. }
  110. func createImageFileFrom(metadata: tableMetadata) {
  111. if metadata.classFile != NKCommon.TypeClassFile.image.rawValue, metadata.classFile != NKCommon.TypeClassFile.video.rawValue { return }
  112. var image: UIImage?
  113. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  114. if image == nil {
  115. if metadata.classFile == NKCommon.TypeClassFile.image.rawValue {
  116. image = UIImage(contentsOfFile: fileNamePath)
  117. } else if metadata.classFile == NKCommon.TypeClassFile.video.rawValue {
  118. let videoPath = NSTemporaryDirectory() + "tempvideo.mp4"
  119. utilityFileSystem.linkItem(atPath: fileNamePath, toPath: videoPath)
  120. image = imageFromVideo(url: URL(fileURLWithPath: videoPath), at: 0)
  121. }
  122. }
  123. guard let image else { return }
  124. createImageStandard(ocId: metadata.ocId, etag: metadata.etag, image: image)
  125. }
  126. func createImageFileFrom(data: Data, metadata: tableMetadata) {
  127. createImageFileFrom( data: data, ocId: metadata.ocId, etag: metadata.etag)
  128. }
  129. func createImageFileFrom(data: Data, ocId: String, etag: String) {
  130. guard let image = UIImage(data: data) else { return }
  131. let fileNamePath1024 = self.utilityFileSystem.getDirectoryProviderStorageImageOcId(ocId, etag: etag, ext: global.previewExt1024)
  132. do {
  133. try data.write(to: URL(fileURLWithPath: fileNamePath1024), options: .atomic)
  134. } catch { }
  135. createImageStandard(ocId: ocId, etag: etag, image: image)
  136. }
  137. private func createImageStandard(ocId: String, etag: String, image: UIImage) {
  138. let ext = [global.previewExt1024, global.previewExt512, global.previewExt256]
  139. let size = [global.size1024, global.size512, global.size256]
  140. let compressionQuality = [0.5, 0.6, 0.7]
  141. for i in 0..<ext.count {
  142. if !utilityFileSystem.fileProviderStorageImageExists(ocId, etag: etag, ext: ext[i]),
  143. let image = image.resizeImage(size: size[i]),
  144. let data = image.jpegData(compressionQuality: compressionQuality[i]) {
  145. do {
  146. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageImageOcId(ocId, etag: etag, ext: ext[i])
  147. try data.write(to: URL(fileURLWithPath: fileNamePath))
  148. } catch { }
  149. }
  150. }
  151. }
  152. func getImage(ocId: String, etag: String, ext: String) -> UIImage? {
  153. return UIImage(contentsOfFile: self.utilityFileSystem.getDirectoryProviderStorageImageOcId(ocId, etag: etag, ext: ext))
  154. }
  155. func existsImage(ocId: String, etag: String, ext: String) -> Bool {
  156. return FileManager().fileExists(atPath: self.utilityFileSystem.getDirectoryProviderStorageImageOcId(ocId, etag: etag, ext: ext))
  157. }
  158. func imageFromVideo(url: URL, at time: TimeInterval, completion: @escaping (UIImage?) -> Void) {
  159. DispatchQueue.global(qos: .userInteractive).async {
  160. let asset = AVURLAsset(url: url)
  161. let assetIG = AVAssetImageGenerator(asset: asset)
  162. assetIG.appliesPreferredTrackTransform = true
  163. assetIG.apertureMode = AVAssetImageGenerator.ApertureMode.encodedPixels
  164. let cmTime = CMTime(seconds: time, preferredTimescale: 60)
  165. let thumbnailImageRef: CGImage
  166. do {
  167. thumbnailImageRef = try assetIG.copyCGImage(at: cmTime, actualTime: nil)
  168. } catch let error {
  169. print("Error: \(error)")
  170. return completion(nil)
  171. }
  172. DispatchQueue.main.async {
  173. completion(UIImage(cgImage: thumbnailImageRef))
  174. }
  175. }
  176. }
  177. func pdfThumbnail(url: URL, width: CGFloat = 240) -> UIImage? {
  178. guard let data = try? Data(contentsOf: url), let page = PDFDocument(data: data)?.page(at: 0) else {
  179. return nil
  180. }
  181. let pageSize = page.bounds(for: .mediaBox)
  182. let pdfScale = width / pageSize.width
  183. // Apply if you're displaying the thumbnail on screen
  184. let scale = UIScreen.main.scale * pdfScale
  185. let screenSize = CGSize(width: pageSize.width * scale, height: pageSize.height * scale)
  186. return page.thumbnail(of: screenSize, for: .mediaBox)
  187. }
  188. func createAvatar(displayName: String, size: CGFloat) -> UIImage? {
  189. func usernameToColor(_ username: String) -> CGColor {
  190. // Normalize hash
  191. let lowerUsername = username.lowercased()
  192. var hash: String
  193. // swiftlint:disable force_try
  194. let regex = try! NSRegularExpression(pattern: "^([0-9a-f]{4}-?){8}$")
  195. // swiftlint:enable force_try
  196. let matches = regex.matches(
  197. in: username,
  198. range: NSRange(username.startIndex..., in: username))
  199. if !matches.isEmpty {
  200. // Already a md5 hash?
  201. // done, use as is.
  202. hash = lowerUsername
  203. } else {
  204. hash = lowerUsername.md5()
  205. }
  206. hash = hash.replacingOccurrences(of: "[^0-9a-f]", with: "", options: .regularExpression)
  207. // userColors has 18 colors by default
  208. let result = hash.compactMap(\.hexDigitValue)
  209. let userColorIx = result.reduce(0, { $0 + $1 }) % 18
  210. return NCBrandColor.shared.userColors[userColorIx]
  211. }
  212. guard let initials = displayName.uppercaseInitials else {
  213. return nil
  214. }
  215. let userColor = usernameToColor(displayName)
  216. let rect = CGRect(x: 0, y: 0, width: size, height: size)
  217. var avatarImage: UIImage?
  218. UIGraphicsBeginImageContextWithOptions(rect.size, false, 3.0)
  219. let context = UIGraphicsGetCurrentContext()
  220. UIBezierPath(roundedRect: rect, cornerRadius: rect.size.height).addClip()
  221. context?.setFillColor(userColor)
  222. context?.fill(rect)
  223. let textStyle = NSMutableParagraphStyle()
  224. textStyle.alignment = NSTextAlignment.center
  225. let lineHeight = UIFont.systemFont(ofSize: UIFont.systemFontSize).pointSize
  226. NSString(string: initials)
  227. .draw(
  228. in: CGRect(x: 0, y: (size - lineHeight) / 2, width: size, height: lineHeight),
  229. withAttributes: [NSAttributedString.Key.paragraphStyle: textStyle])
  230. avatarImage = UIGraphicsGetImageFromCurrentImageContext()
  231. UIGraphicsEndImageContext()
  232. return avatarImage
  233. }
  234. func convertSVGtoPNGWriteToUserData(svgUrlString: String, fileName: String? = nil, width: CGFloat? = nil, rewrite: Bool, account: String, id: Int? = nil, completion: @escaping (_ imageNamePath: String?, _ id: Int?) -> Void) {
  235. var fileNamePNG = ""
  236. guard let svgUrlString = svgUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
  237. let iconURL = URL(string: svgUrlString) else {
  238. return completion(nil, id)
  239. }
  240. if let fileName = fileName {
  241. fileNamePNG = fileName
  242. } else {
  243. fileNamePNG = iconURL.deletingPathExtension().lastPathComponent + ".png"
  244. }
  245. let imageNamePath = utilityFileSystem.directoryUserData + "/" + fileNamePNG
  246. if !FileManager.default.fileExists(atPath: imageNamePath) || rewrite == true {
  247. NextcloudKit.shared.downloadContent(serverUrl: iconURL.absoluteString, account: account) { _, responseData, error in
  248. if error == .success, let data = responseData?.data {
  249. if let image = UIImage(data: data) {
  250. var newImage: UIImage = image
  251. if width != nil {
  252. let ratio = image.size.height / image.size.width
  253. let newSize = CGSize(width: width!, height: width! * ratio)
  254. let renderFormat = UIGraphicsImageRendererFormat.default()
  255. renderFormat.opaque = false
  256. let renderer = UIGraphicsImageRenderer(size: CGSize(width: newSize.width, height: newSize.height), format: renderFormat)
  257. newImage = renderer.image { _ in
  258. image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  259. }
  260. }
  261. guard let pngImageData = newImage.pngData() else {
  262. return completion(nil, id)
  263. }
  264. try? pngImageData.write(to: URL(fileURLWithPath: imageNamePath))
  265. return completion(imageNamePath, id)
  266. } else {
  267. guard let svgImage: SVGKImage = SVGKImage(data: data) else {
  268. return completion(nil, id)
  269. }
  270. if width != nil {
  271. let scale = svgImage.size.height / svgImage.size.width
  272. svgImage.size = CGSize(width: width!, height: width! * scale)
  273. }
  274. guard let image: UIImage = svgImage.uiImage else {
  275. return completion(nil, id)
  276. }
  277. guard let pngImageData = image.pngData() else {
  278. return completion(nil, id)
  279. }
  280. try? pngImageData.write(to: URL(fileURLWithPath: imageNamePath))
  281. return completion(imageNamePath, id)
  282. }
  283. } else {
  284. return completion(nil, id)
  285. }
  286. }
  287. } else {
  288. return completion(imageNamePath, id)
  289. }
  290. }
  291. func getUserStatus(userIcon: String?, userStatus: String?, userMessage: String?) -> (statusImage: UIImage?, statusMessage: String, descriptionMessage: String) {
  292. var statusImage: UIImage?
  293. var statusMessage: String = ""
  294. var descriptionMessage: String = ""
  295. var messageUserDefined: String = ""
  296. if userStatus?.lowercased() == "online" {
  297. statusImage = loadImage(named: "circle_fill", colors: [UIColor(red: 103.0 / 255.0, green: 176.0 / 255.0, blue: 134.0 / 255.0, alpha: 1.0)])
  298. messageUserDefined = NSLocalizedString("_online_", comment: "")
  299. }
  300. if userStatus?.lowercased() == "away" {
  301. statusImage = loadImage(named: "userStatusAway", colors: [UIColor(red: 233.0 / 255.0, green: 166.0 / 255.0, blue: 75.0 / 255.0, alpha: 1.0)])
  302. messageUserDefined = NSLocalizedString("_away_", comment: "")
  303. }
  304. if userStatus?.lowercased() == "dnd" {
  305. statusImage = loadImage(named: "userStatusDnd")
  306. messageUserDefined = NSLocalizedString("_dnd_", comment: "")
  307. descriptionMessage = NSLocalizedString("_dnd_description_", comment: "")
  308. }
  309. if userStatus?.lowercased() == "offline" || userStatus?.lowercased() == "invisible" {
  310. statusImage = UIImage(named: "userStatusOffline")!.withTintColor(.init(named: "SystemBackgroundInverted")!)
  311. messageUserDefined = NSLocalizedString("_invisible_", comment: "")
  312. descriptionMessage = NSLocalizedString("_invisible_description_", comment: "")
  313. }
  314. if let userIcon = userIcon {
  315. statusMessage = userIcon + " "
  316. }
  317. if let userMessage = userMessage {
  318. statusMessage += userMessage
  319. }
  320. statusMessage = statusMessage.trimmingCharacters(in: .whitespaces)
  321. if statusMessage.isEmpty {
  322. statusMessage = messageUserDefined
  323. }
  324. return(statusImage, statusMessage, descriptionMessage)
  325. }
  326. func memorySizeOfImage(_ image: UIImage) -> Int {
  327. guard let imageData = image.pngData() else { return 0 }
  328. return imageData.count
  329. }
  330. }