NCCollectionCommon.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. //
  2. // NCCollectionCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/09/2020.
  6. // Copyright © 2020 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 NCCommunication
  25. class NCCollectionCommon: NSObject, NCSelectDelegate {
  26. @objc static let shared: NCCollectionCommon = {
  27. let instance = NCCollectionCommon()
  28. instance.createImagesThemingColor()
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. struct images {
  33. static var file = UIImage()
  34. static var shared = UIImage()
  35. static var canShare = UIImage()
  36. static var shareByLink = UIImage()
  37. static var favorite = UIImage()
  38. static var comment = UIImage()
  39. static var livePhoto = UIImage()
  40. static var offlineFlag = UIImage()
  41. static var local = UIImage()
  42. static var folderEncrypted = UIImage()
  43. static var folderSharedWithMe = UIImage()
  44. static var folderPublic = UIImage()
  45. static var folderGroup = UIImage()
  46. static var folderExternal = UIImage()
  47. static var folderAutomaticUpload = UIImage()
  48. static var folder = UIImage()
  49. static var checkedYes = UIImage()
  50. static var checkedNo = UIImage()
  51. static var buttonMore = UIImage()
  52. static var buttonStop = UIImage()
  53. }
  54. // MARK: -
  55. @objc func createImagesThemingColor() {
  56. images.file = UIImage.init(named: "file")!
  57. images.shared = UIImage(named: "share")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  58. images.canShare = UIImage(named: "share")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  59. images.shareByLink = UIImage(named: "sharebylink")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  60. images.favorite = UIImage(named: "favorite")!.image(color: NCBrandColor.shared.yellowFavorite, size: 50)
  61. images.comment = UIImage(named: "comment")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  62. images.livePhoto = UIImage(named: "livePhoto")!.image(color: NCBrandColor.shared.textView, size: 50)
  63. images.offlineFlag = UIImage.init(named: "offlineFlag")!
  64. images.local = UIImage.init(named: "local")!
  65. let folderWidth: CGFloat = UIScreen.main.bounds.width / 3
  66. images.folderEncrypted = UIImage(named: "folderEncrypted")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  67. images.folderSharedWithMe = UIImage(named: "folder_shared_with_me")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  68. images.folderPublic = UIImage(named: "folder_public")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  69. images.folderGroup = UIImage(named: "folder_group")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  70. images.folderExternal = UIImage(named: "folder_external")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  71. images.folderAutomaticUpload = UIImage(named: "folderAutomaticUpload")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  72. images.folder = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: folderWidth)
  73. images.checkedYes = UIImage(named: "checkedYes")!.image(color: .darkGray, size: 50)
  74. images.checkedNo = UIImage(named: "checkedNo")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  75. images.buttonMore = UIImage(named: "more")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  76. images.buttonStop = UIImage(named: "stop")!.image(color: NCBrandColor.shared.graySoft, size: 50)
  77. }
  78. @objc func loadImage(named: String, color: UIColor = NCBrandColor.shared.icon, size: CGFloat = 50, forcedResizeSF: Bool = false) -> UIImage {
  79. var image: UIImage?
  80. if #available(iOS 13.0, *) {
  81. if forcedResizeSF {
  82. let configuration = UIImage.SymbolConfiguration(pointSize: size)
  83. image = UIImage(systemName: named, withConfiguration: configuration)?.imageColor(color)
  84. } else {
  85. image = UIImage(systemName: named)?.imageColor(color)
  86. }
  87. if image == nil {
  88. image = UIImage(named: named)?.image(color: color, size: size)
  89. }
  90. } else {
  91. image = UIImage(named: named)?.image(color: color, size: size)
  92. }
  93. if image != nil {
  94. return image!
  95. }
  96. return UIImage(named: "file")!.image(color: color, size: size)
  97. }
  98. // MARK: - NCSelect + Delegate
  99. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], buttonType: String, overwrite: Bool) {
  100. if (serverUrl != nil && items.count > 0) {
  101. var move = true
  102. if buttonType == "done1" { move = false }
  103. for metadata in items as! [tableMetadata] {
  104. NCOperationQueue.shared.copyMove(metadata: metadata, serverUrl: serverUrl!, overwrite: overwrite, move: move)
  105. }
  106. }
  107. }
  108. func openSelectView(items: [Any], viewController: UIViewController) {
  109. let navigationController = UIStoryboard.init(name: "NCSelect", bundle: nil).instantiateInitialViewController() as! UINavigationController
  110. let topViewController = navigationController.topViewController as! NCSelect
  111. var listViewController = [NCSelect]()
  112. var copyItems: [Any] = []
  113. for item in items {
  114. copyItems.append(item)
  115. }
  116. let homeUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  117. var serverUrl = (copyItems[0] as! Nextcloud.tableMetadata).serverUrl
  118. // Setup view controllers such that the current view is of the same directory the items to be copied are in
  119. while true {
  120. // If not in the topmost directory, create a new view controller and set correct title.
  121. // If in the topmost directory, use the default view controller as the base.
  122. var viewController: NCSelect?
  123. if serverUrl != homeUrl {
  124. viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect
  125. if viewController == nil {
  126. return
  127. }
  128. viewController!.titleCurrentFolder = (serverUrl as NSString).lastPathComponent
  129. } else {
  130. viewController = topViewController
  131. }
  132. guard let vc = viewController else { return }
  133. vc.delegate = self
  134. vc.hideButtonCreateFolder = false
  135. vc.selectFile = false
  136. vc.includeDirectoryE2EEncryption = false
  137. vc.includeImages = false
  138. vc.type = ""
  139. vc.titleButtonDone = NSLocalizedString("_move_", comment: "")
  140. vc.titleButtonDone1 = NSLocalizedString("_copy_",comment: "")
  141. vc.isButtonDone1Hide = false
  142. vc.isOverwriteHide = false
  143. vc.items = copyItems
  144. vc.serverUrl = serverUrl
  145. vc.navigationItem.backButtonTitle = vc.titleCurrentFolder
  146. listViewController.insert(vc, at: 0)
  147. if serverUrl != homeUrl {
  148. serverUrl = NCUtilityFileSystem.shared.deletingLastPathComponent(serverUrl: serverUrl, urlBase: appDelegate.urlBase, account: appDelegate.account)
  149. } else {
  150. break
  151. }
  152. }
  153. navigationController.setViewControllers(listViewController, animated: false)
  154. navigationController.modalPresentationStyle = .formSheet
  155. viewController.present(navigationController, animated: true, completion: nil)
  156. }
  157. @objc func openFileViewInFolder(serverUrl: String, fileName: String) {
  158. let viewController = UIStoryboard(name: "NCFileViewInFolder", bundle: nil).instantiateInitialViewController() as! NCFileViewInFolder
  159. let navigationController = UINavigationController.init(rootViewController: viewController)
  160. let topViewController = viewController
  161. var listViewController = [NCFileViewInFolder]()
  162. var serverUrl = serverUrl
  163. let homeUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  164. while true {
  165. var viewController: NCFileViewInFolder?
  166. if serverUrl != homeUrl {
  167. viewController = UIStoryboard(name: "NCFileViewInFolder", bundle: nil).instantiateInitialViewController() as? NCFileViewInFolder
  168. if viewController == nil {
  169. return
  170. }
  171. viewController!.titleCurrentFolder = (serverUrl as NSString).lastPathComponent
  172. } else {
  173. viewController = topViewController
  174. }
  175. guard let vc = viewController else { return }
  176. vc.serverUrl = serverUrl
  177. vc.fileName = fileName
  178. vc.navigationItem.backButtonTitle = vc.titleCurrentFolder
  179. listViewController.insert(vc, at: 0)
  180. if serverUrl != homeUrl {
  181. serverUrl = NCUtilityFileSystem.shared.deletingLastPathComponent(serverUrl: serverUrl, urlBase: appDelegate.urlBase, account: appDelegate.account)
  182. } else {
  183. break
  184. }
  185. }
  186. navigationController.setViewControllers(listViewController, animated: false)
  187. navigationController.modalPresentationStyle = .formSheet
  188. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  189. }
  190. // MARK: - Save Photo - Video - Live Photo
  191. func saveAlbum(metadata: tableMetadata) {
  192. let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  193. let status = PHPhotoLibrary.authorizationStatus()
  194. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage && status == PHAuthorizationStatus.authorized {
  195. if let image = UIImage.init(contentsOfFile: fileNamePath) {
  196. UIImageWriteToSavedPhotosAlbum(image, self, #selector(SaveAlbum(_:didFinishSavingWithError:contextInfo:)), nil)
  197. } else {
  198. NCContentPresenter.shared.messageNotification("_save_selected_files_", description: "_file_not_saved_cameraroll_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorFileNotSaved)
  199. }
  200. } else if metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo && status == PHAuthorizationStatus.authorized {
  201. if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(fileNamePath) {
  202. UISaveVideoAtPathToSavedPhotosAlbum(fileNamePath, self, #selector(SaveAlbum(_:didFinishSavingWithError:contextInfo:)), nil)
  203. } else {
  204. NCContentPresenter.shared.messageNotification("_save_selected_files_", description: "_file_not_saved_cameraroll_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorFileNotSaved)
  205. }
  206. } else if status != PHAuthorizationStatus.authorized {
  207. NCContentPresenter.shared.messageNotification("_access_photo_not_enabled_", description: "_access_photo_not_enabled_msg_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorFileNotSaved)
  208. }
  209. }
  210. @objc private func SaveAlbum(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
  211. if error != nil {
  212. NCContentPresenter.shared.messageNotification("_save_selected_files_", description: "_file_not_saved_cameraroll_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorFileNotSaved)
  213. }
  214. }
  215. func saveLivePhoto(metadata: tableMetadata, metadataMOV: tableMetadata) {
  216. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  217. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbumLivePhotoIMG)
  218. }
  219. if !CCUtility.fileProviderStorageExists(metadataMOV.ocId, fileNameView: metadataMOV.fileNameView) {
  220. NCOperationQueue.shared.download(metadata: metadataMOV, selector: NCGlobal.shared.selectorSaveAlbumLivePhotoMOV)
  221. }
  222. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && CCUtility.fileProviderStorageExists(metadataMOV.ocId, fileNameView: metadataMOV.fileNameView) {
  223. saveLivePhotoToDisk(metadata: metadata, metadataMov: metadataMOV, progressView: nil, viewActivity: self.appDelegate.window?.rootViewController?.view)
  224. }
  225. }
  226. func saveLivePhotoToDisk(metadata: tableMetadata, metadataMov: tableMetadata, progressView: UIProgressView?, viewActivity: UIView?) {
  227. let fileNameImage = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  228. let fileNameMov = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView)!)
  229. if let view = viewActivity {
  230. NCUtility.shared.startActivityIndicator(view: view)
  231. }
  232. NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
  233. DispatchQueue.main.async {
  234. progressView?.progress = Float(progress)
  235. }
  236. }, completion: { livePhoto, resources in
  237. NCUtility.shared.stopActivityIndicator()
  238. progressView?.progress = 0
  239. if resources != nil {
  240. NCLivePhoto.saveToLibrary(resources!) { (result) in
  241. if !result {
  242. NCContentPresenter.shared.messageNotification("_error_", description: "_livephoto_save_error_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorInternalError)
  243. }
  244. }
  245. } else {
  246. NCContentPresenter.shared.messageNotification("_error_", description: "_livephoto_save_error_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorInternalError)
  247. }
  248. })
  249. }
  250. // MARK: - Print
  251. func printDocument(metadata: tableMetadata) {
  252. let fileNameURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  253. if UIPrintInteractionController.canPrint(fileNameURL) {
  254. let printInfo = UIPrintInfo(dictionary: nil)
  255. printInfo.jobName = fileNameURL.lastPathComponent
  256. printInfo.outputType = .photo
  257. let printController = UIPrintInteractionController.shared
  258. printController.printInfo = printInfo
  259. printController.showsNumberOfCopies = true
  260. printController.printingItem = fileNameURL
  261. printController.present(animated: true, completionHandler: nil)
  262. }
  263. }
  264. // MARK: - Context Menu Configuration
  265. @available(iOS 13.0, *)
  266. func contextMenuConfiguration(metadata: tableMetadata, viewController: UIViewController, enableDeleteLocal: Bool, enableViewInFolder: Bool) -> UIMenu {
  267. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  268. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  269. var titleSave: String = NSLocalizedString("_save_selected_files_", comment: "")
  270. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  271. if metadataMOV != nil {
  272. titleSave = NSLocalizedString("_livephoto_save_", comment: "")
  273. }
  274. let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc") ) { action in
  275. self.appDelegate.pasteboardOcIds = [metadata.ocId]
  276. self.copyPasteboard()
  277. }
  278. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""), image: UIImage(systemName: "info") ) { action in
  279. NCNetworkingNotificationCenter.shared.openShare(ViewController: viewController, metadata: metadata, indexPage: 0)
  280. }
  281. let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { action in
  282. if metadataMOV != nil {
  283. NCCollectionCommon.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  284. } else {
  285. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  286. self.saveAlbum(metadata: metadata)
  287. } else {
  288. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  289. }
  290. }
  291. }
  292. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""), image: UIImage(systemName: "arrow.forward.square")) { action in
  293. NCCollectionCommon.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  294. }
  295. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""), image: UIImage(systemName: "square.and.arrow.up") ) { action in
  296. NCNetworkingNotificationCenter.shared.downloadOpen(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  297. }
  298. let openQuickLook = UIAction(title: NSLocalizedString("_open_quicklook_", comment: ""), image: UIImage(systemName: "eye")) { action in
  299. NCNetworkingNotificationCenter.shared.downloadOpen(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  300. }
  301. let open = UIMenu(title: NSLocalizedString("_open_", comment: ""), image: UIImage(systemName: "square.and.arrow.up"), children: [openIn, openQuickLook])
  302. let moveCopy = UIAction(title: NSLocalizedString("_move_or_copy_", comment: ""), image: UIImage(systemName: "arrow.up.right.square")) { action in
  303. NCCollectionCommon.shared.openSelectView(items: [metadata], viewController: viewController)
  304. }
  305. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile, image: UIImage(systemName: "trash"), attributes: .destructive) { action in
  306. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  307. if errorCode != 0 {
  308. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  309. }
  310. }
  311. }
  312. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { action in
  313. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: true) { (errorCode, errorDescription) in
  314. }
  315. }
  316. var delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmLocal, deleteConfirmFile])
  317. if !enableDeleteLocal {
  318. delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  319. }
  320. if metadata.directory {
  321. delete = UIMenu(title: NSLocalizedString("_delete_folder_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  322. }
  323. // ------ MENU -----
  324. if metadata.directory {
  325. return UIMenu(title: "", children: [detail, moveCopy, delete])
  326. }
  327. var children: [UIMenuElement] = [detail, open, moveCopy, copy, delete]
  328. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo {
  329. children.insert(save, at: 2)
  330. }
  331. if enableViewInFolder {
  332. children.insert(viewInFolder, at: 5)
  333. }
  334. return UIMenu(title: "", image: nil, identifier: nil, children: children)
  335. }
  336. // MARK: - Copy & Paste
  337. func copyPasteboard() {
  338. var metadatas: [tableMetadata] = []
  339. var items = [[String : Any]]()
  340. for ocId in appDelegate.pasteboardOcIds {
  341. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  342. metadatas.append(metadata)
  343. }
  344. }
  345. for metadata in metadatas {
  346. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  347. do {
  348. // Get Data
  349. let data = try Data.init(contentsOf: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)))
  350. // Pasteboard item
  351. if let unmanagedFileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (metadata.fileNameView as NSString).pathExtension as CFString, nil) {
  352. let fileUTI = unmanagedFileUTI.takeRetainedValue() as String
  353. items.append([fileUTI:data])
  354. }
  355. } catch {
  356. print("error")
  357. }
  358. } else {
  359. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadCopy) { (_) in }
  360. }
  361. }
  362. UIPasteboard.general.setItems(items, options: [:])
  363. }
  364. func pastePasteboard(serverUrl: String) {
  365. for (index, items) in UIPasteboard.general.items.enumerated() {
  366. for item in items {
  367. let pasteboardType = item.key
  368. if let data = UIPasteboard.general.data(forPasteboardType: pasteboardType, inItemSet: IndexSet([index]))?.first {
  369. let results = NCCommunicationCommon.shared.getDescriptionFile(inUTI: pasteboardType as CFString)
  370. if results.resultTypeFile != NCCommunicationCommon.typeFile.unknow.rawValue {
  371. uploadPasteFile(fileName: results.resultFilename, ext: results.resultExtension, contentType: pasteboardType, serverUrl: serverUrl, data: data)
  372. }
  373. }
  374. }
  375. }
  376. }
  377. private func uploadPasteFile(fileName: String, ext: String, contentType: String, serverUrl: String, data: Data) {
  378. do {
  379. let fileNameView = fileName + "_" + CCUtility.getIncrementalNumber() + "." + ext
  380. let ocId = UUID().uuidString
  381. let filePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileNameView)!
  382. try data.write(to: URL(fileURLWithPath: filePath))
  383. let metadataForUpload = NCManageDatabase.shared.createMetadata(account: appDelegate.account, fileName: fileNameView, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: contentType, livePhoto: false)
  384. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  385. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  386. metadataForUpload.size = NCUtilityFileSystem.shared.getFileSize(filePath: filePath)
  387. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  388. NCManageDatabase.shared.addMetadata(metadataForUpload)
  389. } catch { }
  390. }
  391. }
  392. // MARK: - List Layout
  393. class NCListLayout: UICollectionViewFlowLayout {
  394. var itemHeight: CGFloat = 60
  395. override init() {
  396. super.init()
  397. sectionHeadersPinToVisibleBounds = false
  398. minimumInteritemSpacing = 0
  399. minimumLineSpacing = 1
  400. self.scrollDirection = .vertical
  401. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  402. }
  403. required init?(coder aDecoder: NSCoder) {
  404. fatalError("init(coder:) has not been implemented")
  405. }
  406. override var itemSize: CGSize {
  407. get {
  408. if let collectionView = collectionView {
  409. let itemWidth: CGFloat = collectionView.frame.width
  410. return CGSize(width: itemWidth, height: self.itemHeight)
  411. }
  412. // Default fallback
  413. return CGSize(width: 100, height: 100)
  414. }
  415. set {
  416. super.itemSize = newValue
  417. }
  418. }
  419. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  420. return proposedContentOffset
  421. }
  422. }
  423. // MARK: - Grid Layout
  424. class NCGridLayout: UICollectionViewFlowLayout {
  425. var heightLabelPlusButton: CGFloat = 45
  426. var marginLeftRight: CGFloat = 6
  427. var itemForLine: CGFloat = 3
  428. var itemWidthDefault: CGFloat = 120
  429. override init() {
  430. super.init()
  431. sectionHeadersPinToVisibleBounds = false
  432. minimumInteritemSpacing = 1
  433. minimumLineSpacing = marginLeftRight
  434. self.scrollDirection = .vertical
  435. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  436. }
  437. required init?(coder aDecoder: NSCoder) {
  438. fatalError("init(coder:) has not been implemented")
  439. }
  440. override var itemSize: CGSize {
  441. get {
  442. if let collectionView = collectionView {
  443. if collectionView.frame.width < 400 {
  444. itemForLine = 3
  445. } else {
  446. itemForLine = collectionView.frame.width / itemWidthDefault
  447. }
  448. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  449. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  450. return CGSize(width: itemWidth, height: itemHeight)
  451. }
  452. // Default fallback
  453. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  454. }
  455. set {
  456. super.itemSize = newValue
  457. }
  458. }
  459. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  460. return proposedContentOffset
  461. }
  462. }