NCCreateFormUploadAssets.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 Foundation
  24. @objc protocol createFormUploadAssetsDelegate {
  25. func dismissFormUploadAssets()
  26. }
  27. class NCCreateFormUploadAssets: XLFormViewController, NCSelectDelegate {
  28. var serverUrl : String = ""
  29. var titleServerUrl : String?
  30. var assets: NSMutableArray = []
  31. var cryptated : Bool = false
  32. var session : String = ""
  33. weak var delegate: createFormUploadAssetsDelegate?
  34. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  35. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  36. self.init()
  37. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  38. titleServerUrl = "/"
  39. } else {
  40. titleServerUrl = (serverUrl as NSString).lastPathComponent
  41. }
  42. self.serverUrl = serverUrl
  43. self.assets = assets
  44. self.cryptated = cryptated
  45. self.session = session
  46. self.delegate = delegate
  47. self.initializeForm()
  48. }
  49. //MARK: XLFormDescriptorDelegate
  50. func initializeForm() {
  51. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_upload_photos_videos_", comment: "")) as XLFormDescriptor
  52. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  53. var section : XLFormSectionDescriptor
  54. var row : XLFormRowDescriptor
  55. // Section: Destination Folder
  56. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  57. form.addFormSection(section)
  58. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  59. row.action.formSelector = #selector(changeDestinationFolder(_:))
  60. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:1, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  61. row.cellConfig["imageView.image"] = imageFolder
  62. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  63. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  64. section.addFormRow(row)
  65. // User folder Autoupload
  66. row = XLFormRowDescriptor(tag: "useFolderAutoUpload", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  67. row.value = 0
  68. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  69. section.addFormRow(row)
  70. // Use Sub folder
  71. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  72. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  73. if tableAccount?.autoUploadCreateSubfolder == true {
  74. row.value = 1
  75. } else {
  76. row.value = 0
  77. }
  78. row.hidden = "$\("useFolderAutoUpload") == 0"
  79. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  80. section.addFormRow(row)
  81. // Section Mode filename
  82. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  83. form.addFormSection(section)
  84. // Maintain the original fileName
  85. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  86. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  87. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  88. section.addFormRow(row)
  89. // Add File Name Type
  90. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  91. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  92. row.hidden = "$\("maintainOriginalFileName") == 1"
  93. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  94. section.addFormRow(row)
  95. // Section: Rename File Name
  96. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  97. form.addFormSection(section)
  98. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  99. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  100. if fileNameMask.count > 0 {
  101. row.value = fileNameMask
  102. }
  103. row.hidden = "$\("maintainOriginalFileName") == 1"
  104. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  105. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  106. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  107. section.addFormRow(row)
  108. // Section: Preview File Name
  109. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  110. row.height = 180
  111. row.disabled = true
  112. row.cellConfig["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  113. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  114. section.addFormRow(row)
  115. self.form = form
  116. }
  117. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  118. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  119. if formRow.tag == "useFolderAutoUpload" {
  120. if (formRow.value! as AnyObject).boolValue == true {
  121. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  122. buttonDestinationFolder.hidden = true
  123. } else{
  124. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  125. buttonDestinationFolder.hidden = false
  126. }
  127. }
  128. else if formRow.tag == "useSubFolder" {
  129. if (formRow.value! as AnyObject).boolValue == true {
  130. } else{
  131. }
  132. }
  133. else if formRow.tag == "maintainOriginalFileName" {
  134. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  135. self.reloadForm()
  136. }
  137. else if formRow.tag == "addFileNameType" {
  138. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  139. self.reloadForm()
  140. }
  141. else if formRow.tag == "maskFileName" {
  142. let fileName = formRow.value as? String
  143. self.form.delegate = nil
  144. if let fileName = fileName {
  145. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  146. }
  147. self.form.delegate = self
  148. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  149. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  150. // reload cell
  151. if fileName != nil {
  152. if newValue as! String != formRow.value as! String {
  153. self.reloadFormRow(formRow)
  154. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  155. }
  156. }
  157. self.reloadFormRow(previewFileName)
  158. }
  159. }
  160. // MARK: - View Life Cycle
  161. override func viewDidLoad() {
  162. super.viewDidLoad()
  163. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  164. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  165. self.navigationItem.leftBarButtonItem = cancelButton
  166. self.navigationItem.rightBarButtonItem = saveButton
  167. self.navigationController?.navigationBar.isTranslucent = false
  168. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  169. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  170. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  171. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  172. self.reloadForm()
  173. }
  174. override func viewWillDisappear(_ animated: Bool)
  175. {
  176. super.viewWillDisappear(animated)
  177. self.delegate?.dismissFormUploadAssets()
  178. }
  179. func reloadForm() {
  180. self.form.delegate = nil
  181. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  182. buttonDestinationFolder.title = self.titleServerUrl
  183. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  184. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  185. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  186. self.tableView.reloadData()
  187. self.form.delegate = self
  188. }
  189. // MARK: - Action
  190. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  191. if serverUrl != nil {
  192. self.serverUrl = serverUrl!
  193. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  194. self.titleServerUrl = "/"
  195. } else {
  196. self.titleServerUrl = (serverUrl! as NSString).lastPathComponent
  197. }
  198. // Update
  199. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  200. row.title = self.titleServerUrl
  201. self.updateFormRow(row)
  202. }
  203. }
  204. @objc func save() {
  205. self.dismiss(animated: true, completion: {
  206. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderAutoUpload")!
  207. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  208. var useSubFolder : Bool = false
  209. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  210. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  211. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  212. }
  213. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  214. })
  215. }
  216. @objc func cancel() {
  217. self.dismiss(animated: true, completion: nil)
  218. }
  219. // MARK: - Utility
  220. func previewFileName(valueRename : String?) -> String {
  221. var returnString: String = ""
  222. let asset = assets[0] as! PHAsset
  223. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  224. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  225. } else if let valueRename = valueRename {
  226. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  227. if valueRenameTrimming.count > 0 {
  228. self.form.delegate = nil
  229. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  230. self.form.delegate = self
  231. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  232. } else {
  233. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  234. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  235. }
  236. } else {
  237. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  238. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  239. }
  240. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  241. }
  242. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  243. self.deselectFormRow(sender)
  244. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  245. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  246. let viewController = navigationController.topViewController as! NCSelect
  247. viewController.delegate = self
  248. viewController.hideButtonCreateFolder = false
  249. viewController.includeDirectoryE2EEncryption = true
  250. viewController.includeImages = false
  251. viewController.layoutViewSelect = k_layout_view_move
  252. viewController.selectFile = false
  253. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  254. viewController.type = ""
  255. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  256. self.present(navigationController, animated: true, completion: nil)
  257. }
  258. }