NCViewerQuickLook.swift 8.0 KB

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