NCCreateFormUploadAssets.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // NCCreateFormUploadAssets.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/11/2018.
  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 UIKit
  24. import Queuer
  25. import NextcloudKit
  26. import XLForm
  27. import Photos
  28. class NCCreateFormUploadAssets: XLFormViewController, NCSelectDelegate {
  29. var serverUrl: String = ""
  30. var titleServerUrl: String?
  31. var assets: [PHAsset] = []
  32. var cryptated: Bool = false
  33. var session: String = ""
  34. let requestOptions = PHImageRequestOptions()
  35. var imagePreview: UIImage?
  36. let targetSizeImagePreview = CGSize(width: 100, height: 100)
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. var cellBackgoundColor = UIColor.secondarySystemGroupedBackground
  39. // MARK: - View Life Cycle
  40. convenience init(serverUrl: String, assets: [PHAsset], cryptated: Bool, session: String) {
  41. self.init()
  42. if serverUrl == NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId) {
  43. titleServerUrl = "/"
  44. } else {
  45. if let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
  46. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(tableDirectory.ocId) {
  47. titleServerUrl = metadata.fileNameView
  48. } else { titleServerUrl = (serverUrl as NSString).lastPathComponent }
  49. } else { titleServerUrl = (serverUrl as NSString).lastPathComponent }
  50. }
  51. self.serverUrl = serverUrl
  52. self.assets = assets
  53. self.cryptated = cryptated
  54. self.session = session
  55. requestOptions.resizeMode = PHImageRequestOptionsResizeMode.exact
  56. requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
  57. requestOptions.isSynchronous = true
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. self.title = NSLocalizedString("_upload_photos_videos_", comment: "")
  62. view.backgroundColor = .systemGroupedBackground
  63. tableView.backgroundColor = .systemGroupedBackground
  64. cellBackgoundColor = .secondarySystemGroupedBackground
  65. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  66. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  67. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  68. if assets.count == 1 && assets[0].mediaType == PHAssetMediaType.image {
  69. PHImageManager.default().requestImage(for: assets[0], targetSize: targetSizeImagePreview, contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { image, _ in
  70. self.imagePreview = image
  71. })
  72. }
  73. initializeForm()
  74. reloadForm()
  75. }
  76. // MARK: XLForm
  77. func initializeForm() {
  78. let form: XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  79. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  80. var section: XLFormSectionDescriptor
  81. var row: XLFormRowDescriptor
  82. // Section: Destination Folder
  83. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  84. form.addFormSection(section)
  85. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  86. row.action.formSelector = #selector(changeDestinationFolder(_:))
  87. row.cellConfig["backgroundColor"] = cellBackgoundColor
  88. row.cellConfig["imageView.image"] = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: 25)
  89. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  90. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  91. row.cellConfig["textLabel.textColor"] = UIColor.label
  92. section.addFormRow(row)
  93. // User folder Autoupload
  94. row = XLFormRowDescriptor(tag: "useFolderAutoUpload", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  95. row.value = 0
  96. row.cellConfig["backgroundColor"] = cellBackgoundColor
  97. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  98. row.cellConfig["textLabel.textColor"] = UIColor.label
  99. section.addFormRow(row)
  100. // Use Sub folder
  101. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  102. let activeAccount = NCManageDatabase.shared.getActiveAccount()
  103. if activeAccount?.autoUploadCreateSubfolder == true {
  104. row.value = 1
  105. } else {
  106. row.value = 0
  107. }
  108. row.hidden = "$\("useFolderAutoUpload") == 0"
  109. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  110. row.cellConfig["textLabel.textColor"] = UIColor.label
  111. section.addFormRow(row)
  112. // Section Mode filename
  113. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  114. form.addFormSection(section)
  115. // Maintain the original fileName
  116. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  117. row.value = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal)
  118. row.cellConfig["backgroundColor"] = cellBackgoundColor
  119. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  120. row.cellConfig["textLabel.textColor"] = UIColor.label
  121. section.addFormRow(row)
  122. // Add File Name Type
  123. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  124. row.value = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameType)
  125. row.hidden = "$\("maintainOriginalFileName") == 1"
  126. row.cellConfig["backgroundColor"] = cellBackgoundColor
  127. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  128. row.cellConfig["textLabel.textColor"] = UIColor.label
  129. section.addFormRow(row)
  130. // Section: Rename File Name
  131. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  132. form.addFormSection(section)
  133. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeText, title: (NSLocalizedString("_filename_", comment: "")))
  134. let fileNameMask: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameMask)
  135. if fileNameMask.count > 0 {
  136. row.value = fileNameMask
  137. }
  138. row.hidden = "$\("maintainOriginalFileName") == 1"
  139. row.cellConfig["backgroundColor"] = cellBackgoundColor
  140. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  141. row.cellConfig["textLabel.textColor"] = UIColor.label
  142. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  143. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  144. row.cellConfig["textField.textColor"] = UIColor.label
  145. section.addFormRow(row)
  146. // Section: Preview File Name
  147. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  148. row.height = 180
  149. row.disabled = true
  150. row.cellConfig["backgroundColor"] = cellBackgoundColor
  151. row.cellConfig["textView.backgroundColor"] = cellBackgoundColor
  152. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  153. row.cellConfig["textView.textColor"] = UIColor.label
  154. section.addFormRow(row)
  155. self.form = form
  156. }
  157. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  158. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  159. if formRow.tag == "useFolderAutoUpload" {
  160. if (formRow.value! as AnyObject).boolValue == true {
  161. let buttonDestinationFolder: XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  162. buttonDestinationFolder.hidden = true
  163. } else {
  164. let buttonDestinationFolder: XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  165. buttonDestinationFolder.hidden = false
  166. }
  167. } else if formRow.tag == "useSubFolder" {
  168. if (formRow.value! as AnyObject).boolValue == true {
  169. } else {
  170. }
  171. } else if formRow.tag == "maintainOriginalFileName" {
  172. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: NCGlobal.shared.keyFileNameOriginal)
  173. self.reloadForm()
  174. } else if formRow.tag == "addFileNameType" {
  175. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: NCGlobal.shared.keyFileNameType)
  176. self.reloadForm()
  177. } else if formRow.tag == "maskFileName" {
  178. let fileName = formRow.value as? String
  179. self.form.delegate = nil
  180. if let fileName = fileName {
  181. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  182. }
  183. self.form.delegate = self
  184. let previewFileName: XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  185. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  186. // reload cell
  187. if fileName != nil {
  188. if newValue as! String != formRow.value as! String {
  189. self.reloadFormRow(formRow)
  190. let error = NKError(errorCode: NCGlobal.shared.errorCharactersForbidden, errorDescription: "_forbidden_characters_")
  191. NCContentPresenter.shared.showInfo(error: error)
  192. }
  193. }
  194. self.reloadFormRow(previewFileName)
  195. }
  196. }
  197. func reloadForm() {
  198. self.form.delegate = nil
  199. let buttonDestinationFolder: XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  200. buttonDestinationFolder.title = self.titleServerUrl
  201. let maskFileName: XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  202. let previewFileName: XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  203. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  204. self.tableView.reloadData()
  205. self.form.delegate = self
  206. }
  207. // MARK: - Action
  208. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
  209. if serverUrl != nil {
  210. self.serverUrl = serverUrl!
  211. if serverUrl == NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId) {
  212. self.titleServerUrl = "/"
  213. } else {
  214. if let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, self.serverUrl)) {
  215. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(tableDirectory.ocId) {
  216. titleServerUrl = metadata.fileNameView
  217. } else { titleServerUrl = (self.serverUrl as NSString).lastPathComponent }
  218. } else { titleServerUrl = (self.serverUrl as NSString).lastPathComponent }
  219. }
  220. // Update
  221. let row: XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  222. row.title = self.titleServerUrl
  223. self.updateFormRow(row)
  224. }
  225. }
  226. @objc func save() {
  227. DispatchQueue.global().async {
  228. let useFolderPhotoRow: XLFormRowDescriptor = self.form.formRow(withTag: "useFolderAutoUpload")!
  229. let useSubFolderRow: XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  230. var useSubFolder: Bool = false
  231. var metadatasNOConflict: [tableMetadata] = []
  232. var metadatasUploadInConflict: [tableMetadata] = []
  233. let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId, account: self.appDelegate.account)
  234. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  235. self.serverUrl = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId, account: self.appDelegate.account)
  236. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  237. }
  238. if autoUploadPath == self.serverUrl {
  239. if !NCNetworking.shared.createFolder(assets: self.assets, selector: NCGlobal.shared.selectorUploadFile, useSubFolder: useSubFolder, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId, withPush: false) {
  240. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_createsubfolders_upload_")
  241. NCContentPresenter.shared.showError(error: error)
  242. return
  243. }
  244. }
  245. for asset in self.assets {
  246. var serverUrl = self.serverUrl
  247. var livePhoto: Bool = false
  248. let creationDate = asset.creationDate ?? Date()
  249. let fileName = CCUtility.createFileName(asset.originalFilename as String, fileDate: creationDate, fileType: asset.mediaType, keyFileName: NCGlobal.shared.keyFileNameMask, keyFileNameType: NCGlobal.shared.keyFileNameType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal, forcedNewFileName: false)!
  250. if asset.mediaSubtypes.contains(.photoLive) && CCUtility.getLivePhoto() {
  251. livePhoto = true
  252. }
  253. if useSubFolder {
  254. let dateFormatter = DateFormatter()
  255. dateFormatter.dateFormat = "yyyy"
  256. let yearString = dateFormatter.string(from: creationDate)
  257. dateFormatter.dateFormat = "MM"
  258. let monthString = dateFormatter.string(from: creationDate)
  259. serverUrl = autoUploadPath + "/" + yearString + "/" + monthString
  260. }
  261. // Check if is in upload
  262. let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", self.appDelegate.account, serverUrl, fileName), sorted: "fileName", ascending: false)
  263. if isRecordInSessions.count > 0 { continue }
  264. let metadataForUpload = NCManageDatabase.shared.createMetadata(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: NSUUID().uuidString, serverUrl: serverUrl, urlBase: self.appDelegate.urlBase, url: "", contentType: "", isLivePhoto: livePhoto)
  265. metadataForUpload.assetLocalIdentifier = asset.localIdentifier
  266. metadataForUpload.session = self.session
  267. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  268. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  269. if let result = NCManageDatabase.shared.getMetadataConflict(account: self.appDelegate.account, serverUrl: serverUrl, fileNameView: fileName) {
  270. metadataForUpload.fileName = result.fileName
  271. metadatasUploadInConflict.append(metadataForUpload)
  272. } else {
  273. metadatasNOConflict.append(metadataForUpload)
  274. }
  275. }
  276. // Verify if file(s) exists
  277. if metadatasUploadInConflict.count > 0 {
  278. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  279. if let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
  280. conflict.serverUrl = self.serverUrl
  281. conflict.metadatasNOConflict = metadatasNOConflict
  282. conflict.metadatasUploadInConflict = metadatasUploadInConflict
  283. conflict.delegate = self.appDelegate
  284. self.appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
  285. }
  286. }
  287. } else {
  288. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: metadatasNOConflict, completion: { _ in })
  289. }
  290. DispatchQueue.main.async {self.dismiss(animated: true, completion: nil) }
  291. }
  292. }
  293. @objc func cancel() {
  294. self.dismiss(animated: true, completion: nil)
  295. }
  296. // MARK: - Utility
  297. func previewFileName(valueRename: String?) -> String {
  298. var returnString: String = ""
  299. let asset = assets[0]
  300. let creationDate = asset.creationDate ?? Date()
  301. if CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal) {
  302. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.originalFilename as String))
  303. } else if let valueRename = valueRename {
  304. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  305. if valueRenameTrimming.count > 0 {
  306. self.form.delegate = nil
  307. CCUtility.setFileNameMask(valueRename, key: NCGlobal.shared.keyFileNameMask)
  308. self.form.delegate = self
  309. returnString = CCUtility.createFileName(asset.originalFilename as String, fileDate: creationDate, fileType: asset.mediaType, keyFileName: NCGlobal.shared.keyFileNameMask, keyFileNameType: NCGlobal.shared.keyFileNameType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal, forcedNewFileName: false)
  310. } else {
  311. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  312. returnString = CCUtility.createFileName(asset.originalFilename as String, fileDate: creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: NCGlobal.shared.keyFileNameType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal, forcedNewFileName: false)
  313. }
  314. } else {
  315. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  316. returnString = CCUtility.createFileName(asset.originalFilename as String, fileDate: creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: NCGlobal.shared.keyFileNameType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal, forcedNewFileName: false)
  317. }
  318. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + returnString
  319. }
  320. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  321. self.deselectFormRow(sender)
  322. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  323. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  324. let viewController = navigationController.topViewController as! NCSelect
  325. viewController.delegate = self
  326. viewController.typeOfCommandView = .selectCreateFolder
  327. viewController.includeDirectoryE2EEncryption = true
  328. self.present(navigationController, animated: true, completion: nil)
  329. }
  330. }