NCUtility.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 <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 SVGKit
  25. import KTVHTTPCache
  26. import NCCommunication
  27. import PDFKit
  28. class NCUtility: NSObject {
  29. @objc static let shared: NCUtility = {
  30. let instance = NCUtility()
  31. return instance
  32. }()
  33. let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
  34. @objc func getWebDAV(account: String) -> String {
  35. return NCManageDatabase.sharedInstance.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesWebDavRoot) ?? "remote.php/webdav"
  36. }
  37. @objc func getDAV() -> String {
  38. return "remote.php/dav"
  39. }
  40. @objc func getHomeServer(urlBase: String, account: String) -> String {
  41. return urlBase + "/" + self.getWebDAV(account: account)
  42. }
  43. @objc func createFileName(_ fileName: String, serverUrl: String, account: String) -> String {
  44. var resultFileName = fileName
  45. var exitLoop = false
  46. while exitLoop == false {
  47. if NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileNameView == %@ AND serverUrl == %@ AND account == %@", resultFileName, serverUrl, account)) != nil {
  48. var name = NSString(string: resultFileName).deletingPathExtension
  49. let ext = NSString(string: resultFileName).pathExtension
  50. let characters = Array(name)
  51. if characters.count < 2 {
  52. if ext == "" {
  53. resultFileName = name + " " + "1"
  54. } else {
  55. resultFileName = name + " " + "1" + "." + ext
  56. }
  57. } else {
  58. let space = characters[characters.count-2]
  59. let numChar = characters[characters.count-1]
  60. var num = Int(String(numChar))
  61. if (space == " " && num != nil) {
  62. name = String(name.dropLast())
  63. num = num! + 1
  64. if ext == "" {
  65. resultFileName = name + "\(num!)"
  66. } else {
  67. resultFileName = name + "\(num!)" + "." + ext
  68. }
  69. } else {
  70. if ext == "" {
  71. resultFileName = name + " " + "1"
  72. } else {
  73. resultFileName = name + " " + "1" + "." + ext
  74. }
  75. }
  76. }
  77. } else {
  78. exitLoop = true
  79. }
  80. }
  81. return resultFileName
  82. }
  83. @objc func isEncryptedMetadata(_ metadata: tableMetadata) -> Bool {
  84. if metadata.fileName != metadata.fileNameView && metadata.fileName.count == 32 && metadata.fileName.contains(".") == false {
  85. return true
  86. }
  87. return false
  88. }
  89. @objc func resizeImage(image: UIImage, toHeight: CGFloat) -> UIImage {
  90. return autoreleasepool { () -> UIImage in
  91. let toWidth = image.size.width * (toHeight/image.size.height)
  92. let targetSize = CGSize(width: toWidth, height: toHeight)
  93. let size = image.size
  94. let widthRatio = targetSize.width / size.width
  95. let heightRatio = targetSize.height / size.height
  96. // Orientation detection
  97. var newSize: CGSize
  98. if(widthRatio > heightRatio) {
  99. newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
  100. } else {
  101. newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
  102. }
  103. // Calculated rect
  104. let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  105. // Resize
  106. UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
  107. image.draw(in: rect)
  108. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  109. UIGraphicsEndImageContext()
  110. return newImage!
  111. }
  112. }
  113. func cellBlurEffect(with frame: CGRect) -> UIView {
  114. let blurEffect = UIBlurEffect(style: .extraLight)
  115. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  116. blurEffectView.frame = frame
  117. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  118. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brandElement.withAlphaComponent(0.2)
  119. return blurEffectView
  120. }
  121. func setLayoutForView(key: String, serverUrl: String, layout: String, sort: String, ascending: Bool, groupBy: String, directoryOnTop: Bool, titleButton: String, itemForLine: Int) {
  122. let string = layout + "|" + sort + "|" + "\(ascending)" + "|" + groupBy + "|" + "\(directoryOnTop)" + "|" + titleButton + "|" + "\(itemForLine)"
  123. let key = key + serverUrl
  124. UICKeyChainStore.setString(string, forKey: key, service: k_serviceShareKeyChain)
  125. }
  126. func setLayoutForView(key: String, serverUrl: String, layout: String) {
  127. var sort: String
  128. var ascending: Bool
  129. var groupBy: String
  130. var directoryOnTop: Bool
  131. var titleButton: String
  132. var itemForLine: Int
  133. (_, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_favorite, serverUrl: serverUrl)
  134. setLayoutForView(key: key, serverUrl: serverUrl, layout: layout, sort: sort, ascending: ascending, groupBy: groupBy, directoryOnTop: directoryOnTop, titleButton: titleButton, itemForLine: itemForLine)
  135. }
  136. @objc func getLayoutForView(key: String, serverUrl: String) -> (String) {
  137. var layout: String
  138. (layout, _, _, _, _, _, _) = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  139. return layout
  140. }
  141. @objc func getSortedForView(key: String, serverUrl: String) -> (String) {
  142. var sort: String
  143. (_, sort, _, _, _, _, _) = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  144. return sort
  145. }
  146. @objc func getAscendingForView(key: String, serverUrl: String) -> (Bool) {
  147. var ascending: Bool
  148. (_, _, ascending, _, _, _, _) = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  149. return ascending
  150. }
  151. @objc func getGroupByForView(key: String, serverUrl: String) -> (String) {
  152. var groupBy: String
  153. (_, _, _, groupBy, _, _, _) = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  154. return groupBy
  155. }
  156. @objc func getDirectoryOnTopForView(key: String, serverUrl: String) -> (Bool) {
  157. var directoryOnTop: Bool
  158. (_, _, _, _, directoryOnTop, _, _) = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  159. return directoryOnTop
  160. }
  161. @objc func getTitleButtonForView(key: String, serverUrl: String) -> (String) {
  162. var titleButton: String
  163. (_, _, _, _, _, titleButton, _) = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl)
  164. return titleButton
  165. }
  166. func getLayoutForView(key: String, serverUrl: String) -> (layout: String, sort: String, ascending: Bool, groupBy: String, directoryOnTop: Bool, titleButton: String, itemForLine: Int) {
  167. let key = key + serverUrl
  168. guard let string = UICKeyChainStore.string(forKey: key, service: k_serviceShareKeyChain) else {
  169. setLayoutForView(key: key, serverUrl: serverUrl, layout: k_layout_list, sort: "fileName", ascending: true, groupBy: "none", directoryOnTop: true, titleButton: "_sorted_by_name_a_z_", itemForLine: 3)
  170. return (k_layout_list, "fileName", true, "none", true, "_sorted_by_name_a_z_", 3)
  171. }
  172. let array = string.components(separatedBy: "|")
  173. if array.count == 7 {
  174. let sort = NSString(string: array[2])
  175. let directoryOnTop = NSString(string: array[4])
  176. let itemForLine = NSString(string: array[6])
  177. return (array[0], array[1], sort.boolValue, array[3], directoryOnTop.boolValue, array[5], Int(itemForLine.intValue))
  178. }
  179. setLayoutForView(key: key, serverUrl: serverUrl, layout: k_layout_list, sort: "fileName", ascending: true, groupBy: "none", directoryOnTop: true, titleButton: "_sorted_by_name_a_z_", itemForLine: 3)
  180. return (k_layout_list, "fileName", true, "none", true, "_sorted_by_name_a_z_", 3)
  181. }
  182. func convertSVGtoPNGWriteToUserData(svgUrlString: String, fileName: String?, width: CGFloat?, rewrite: Bool, account: String, closure: @escaping (String?) -> ()) {
  183. var fileNamePNG = ""
  184. guard let svgUrlString = svgUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  185. return closure(nil)
  186. }
  187. guard let iconURL = URL(string: svgUrlString) else {
  188. return closure(nil)
  189. }
  190. if fileName == nil {
  191. fileNamePNG = iconURL.deletingPathExtension().lastPathComponent + ".png"
  192. } else {
  193. fileNamePNG = fileName!
  194. }
  195. let imageNamePath = CCUtility.getDirectoryUserData() + "/" + fileNamePNG
  196. if !FileManager.default.fileExists(atPath: imageNamePath) || rewrite == true {
  197. NCCommunication.shared.downloadContent(serverUrl: iconURL.absoluteString) { (account, data, errorCode, errorMessage) in
  198. if errorCode == 0 && data != nil {
  199. if let image = UIImage.init(data: data!) {
  200. var newImage: UIImage = image
  201. if width != nil {
  202. let ratio = image.size.height / image.size.width
  203. let newSize = CGSize(width: width!, height: width! * ratio)
  204. let renderFormat = UIGraphicsImageRendererFormat.default()
  205. renderFormat.opaque = false
  206. let renderer = UIGraphicsImageRenderer(size: CGSize(width: newSize.width, height: newSize.height), format: renderFormat)
  207. newImage = renderer.image {
  208. (context) in
  209. image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  210. }
  211. }
  212. guard let pngImageData = newImage.pngData() else {
  213. return closure(nil)
  214. }
  215. CCUtility.write(pngImageData, fileNamePath: imageNamePath)
  216. return closure(imageNamePath)
  217. } else {
  218. guard let svgImage: SVGKImage = SVGKImage(data: data) else {
  219. return closure(nil)
  220. }
  221. if width != nil {
  222. let scale = svgImage.size.height / svgImage.size.width
  223. svgImage.size = CGSize(width: width!, height: width! * scale)
  224. }
  225. guard let image: UIImage = svgImage.uiImage else {
  226. return closure(nil)
  227. }
  228. guard let pngImageData = image.pngData() else {
  229. return closure(nil)
  230. }
  231. CCUtility.write(pngImageData, fileNamePath: imageNamePath)
  232. return closure(imageNamePath)
  233. }
  234. } else {
  235. return closure(nil)
  236. }
  237. }
  238. } else {
  239. return closure(imageNamePath)
  240. }
  241. }
  242. @objc func startActivityIndicator(view: UIView?, bottom: CGFloat = 0) {
  243. guard let view = view else { return }
  244. activityIndicator.color = NCBrandColor.sharedInstance.brandElement
  245. activityIndicator.hidesWhenStopped = true
  246. view.addSubview(activityIndicator)
  247. activityIndicator.translatesAutoresizingMaskIntoConstraints = false
  248. let horizontalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
  249. view.addConstraint(horizontalConstraint)
  250. var verticalConstant: CGFloat = 0
  251. if bottom > 0 {
  252. verticalConstant = (view.frame.size.height / 2) - bottom
  253. }
  254. let verticalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: verticalConstant)
  255. view.addConstraint(verticalConstraint)
  256. activityIndicator.startAnimating()
  257. }
  258. @objc func stopActivityIndicator() {
  259. activityIndicator.stopAnimating()
  260. activityIndicator.removeFromSuperview()
  261. }
  262. @objc func isSimulatorOrTestFlight() -> Bool {
  263. guard let path = Bundle.main.appStoreReceiptURL?.path else {
  264. return false
  265. }
  266. return path.contains("CoreSimulator") || path.contains("sandboxReceipt")
  267. }
  268. @objc func formatSecondsToString(_ seconds: TimeInterval) -> String {
  269. if seconds.isNaN {
  270. return "00:00:00"
  271. }
  272. let sec = Int(seconds.truncatingRemainder(dividingBy: 60))
  273. let min = Int(seconds.truncatingRemainder(dividingBy: 3600) / 60)
  274. let hour = Int(seconds / 3600)
  275. return String(format: "%02d:%02d:%02d", hour, min, sec)
  276. }
  277. @objc func blink(cell: AnyObject?) {
  278. DispatchQueue.main.async {
  279. if let cell = cell as? UITableViewCell {
  280. cell.backgroundColor = NCBrandColor.sharedInstance.brandElement.withAlphaComponent(0.3)
  281. UIView.animate(withDuration: 2) {
  282. cell.backgroundColor = .clear
  283. }
  284. } else if let cell = cell as? UICollectionViewCell {
  285. cell.backgroundColor = NCBrandColor.sharedInstance.brandElement.withAlphaComponent(0.3)
  286. UIView.animate(withDuration: 2) {
  287. cell.backgroundColor = .clear
  288. }
  289. }
  290. }
  291. }
  292. @objc func isRichDocument(_ metadata: tableMetadata) -> Bool {
  293. guard let mimeType = CCUtility.getMimeType(metadata.fileNameView) else {
  294. return false
  295. }
  296. guard let richdocumentsMimetypes = NCManageDatabase.sharedInstance.getCapabilitiesServerArray(account: metadata.account, elements: NCElementsJSON.shared.capabilitiesRichdocumentsMimetypes) else {
  297. return false
  298. }
  299. if richdocumentsMimetypes.count > 0 && mimeType.components(separatedBy: ".").count > 2 {
  300. let mimeTypeArray = mimeType.components(separatedBy: ".")
  301. let mimeType = mimeTypeArray[mimeTypeArray.count - 2] + "." + mimeTypeArray[mimeTypeArray.count - 1]
  302. for richdocumentMimetype: String in richdocumentsMimetypes {
  303. if richdocumentMimetype.contains(mimeType) {
  304. return true
  305. }
  306. }
  307. }
  308. return false
  309. }
  310. @objc func isDirectEditing(account: String, contentType: String) -> String? {
  311. var editor: String?
  312. guard let results = NCManageDatabase.sharedInstance.getDirectEditingEditors(account: account) else {
  313. return editor
  314. }
  315. for result: tableDirectEditingEditors in results {
  316. for mimetype in result.mimetypes {
  317. if mimetype == contentType {
  318. editor = result.editor
  319. }
  320. // HARDCODE
  321. // https://github.com/nextcloud/text/issues/913
  322. if mimetype == "text/markdown" && contentType == "text/x-markdown" {
  323. editor = result.editor
  324. }
  325. }
  326. for mimetype in result.optionalMimetypes {
  327. if mimetype == contentType {
  328. editor = result.editor
  329. }
  330. }
  331. }
  332. // HARDCODE
  333. if editor == "" {
  334. editor = k_editor_text
  335. }
  336. return editor
  337. }
  338. @objc func removeAllSettings() {
  339. URLCache.shared.memoryCapacity = 0
  340. URLCache.shared.diskCapacity = 0
  341. KTVHTTPCache.cacheDeleteAllCaches()
  342. NCManageDatabase.sharedInstance.clearDatabase(account: nil, removeAccount: true)
  343. CCUtility.removeGroupDirectoryProviderStorage()
  344. CCUtility.removeGroupLibraryDirectory()
  345. CCUtility.removeDocumentsDirectory()
  346. CCUtility.removeTemporaryDirectory()
  347. CCUtility.createDirectoryStandard()
  348. CCUtility.deleteAllChainStore()
  349. }
  350. #if !EXTENSION
  351. @objc func createAvatar(fileNameSource: String, fileNameSourceAvatar: String) -> UIImage? {
  352. guard let imageSource = UIImage(contentsOfFile: fileNameSource) else { return nil }
  353. let size = Int(k_avatar_size)
  354. UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, UIScreen.main.scale)
  355. imageSource.draw(in: CGRect(x: 0, y: 0, width: size, height: size))
  356. let image = UIGraphicsGetImageFromCurrentImageContext()
  357. UIGraphicsEndImageContext()
  358. UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, UIScreen.main.scale)
  359. let avatarImageView = CCAvatar.init(image: image, borderColor: .lightGray, borderWidth: Float(1 * UIScreen.main.scale))
  360. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  361. avatarImageView?.layer.render(in: context)
  362. guard let imageAvatar = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
  363. UIGraphicsEndImageContext()
  364. guard let data = imageAvatar.pngData() else {
  365. return nil
  366. }
  367. do {
  368. try data.write(to: NSURL(fileURLWithPath: fileNameSourceAvatar) as URL, options: .atomic)
  369. } catch { }
  370. return imageAvatar
  371. }
  372. #endif
  373. @objc func UIColorFromRGB(rgbValue: UInt32) -> UIColor {
  374. return UIColor(
  375. red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
  376. green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
  377. blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
  378. alpha: CGFloat(1.0)
  379. )
  380. }
  381. @objc func RGBFromUIColor(uicolorValue: UIColor) -> UInt32 {
  382. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  383. if uicolorValue.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
  384. var colorAsUInt : UInt32 = 0
  385. colorAsUInt += UInt32(red * 255.0) << 16 +
  386. UInt32(green * 255.0) << 8 +
  387. UInt32(blue * 255.0)
  388. return colorAsUInt
  389. }
  390. return 0
  391. }
  392. @objc func permissionsContainsString(_ metadataPermissions: String, permissions: String) -> Bool {
  393. for char in permissions {
  394. if metadataPermissions.contains(char) == false {
  395. return false
  396. }
  397. }
  398. return true
  399. }
  400. @objc func getCustomUserAgentOnlyOffice() -> String {
  401. let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")!
  402. if UIDevice.current.userInterfaceIdiom == .pad {
  403. return "Mozilla/5.0 (iPad) Nextcloud-iOS/\(appVersion)"
  404. }else{
  405. return "Mozilla/5.0 (iPhone) Mobile Nextcloud-iOS/\(appVersion)"
  406. }
  407. }
  408. @objc func pdfThumbnail(url: URL, width: CGFloat = 240) -> UIImage? {
  409. guard let data = try? Data(contentsOf: url), let page = PDFDocument(data: data)?.page(at: 0) else {
  410. return nil
  411. }
  412. let pageSize = page.bounds(for: .mediaBox)
  413. let pdfScale = width / pageSize.width
  414. // Apply if you're displaying the thumbnail on screen
  415. let scale = UIScreen.main.scale * pdfScale
  416. let screenSize = CGSize(width: pageSize.width * scale, height: pageSize.height * scale)
  417. return page.thumbnail(of: screenSize, for: .mediaBox)
  418. }
  419. @objc func getMetadataConflict(account: String, serverUrl: String, fileName: String) -> tableMetadata? {
  420. // verify exists conflict
  421. let fileNameExtension = (fileName as NSString).pathExtension.lowercased()
  422. let fileNameWithoutExtension = (fileName as NSString).deletingPathExtension
  423. var fileNameConflict = fileName
  424. if fileNameExtension == "heic" && CCUtility.getFormatCompatibility() {
  425. fileNameConflict = fileNameWithoutExtension + ".jpg"
  426. }
  427. return NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView == %@", account, serverUrl, fileNameConflict))
  428. }
  429. @objc func isQuickLookDisplayable(metadata: tableMetadata) -> Bool {
  430. return true
  431. }
  432. @objc func fromColor(color: UIColor) -> UIImage {
  433. let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
  434. UIGraphicsBeginImageContext(rect.size)
  435. let context: CGContext? = UIGraphicsGetCurrentContext()
  436. context?.setFillColor(color.cgColor)
  437. context?.fill(rect)
  438. let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
  439. UIGraphicsEndImageContext()
  440. return image ?? UIImage()
  441. }
  442. // Delete Asset on Photos album
  443. @objc func deleteAssetLocalIdentifiers(account: String, sessionSelector: String, completition: @escaping () -> ()) {
  444. if UIApplication.shared.applicationState != .active {
  445. completition()
  446. return
  447. }
  448. let metadatasSessionUpload = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND session CONTAINS[cd] %@", account, "upload"))
  449. if metadatasSessionUpload.count > 0 {
  450. completition()
  451. return
  452. }
  453. let localIdentifiers = NCManageDatabase.sharedInstance.getAssetLocalIdentifiersUploaded(account: account, sessionSelector: sessionSelector)
  454. if localIdentifiers.count == 0 {
  455. completition()
  456. return
  457. }
  458. let assets = PHAsset.fetchAssets(withLocalIdentifiers: localIdentifiers, options: nil)
  459. PHPhotoLibrary.shared().performChanges({
  460. PHAssetChangeRequest.deleteAssets(assets as NSFastEnumeration)
  461. }, completionHandler: { success, error in
  462. DispatchQueue.main.async {
  463. NCManageDatabase.sharedInstance.clearAssetLocalIdentifiers(localIdentifiers, account: account)
  464. completition()
  465. }
  466. })
  467. }
  468. @objc func ocIdToFileId(ocId: String?) -> String? {
  469. guard let ocId = ocId else { return nil }
  470. let items = ocId.components(separatedBy: "oc")
  471. if items.count < 2 { return nil }
  472. guard let intFileId = Int(items[0]) else { return nil }
  473. return String(intFileId)
  474. }
  475. }