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