NCUtility.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // NCUtility.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/06/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 SVGKit
  25. class NCUtility: NSObject {
  26. @objc static let sharedInstance: NCUtility = {
  27. let instance = NCUtility()
  28. return instance
  29. }()
  30. @objc func createFileName(_ fileName: String, directoryID: String) -> String {
  31. var resultFileName = fileName
  32. var exitLoop = false
  33. while exitLoop == false {
  34. if NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileNameView == %@ AND directoryID == %@", resultFileName, directoryID)) != nil {
  35. var name = NSString(string: resultFileName).deletingPathExtension
  36. let ext = NSString(string: resultFileName).pathExtension
  37. let characters = Array(name)
  38. if characters.count < 2 {
  39. resultFileName = name + " " + "1" + "." + ext
  40. } else {
  41. let space = characters[characters.count-2]
  42. let numChar = characters[characters.count-1]
  43. var num = Int(String(numChar))
  44. if (space == " " && num != nil) {
  45. name = String(name.dropLast())
  46. num = num! + 1
  47. resultFileName = name + "\(num!)" + "." + ext
  48. } else {
  49. resultFileName = name + " " + "1" + "." + ext
  50. }
  51. }
  52. } else {
  53. exitLoop = true
  54. }
  55. }
  56. return resultFileName
  57. }
  58. @objc func isEncryptedMetadata(_ metadata: tableMetadata) -> Bool {
  59. if metadata.fileName != metadata.fileNameView && metadata.fileName.count == 32 && metadata.fileName.contains(".") == false {
  60. return true
  61. }
  62. return false
  63. }
  64. @objc func getFileSize(asset: PHAsset) -> Int64 {
  65. let resources = PHAssetResource.assetResources(for: asset)
  66. if let resource = resources.first {
  67. if resource.responds(to: #selector(NSDictionary.fileSize)) {
  68. let unsignedInt64 = resource.value(forKey: "fileSize") as! CLong
  69. return Int64(bitPattern: UInt64(unsignedInt64))
  70. }
  71. }
  72. return 0
  73. }
  74. @objc func getScreenWidthForPreview() -> CGFloat {
  75. let screenSize = UIScreen.main.bounds
  76. let screenWidth = screenSize.width * 0.75
  77. return screenWidth
  78. }
  79. @objc func getScreenHeightForPreview() -> CGFloat {
  80. let screenSize = UIScreen.main.bounds
  81. let screenWidth = screenSize.height * 0.75
  82. return screenWidth
  83. }
  84. @objc func convertFileIDClientToFileIDServer(_ fileID: NSString) -> String {
  85. let split = fileID.components(separatedBy: "oc")
  86. if split.count == 2 {
  87. let fileIDServerInt = CLongLong(split[0])
  88. return String(describing: fileIDServerInt ?? 0)
  89. }
  90. return fileID as String
  91. }
  92. func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
  93. let scale = newWidth / image.size.width
  94. let newHeight = image.size.height * scale
  95. UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
  96. image.draw(in: (CGRect(x: 0, y: 0, width: newWidth, height: newHeight)))
  97. let newImage = UIGraphicsGetImageFromCurrentImageContext()!
  98. UIGraphicsEndImageContext()
  99. return newImage
  100. }
  101. func cellBlurEffect(with frame: CGRect) -> UIView {
  102. let blurEffect = UIBlurEffect(style: .extraLight)
  103. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  104. blurEffectView.frame = frame
  105. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  106. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  107. return blurEffectView
  108. }
  109. func setLayoutForView(key: String, layout: String, sort: String, ascending: Bool, groupBy: String, directoryOnTop: Bool) {
  110. let string = layout + "|" + sort + "|" + "\(ascending)" + "|" + groupBy + "|" + "\(directoryOnTop)"
  111. UICKeyChainStore.setString(string, forKey: key, service: k_serviceShareKeyChain)
  112. }
  113. func getLayoutForView(key: String) -> (String, String, Bool, String, Bool) {
  114. guard let string = UICKeyChainStore.string(forKey: key, service: k_serviceShareKeyChain) else {
  115. return (k_layout_list, "fileName", true, "none", true)
  116. }
  117. let array = string.components(separatedBy: "|")
  118. if array.count == 5 {
  119. let sort = NSString(string: array[2])
  120. let directoryOnTop = NSString(string: array[4])
  121. return (array[0], array[1], sort.boolValue, array[3], directoryOnTop.boolValue)
  122. }
  123. return (k_layout_list, "fileName", true, "none", true)
  124. }
  125. func convertSVGtoPNGWriteToUserData(svgUrlString: String) {
  126. guard let svgUrlString = svgUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  127. return
  128. }
  129. guard let iconURL = URL(string: svgUrlString) else {
  130. return
  131. }
  132. let fileName = iconURL.deletingPathExtension().lastPathComponent
  133. let imageNamePath = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
  134. if !FileManager.default.fileExists(atPath: imageNamePath) {
  135. guard let svgkImage: SVGKImage = SVGKImage(contentsOf: iconURL) else {
  136. return
  137. }
  138. guard let image: UIImage = svgkImage.uiImage else {
  139. return
  140. }
  141. guard let pngImageData = image.pngData() else {
  142. return
  143. }
  144. CCUtility.write(pngImageData, fileNamePath: imageNamePath)
  145. }
  146. }
  147. }
  148. //MARK: -
  149. @IBDesignable class NCAvatar: UIImageView {
  150. @IBInspectable var roundness: CGFloat = 2 {
  151. didSet{
  152. layoutSubviews()
  153. }
  154. }
  155. @IBInspectable var borderWidth: CGFloat = 5 {
  156. didSet{
  157. layoutSubviews()
  158. }
  159. }
  160. @IBInspectable var borderColor: UIColor = UIColor.blue {
  161. didSet{
  162. layoutSubviews()
  163. }
  164. }
  165. @IBInspectable var background: UIColor = UIColor.clear {
  166. didSet{
  167. layoutSubviews()
  168. }
  169. }
  170. override func layoutSubviews() {
  171. super.layoutSubviews()
  172. layer.cornerRadius = bounds.width / roundness
  173. layer.borderWidth = borderWidth
  174. layer.borderColor = borderColor.cgColor
  175. layer.backgroundColor = background.cgColor
  176. clipsToBounds = true
  177. let path = UIBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), cornerRadius: bounds.width / roundness)
  178. let mask = CAShapeLayer()
  179. mask.path = path.cgPath
  180. layer.mask = mask
  181. }
  182. }