NCViewerQuickLook.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // NCViewerQuickLook.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/05/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. // Copyright © 2022 Henrik Storch. All rights reserved.
  8. // Copyright © 2023 Marino Faggiana. All rights reserved.
  9. //
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. // Author Henrik Storch <henrik.storch@nextcloud.com>
  12. //
  13. // This program is free software: you can redistribute it and/or modify
  14. // it under the terms of the GNU General Public License as published by
  15. // the Free Software Foundation, either version 3 of the License, or
  16. // (at your option) any later version.
  17. //
  18. // This program is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. // GNU General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU General Public License
  24. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. //
  26. import UIKit
  27. import QuickLook
  28. import NextcloudKit
  29. import Mantis
  30. protocol NCViewerQuickLookDelegate: AnyObject {
  31. func dismiss(url: URL, hasChanges: Bool)
  32. }
  33. @objc class NCViewerQuickLook: QLPreviewController {
  34. let url: URL
  35. var previewItems: [PreviewItem] = []
  36. var isEditingEnabled: Bool
  37. var metadata: tableMetadata?
  38. var delegateViewer: NCViewerQuickLookDelegate?
  39. // if the document has any changes (annotations)
  40. var hasChanges = false
  41. // used to display the save alert
  42. var parentVC: UIViewController?
  43. // if the Crop is presented
  44. var isPresentCrop = false
  45. required init?(coder: NSCoder) {
  46. fatalError("init(coder:) has not been implemented")
  47. }
  48. @objc init(with url: URL, isEditingEnabled: Bool, metadata: tableMetadata?) {
  49. self.url = url
  50. self.isEditingEnabled = isEditingEnabled
  51. if let metadata = metadata {
  52. self.metadata = tableMetadata.init(value: metadata)
  53. }
  54. let previewItem = PreviewItem()
  55. previewItem.previewItemURL = url
  56. self.previewItems.append(previewItem)
  57. super.init(nibName: nil, bundle: nil)
  58. self.dataSource = self
  59. self.delegate = self
  60. self.currentPreviewItemIndex = 0
  61. }
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. guard isEditingEnabled else { return }
  65. if metadata?.livePhoto == true {
  66. let error = NKError(errorCode: NCGlobal.shared.errorCharactersForbidden, errorDescription: "_message_disable_overwrite_livephoto_")
  67. NCContentPresenter.shared.showInfo(error: error)
  68. }
  69. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(crop))
  70. }
  71. override func viewDidAppear(_ animated: Bool) {
  72. super.viewDidAppear(animated)
  73. // needs to be saved bc in didDisappear presentingVC is already nil
  74. self.parentVC = presentingViewController
  75. }
  76. override func viewDidDisappear(_ animated: Bool) {
  77. super.viewDidDisappear(animated)
  78. guard !isPresentCrop else { return }
  79. delegateViewer?.dismiss(url: url, hasChanges: hasChanges)
  80. guard isEditingEnabled, hasChanges, let metadata = metadata else { return }
  81. let alertController = UIAlertController(title: NSLocalizedString("_save_", comment: ""), message: nil, preferredStyle: .alert)
  82. var message: String?
  83. if metadata.livePhoto {
  84. message = NSLocalizedString("_message_disable_overwrite_livephoto_", comment: "")
  85. } else if metadata.lock {
  86. message = NSLocalizedString("_file_locked_no_override_", comment: "")
  87. } else {
  88. alertController.addAction(UIAlertAction(title: NSLocalizedString("_overwrite_original_", comment: ""), style: .default) { _ in
  89. self.saveModifiedFile(override: true)
  90. })
  91. }
  92. alertController.message = message
  93. alertController.addAction(UIAlertAction(title: NSLocalizedString("_save_as_copy_", comment: ""), style: .default) { _ in
  94. self.saveModifiedFile(override: false)
  95. })
  96. alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive) { _ in })
  97. parentVC?.present(alertController, animated: true)
  98. }
  99. @objc func crop() {
  100. guard let image = UIImage(contentsOfFile: url.path) else { return }
  101. let config = Mantis.Config()
  102. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  103. config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
  104. config.localizationConfig.tableName = "Localizable"
  105. }
  106. let cropViewController = Mantis.cropViewController(image: image, config: config)
  107. cropViewController.delegate = self
  108. cropViewController.modalPresentationStyle = .fullScreen
  109. self.isPresentCrop = true
  110. self.present(cropViewController, animated: true)
  111. }
  112. }
  113. extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerDelegate {
  114. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  115. previewItems.count
  116. }
  117. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  118. previewItems[index]
  119. }
  120. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  121. return isEditingEnabled ? .createCopy : .disabled
  122. }
  123. fileprivate func saveModifiedFile(override: Bool) {
  124. guard let metadata = self.metadata else { return }
  125. let ocId = NSUUID().uuidString
  126. let size = NCUtilityFileSystem.shared.getFileSize(filePath: url.path)
  127. if !override {
  128. let fileName = NCUtilityFileSystem.shared.createFileName(metadata.fileNameView, serverUrl: metadata.serverUrl, account: metadata.account)
  129. metadata.fileName = fileName
  130. metadata.fileNameView = fileName
  131. }
  132. guard let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: metadata.fileNameView),
  133. NCUtilityFileSystem.shared.copyFile(atPath: url.path, toPath: fileNamePath) else { return }
  134. let metadataForUpload = NCManageDatabase.shared.createMetadata(
  135. account: metadata.account,
  136. user: metadata.user,
  137. userId: metadata.userId,
  138. fileName: metadata.fileName,
  139. fileNameView: metadata.fileNameView,
  140. ocId: ocId,
  141. serverUrl: metadata.serverUrl,
  142. urlBase: metadata.urlBase,
  143. url: url.path,
  144. contentType: "")
  145. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  146. if override {
  147. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFileNODelete
  148. } else {
  149. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  150. }
  151. metadataForUpload.size = size
  152. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  153. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: [metadataForUpload]) { _ in }
  154. }
  155. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  156. // easier to handle that way than to use `.updateContents`
  157. // needs to be moved otherwise it will only be called once!
  158. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: url.path) else { return }
  159. hasChanges = true
  160. }
  161. }
  162. extension NCViewerQuickLook: CropViewControllerDelegate {
  163. func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
  164. cropViewController.dismiss(animated: true) {
  165. self.isPresentCrop = false
  166. }
  167. guard let data = cropped.jpegData(compressionQuality: 1) else { return }
  168. do {
  169. try data.write(to: self.url)
  170. reloadData()
  171. } catch { }
  172. }
  173. func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
  174. cropViewController.dismiss(animated: true) {
  175. self.isPresentCrop = false
  176. }
  177. }
  178. }
  179. class PreviewItem: NSObject, QLPreviewItem {
  180. var previewItemURL: URL?
  181. }