NCCreateFormUploadAssets.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 requestOptions = PHImageRequestOptions()
  35. var imagePreview: UIImage?
  36. let targetSizeImagePreview = CGSize(width:100, height: 100)
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  39. self.init()
  40. if serverUrl == CCUtility.getHomeServer(appDelegate.urlBase) {
  41. titleServerUrl = "/"
  42. } else {
  43. if let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
  44. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", tableDirectory.ocId)) {
  45. titleServerUrl = metadata.fileNameView
  46. } else { titleServerUrl = (serverUrl as NSString).lastPathComponent }
  47. } else { titleServerUrl = (serverUrl as NSString).lastPathComponent }
  48. }
  49. self.serverUrl = serverUrl
  50. self.assets = assets
  51. self.cryptated = cryptated
  52. self.session = session
  53. self.delegate = delegate
  54. requestOptions.resizeMode = PHImageRequestOptionsResizeMode.exact
  55. requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
  56. requestOptions.isSynchronous = true
  57. }
  58. // MARK: - View Life Cycle
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. self.title = NSLocalizedString("_upload_photos_videos_", comment: "")
  62. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  63. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  64. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  65. if assets.count == 1 && (assets[0] as! PHAsset).mediaType == PHAssetMediaType.image {
  66. PHImageManager.default().requestImage(for: assets[0] as! PHAsset, targetSize: targetSizeImagePreview, contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, info) in
  67. self.imagePreview = image
  68. })
  69. }
  70. // Theming view
  71. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  72. changeTheming()
  73. }
  74. override func viewWillDisappear(_ animated: Bool)
  75. {
  76. super.viewWillDisappear(animated)
  77. self.delegate?.dismissFormUploadAssets()
  78. }
  79. @objc func changeTheming() {
  80. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  81. initializeForm()
  82. self.reloadForm()
  83. }
  84. //MARK: XLForm
  85. func initializeForm() {
  86. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  87. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  88. var section : XLFormSectionDescriptor
  89. var row : XLFormRowDescriptor
  90. // Section: Destination Folder
  91. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  92. form.addFormSection(section)
  93. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  94. row.action.formSelector = #selector(changeDestinationFolder(_:))
  95. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  96. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  97. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  98. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  99. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  100. section.addFormRow(row)
  101. // User folder Autoupload
  102. row = XLFormRowDescriptor(tag: "useFolderAutoUpload", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  103. row.value = 0
  104. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  105. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  106. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  107. section.addFormRow(row)
  108. // Use Sub folder
  109. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  110. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  111. if tableAccount?.autoUploadCreateSubfolder == true {
  112. row.value = 1
  113. } else {
  114. row.value = 0
  115. }
  116. row.hidden = "$\("useFolderAutoUpload") == 0"
  117. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  118. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  119. section.addFormRow(row)
  120. // Section Mode filename
  121. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  122. form.addFormSection(section)
  123. // Maintain the original fileName
  124. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  125. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  126. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  127. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  128. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  129. section.addFormRow(row)
  130. // Add File Name Type
  131. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  132. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  133. row.hidden = "$\("maintainOriginalFileName") == 1"
  134. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  135. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  136. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  137. section.addFormRow(row)
  138. // Section: Rename File Name
  139. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  140. form.addFormSection(section)
  141. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  142. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  143. if fileNameMask.count > 0 {
  144. row.value = fileNameMask
  145. }
  146. row.hidden = "$\("maintainOriginalFileName") == 1"
  147. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  148. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  149. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  150. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  151. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  152. row.cellConfig["textField.textColor"] = NCBrandColor.sharedInstance.textView
  153. section.addFormRow(row)
  154. // Section: Preview File Name
  155. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  156. row.height = 180
  157. row.disabled = true
  158. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  159. row.cellConfig["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  160. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  161. row.cellConfig["textView.textColor"] = NCBrandColor.sharedInstance.textView
  162. section.addFormRow(row)
  163. self.form = form
  164. }
  165. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  166. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  167. if formRow.tag == "useFolderAutoUpload" {
  168. if (formRow.value! as AnyObject).boolValue == true {
  169. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  170. buttonDestinationFolder.hidden = true
  171. } else{
  172. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  173. buttonDestinationFolder.hidden = false
  174. }
  175. }
  176. else if formRow.tag == "useSubFolder" {
  177. if (formRow.value! as AnyObject).boolValue == true {
  178. } else{
  179. }
  180. }
  181. else if formRow.tag == "maintainOriginalFileName" {
  182. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  183. self.reloadForm()
  184. }
  185. else if formRow.tag == "addFileNameType" {
  186. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  187. self.reloadForm()
  188. }
  189. else if formRow.tag == "maskFileName" {
  190. let fileName = formRow.value as? String
  191. self.form.delegate = nil
  192. if let fileName = fileName {
  193. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  194. }
  195. self.form.delegate = self
  196. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  197. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  198. // reload cell
  199. if fileName != nil {
  200. if newValue as! String != formRow.value as! String {
  201. self.reloadFormRow(formRow)
  202. NCContentPresenter.shared.messageNotification("_info_", description: "_forbidden_characters_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorCharactersForbidden), forced: true)
  203. }
  204. }
  205. self.reloadFormRow(previewFileName)
  206. }
  207. }
  208. func reloadForm() {
  209. self.form.delegate = nil
  210. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  211. buttonDestinationFolder.title = self.titleServerUrl
  212. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  213. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  214. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  215. self.tableView.reloadData()
  216. self.form.delegate = self
  217. }
  218. // MARK: - Action
  219. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, buttonType: String, overwrite: Bool) {
  220. if serverUrl != nil {
  221. self.serverUrl = serverUrl!
  222. if serverUrl == CCUtility.getHomeServer(appDelegate.urlBase) {
  223. self.titleServerUrl = "/"
  224. } else {
  225. if let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account
  226. , self.serverUrl)) {
  227. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", tableDirectory.ocId)) {
  228. titleServerUrl = metadata.fileNameView
  229. } else { titleServerUrl = (self.serverUrl as NSString).lastPathComponent }
  230. } else { titleServerUrl = (self.serverUrl as NSString).lastPathComponent }
  231. }
  232. // Update
  233. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  234. row.title = self.titleServerUrl
  235. self.updateFormRow(row)
  236. }
  237. }
  238. @objc func save() {
  239. self.dismiss(animated: true, completion: {
  240. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderAutoUpload")!
  241. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  242. var useSubFolder : Bool = false
  243. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  244. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.urlBase)
  245. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  246. }
  247. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  248. })
  249. }
  250. @objc func cancel() {
  251. self.dismiss(animated: true, completion: nil)
  252. }
  253. // MARK: - Utility
  254. func previewFileName(valueRename : String?) -> String {
  255. var returnString: String = ""
  256. let asset = assets[0] as! PHAsset
  257. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  258. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  259. } else if let valueRename = valueRename {
  260. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  261. if valueRenameTrimming.count > 0 {
  262. self.form.delegate = nil
  263. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  264. self.form.delegate = self
  265. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  266. } else {
  267. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  268. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  269. }
  270. } else {
  271. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  272. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  273. }
  274. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  275. }
  276. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  277. self.deselectFormRow(sender)
  278. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  279. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  280. let viewController = navigationController.topViewController as! NCSelect
  281. viewController.delegate = self
  282. viewController.hideButtonCreateFolder = false
  283. viewController.includeDirectoryE2EEncryption = true
  284. viewController.includeImages = false
  285. viewController.layoutViewSelect = k_layout_view_move
  286. viewController.selectFile = false
  287. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  288. viewController.type = ""
  289. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  290. self.present(navigationController, animated: true, completion: nil)
  291. }
  292. }