CCCreateCloud.swift 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. //
  2. // CCCreateCloud.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/01/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. // MARK: -
  25. class CreateMenuAdd: NSObject {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. let fontButton = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedString.Key.foregroundColor: UIColor.black]
  28. let fontEncrypted = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.encrypted as UIColor]
  29. let fontCancel = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue-Bold", size: 17)!, NSAttributedString.Key.foregroundColor: UIColor.black]
  30. let fontDisable = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedString.Key.foregroundColor: UIColor.darkGray]
  31. let colorLightGray = UIColor(red: 250.0/255.0, green: 250.0/255.0, blue: 250.0/255.0, alpha: 1)
  32. let colorGray = UIColor(red: 150.0/255.0, green: 150.0/255.0, blue: 150.0/255.0, alpha: 1)
  33. var colorIcon = NCBrandColor.sharedInstance.brandElement
  34. @objc init (themingColor : UIColor) {
  35. super.init()
  36. colorIcon = themingColor
  37. }
  38. @objc func createMenu(view : UIView) {
  39. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  40. let actionSheet = AHKActionSheet.init(view: view, title: nil)!
  41. actionSheet.animationDuration = 0.2
  42. actionSheet.automaticallyTintButtonImages = 0
  43. actionSheet.buttonHeight = 50.0
  44. actionSheet.cancelButtonHeight = 50.0
  45. actionSheet.separatorHeight = 5.0
  46. actionSheet.separatorColor = NCBrandColor.sharedInstance.seperator
  47. actionSheet.buttonTextAttributes = fontButton
  48. actionSheet.encryptedButtonTextAttributes = fontEncrypted
  49. actionSheet.cancelButtonTextAttributes = fontCancel
  50. actionSheet.disableButtonTextAttributes = fontDisable
  51. actionSheet.cancelButtonTitle = NSLocalizedString("_cancel_", comment: "")
  52. actionSheet.addButton(withTitle: NSLocalizedString("_upload_photos_videos_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "media"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  53. appDelegate.activeMain.openAssetsPickerController()
  54. })
  55. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  56. appDelegate.activeMain.openImportDocumentPicker()
  57. })
  58. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_text_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file_txt"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  59. let storyboard = UIStoryboard(name: "NCText", bundle: nil)
  60. let controller = storyboard.instantiateViewController(withIdentifier: "NCText")
  61. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  62. appDelegate.activeMain.present(controller, animated: true, completion: nil)
  63. })
  64. if #available(iOS 11.0, *) {
  65. actionSheet.addButton(withTitle: NSLocalizedString("_scans_document_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "scan"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  66. NCCreateScanDocument.sharedInstance.openScannerDocument(viewController: appDelegate.activeMain, openScan: true)
  67. })
  68. }
  69. actionSheet.addButton(withTitle: NSLocalizedString("_create_folder_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "folder"), multiplier:2, color: colorIcon), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0 ,type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  70. appDelegate.activeMain.createFolder()
  71. })
  72. actionSheet.show()
  73. }
  74. }
  75. // MARK: -
  76. @objc protocol createFormUploadAssetsDelegate {
  77. func dismissFormUploadAssets()
  78. }
  79. // MARK: -
  80. class CreateFormUploadAssets: XLFormViewController, CCMoveDelegate {
  81. var serverUrl : String = ""
  82. var titleServerUrl : String?
  83. var assets: NSMutableArray = []
  84. var cryptated : Bool = false
  85. var session : String = ""
  86. weak var delegate: createFormUploadAssetsDelegate?
  87. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  88. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  89. self.init()
  90. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  91. titleServerUrl = "/"
  92. } else {
  93. titleServerUrl = (serverUrl as NSString).lastPathComponent
  94. }
  95. self.serverUrl = serverUrl
  96. self.assets = assets
  97. self.cryptated = cryptated
  98. self.session = session
  99. self.delegate = delegate
  100. self.initializeForm()
  101. }
  102. //MARK: XLFormDescriptorDelegate
  103. func initializeForm() {
  104. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_upload_photos_videos_", comment: "")) as XLFormDescriptor
  105. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  106. var section : XLFormSectionDescriptor
  107. var row : XLFormRowDescriptor
  108. // Section: Destination Folder
  109. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  110. form.addFormSection(section)
  111. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  112. row.action.formSelector = #selector(changeDestinationFolder(_:))
  113. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  114. row.cellConfig["imageView.image"] = imageFolder
  115. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  116. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  117. section.addFormRow(row)
  118. // User folder Media
  119. row = XLFormRowDescriptor(tag: "useFolderMedia", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_media_", comment: ""))
  120. row.value = 0
  121. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  122. section.addFormRow(row)
  123. // Use Sub folder
  124. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  125. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  126. if tableAccount?.autoUploadCreateSubfolder == true {
  127. row.value = 1
  128. } else {
  129. row.value = 0
  130. }
  131. row.hidden = "$\("useFolderMedia") == 0"
  132. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  133. section.addFormRow(row)
  134. // Section Mode filename
  135. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  136. form.addFormSection(section)
  137. // Maintain the original fileName
  138. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  139. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  140. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  141. section.addFormRow(row)
  142. // Add File Name Type
  143. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  144. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  145. row.hidden = "$\("maintainOriginalFileName") == 1"
  146. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  147. section.addFormRow(row)
  148. // Section: Rename File Name
  149. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  150. form.addFormSection(section)
  151. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  152. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  153. if fileNameMask.count > 0 {
  154. row.value = fileNameMask
  155. }
  156. row.hidden = "$\("maintainOriginalFileName") == 1"
  157. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  158. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  159. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  160. section.addFormRow(row)
  161. // Section: Preview File Name
  162. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  163. row.height = 180
  164. row.disabled = true
  165. row.cellConfig["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  166. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  167. section.addFormRow(row)
  168. self.form = form
  169. }
  170. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  171. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  172. if formRow.tag == "useFolderMedia" {
  173. if (formRow.value! as AnyObject).boolValue == true {
  174. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  175. buttonDestinationFolder.hidden = true
  176. } else{
  177. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  178. buttonDestinationFolder.hidden = false
  179. }
  180. }
  181. else if formRow.tag == "useSubFolder" {
  182. if (formRow.value! as AnyObject).boolValue == true {
  183. } else{
  184. }
  185. }
  186. else if formRow.tag == "maintainOriginalFileName" {
  187. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  188. self.reloadForm()
  189. }
  190. else if formRow.tag == "addFileNameType" {
  191. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  192. self.reloadForm()
  193. }
  194. else if formRow.tag == "maskFileName" {
  195. let fileName = formRow.value as? String
  196. self.form.delegate = nil
  197. if let fileName = fileName {
  198. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  199. }
  200. self.form.delegate = self
  201. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  202. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  203. // reload cell
  204. if fileName != nil {
  205. if newValue as! String != formRow.value as! String {
  206. self.reloadFormRow(formRow)
  207. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  208. }
  209. }
  210. self.reloadFormRow(previewFileName)
  211. }
  212. }
  213. // MARK: - View Life Cycle
  214. override func viewDidLoad() {
  215. super.viewDidLoad()
  216. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  217. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  218. self.navigationItem.leftBarButtonItem = cancelButton
  219. self.navigationItem.rightBarButtonItem = saveButton
  220. self.navigationController?.navigationBar.isTranslucent = false
  221. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  222. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  223. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  224. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  225. self.reloadForm()
  226. }
  227. override func viewWillDisappear(_ animated: Bool)
  228. {
  229. super.viewWillDisappear(animated)
  230. self.delegate?.dismissFormUploadAssets()
  231. }
  232. func reloadForm() {
  233. self.form.delegate = nil
  234. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  235. buttonDestinationFolder.title = self.titleServerUrl
  236. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  237. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  238. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  239. self.tableView.reloadData()
  240. self.form.delegate = self
  241. }
  242. // MARK: - Action
  243. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  244. self.serverUrl = serverUrlTo
  245. if let title = title {
  246. self.titleServerUrl = title
  247. } else {
  248. self.titleServerUrl = "/"
  249. }
  250. self.reloadForm()
  251. }
  252. @objc func save() {
  253. self.dismiss(animated: true, completion: {
  254. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderMedia")!
  255. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  256. var useSubFolder : Bool = false
  257. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  258. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  259. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  260. }
  261. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  262. })
  263. }
  264. @objc func cancel() {
  265. self.dismiss(animated: true, completion: nil)
  266. }
  267. // MARK: - Utility
  268. func previewFileName(valueRename : String?) -> String {
  269. var returnString: String = ""
  270. let asset = assets[0] as! PHAsset
  271. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  272. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  273. } else if let valueRename = valueRename {
  274. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  275. if valueRenameTrimming.count > 0 {
  276. self.form.delegate = nil
  277. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  278. self.form.delegate = self
  279. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  280. } else {
  281. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  282. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  283. }
  284. } else {
  285. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  286. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  287. }
  288. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  289. }
  290. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  291. self.deselectFormRow(sender)
  292. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  293. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  294. let viewController : CCMove = navigationController.topViewController as! CCMove
  295. viewController.delegate = self;
  296. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  297. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  298. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  299. viewController.move.title = NSLocalizedString("_select_", comment: "");
  300. viewController.networkingOperationQueue = appDelegate.netQueue
  301. // E2EE
  302. viewController.includeDirectoryE2EEncryption = true;
  303. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  304. self.present(navigationController, animated: true, completion: nil)
  305. }
  306. }
  307. // MARK: -
  308. class CreateFormUploadFileText: XLFormViewController, CCMoveDelegate {
  309. var serverUrl = ""
  310. var titleServerUrl = ""
  311. var fileName = ""
  312. var text = ""
  313. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  314. convenience init(serverUrl: String, text: String, fileName: String) {
  315. self.init()
  316. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  317. titleServerUrl = "/"
  318. } else {
  319. titleServerUrl = (serverUrl as NSString).lastPathComponent
  320. }
  321. self.fileName = fileName
  322. self.serverUrl = serverUrl
  323. self.text = text
  324. initializeForm()
  325. }
  326. //MARK: XLFormDescriptorDelegate
  327. func initializeForm() {
  328. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  329. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  330. var section : XLFormSectionDescriptor
  331. var row : XLFormRowDescriptor
  332. // Section: Destination Folder
  333. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  334. form.addFormSection(section)
  335. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  336. row.action.formSelector = #selector(changeDestinationFolder(_:))
  337. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  338. row.cellConfig["imageView.image"] = imageFolder
  339. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  340. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  341. section.addFormRow(row)
  342. // Section: File Name
  343. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  344. form.addFormSection(section)
  345. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  346. row.value = self.fileName
  347. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  348. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  349. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  350. section.addFormRow(row)
  351. self.form = form
  352. }
  353. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  354. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  355. if formRow.tag == "fileName" {
  356. self.form.delegate = nil
  357. if let fileNameNew = formRow.value {
  358. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as? String)
  359. }
  360. formRow.value = self.fileName
  361. self.title = fileName
  362. self.updateFormRow(formRow)
  363. self.form.delegate = self
  364. }
  365. }
  366. // MARK: - View Life Cycle
  367. override func viewDidLoad() {
  368. super.viewDidLoad()
  369. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  370. self.navigationItem.rightBarButtonItem = saveButton
  371. self.navigationController?.navigationBar.isTranslucent = false
  372. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  373. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  374. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  375. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  376. }
  377. // MARK: - Action
  378. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  379. self.serverUrl = serverUrlTo
  380. if let title = title {
  381. self.titleServerUrl = title
  382. } else {
  383. self.titleServerUrl = "/"
  384. }
  385. // Update
  386. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  387. row.title = self.titleServerUrl
  388. self.updateFormRow(row)
  389. }
  390. @objc func save() {
  391. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  392. guard let name = rowFileName.value else {
  393. return
  394. }
  395. let ext = (name as! NSString).pathExtension.uppercased()
  396. var fileNameSave = ""
  397. if (ext == "") {
  398. fileNameSave = name as! String + ".txt"
  399. } else if (CCUtility.isDocumentModifiableExtension(ext)) {
  400. fileNameSave = name as! String
  401. } else {
  402. fileNameSave = (name as! NSString).deletingPathExtension + ".txt"
  403. }
  404. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
  405. return
  406. }
  407. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
  408. if (metadata != nil) {
  409. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  410. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  411. }
  412. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  413. self.dismissAndUpload(fileNameSave, fileID: metadata!.fileID, directoryID: directoryID)
  414. }
  415. alertController.addAction(cancelAction)
  416. alertController.addAction(overwriteAction)
  417. self.present(alertController, animated: true, completion:nil)
  418. } else {
  419. let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
  420. dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  421. }
  422. }
  423. func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
  424. self.dismiss(animated: true, completion: {
  425. let data = self.text.data(using: .utf8)
  426. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave), contents: data, attributes: nil)
  427. if success {
  428. let metadataForUpload = tableMetadata()
  429. metadataForUpload.account = self.appDelegate.activeAccount
  430. metadataForUpload.date = NSDate()
  431. metadataForUpload.directoryID = directoryID
  432. metadataForUpload.fileID = fileID
  433. metadataForUpload.fileName = fileNameSave
  434. metadataForUpload.fileNameView = fileNameSave
  435. metadataForUpload.session = k_upload_session
  436. metadataForUpload.sessionSelector = selectorUploadFile
  437. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  438. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  439. self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
  440. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  441. } else {
  442. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  443. }
  444. })
  445. }
  446. func cancel() {
  447. self.dismiss(animated: true, completion: nil)
  448. }
  449. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  450. self.deselectFormRow(sender)
  451. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  452. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  453. let viewController : CCMove = navigationController.topViewController as! CCMove
  454. viewController.delegate = self;
  455. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  456. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  457. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  458. viewController.move.title = NSLocalizedString("_select_", comment: "");
  459. viewController.networkingOperationQueue = appDelegate.netQueue
  460. // E2EE
  461. viewController.includeDirectoryE2EEncryption = true;
  462. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  463. self.present(navigationController, animated: true, completion: nil)
  464. }
  465. }
  466. //MARK: -
  467. class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
  468. enum typeDpiQuality {
  469. case low
  470. case medium
  471. case hight
  472. }
  473. var dpiQuality: typeDpiQuality = typeDpiQuality.medium
  474. var serverUrl = ""
  475. var titleServerUrl = ""
  476. var arrayImages = [UIImage]()
  477. var fileName = CCUtility.createFileNameDate("scan", extension: "pdf")
  478. var password : PDFPassword = ""
  479. var fileType = "PDF"
  480. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  481. convenience init(serverUrl: String, arrayImages: [UIImage]) {
  482. self.init()
  483. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  484. titleServerUrl = "/"
  485. } else {
  486. titleServerUrl = (serverUrl as NSString).lastPathComponent
  487. }
  488. self.serverUrl = serverUrl
  489. self.arrayImages = arrayImages
  490. initializeForm()
  491. }
  492. //MARK: XLFormDescriptorDelegate
  493. func initializeForm() {
  494. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_save_settings_", comment: "")) as XLFormDescriptor
  495. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  496. var section : XLFormSectionDescriptor
  497. var row : XLFormRowDescriptor
  498. // Section: Destination Folder
  499. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  500. form.addFormSection(section)
  501. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  502. row.action.formSelector = #selector(changeDestinationFolder(_:))
  503. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  504. row.cellConfig["imageView.image"] = imageFolder
  505. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  506. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  507. section.addFormRow(row)
  508. // Section: Quality
  509. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_quality_image_title_", comment: ""))
  510. form.addFormSection(section)
  511. row = XLFormRowDescriptor(tag: "compressionQuality", rowType: XLFormRowDescriptorTypeSlider)
  512. row.value = 0.5
  513. row.title = NSLocalizedString("_quality_medium_", comment: "")
  514. row.cellConfig["slider.minimumTrackTintColor"] = NCBrandColor.sharedInstance.brand
  515. row.cellConfig["slider.maximumValue"] = 1
  516. row.cellConfig["slider.minimumValue"] = 0
  517. row.cellConfig["steps"] = 2
  518. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.center.rawValue
  519. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  520. section.addFormRow(row)
  521. // Section: Password
  522. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_pdf_password_", comment: ""))
  523. form.addFormSection(section)
  524. row = XLFormRowDescriptor(tag: "password", rowType: XLFormRowDescriptorTypePassword, title: NSLocalizedString("_password_", comment: ""))
  525. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  526. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  527. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  528. section.addFormRow(row)
  529. // Section: File
  530. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_file_creation_", comment: ""))
  531. form.addFormSection(section)
  532. if arrayImages.count == 1 {
  533. row = XLFormRowDescriptor(tag: "filetype", rowType: XLFormRowDescriptorTypeSelectorSegmentedControl, title: NSLocalizedString("_file_type_", comment: ""))
  534. row.selectorOptions = ["PDF","JPG"]
  535. row.value = "PDF"
  536. row.cellConfig["tintColor"] = NCBrandColor.sharedInstance.brand
  537. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  538. section.addFormRow(row)
  539. }
  540. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  541. row.value = self.fileName
  542. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  543. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  544. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  545. section.addFormRow(row)
  546. self.form = form
  547. }
  548. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  549. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  550. if formRow.tag == "fileName" {
  551. self.form.delegate = nil
  552. let fileNameNew = newValue as? String
  553. if fileNameNew != nil {
  554. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew)
  555. } else {
  556. self.fileName = ""
  557. }
  558. formRow.value = self.fileName
  559. self.updateFormRow(formRow)
  560. self.form.delegate = self
  561. }
  562. if formRow.tag == "compressionQuality" {
  563. self.form.delegate = nil
  564. //let row : XLFormRowDescriptor = self.form.formRow(withTag: "descriptionQuality")!
  565. let newQuality = newValue as? NSNumber
  566. let compressionQuality = (newQuality?.doubleValue)!
  567. if compressionQuality >= 0.0 && compressionQuality <= 0.3 {
  568. formRow.title = NSLocalizedString("_quality_low_", comment: "")
  569. dpiQuality = typeDpiQuality.low
  570. } else if compressionQuality > 0.3 && compressionQuality <= 0.6 {
  571. formRow.title = NSLocalizedString("_quality_medium_", comment: "")
  572. dpiQuality = typeDpiQuality.medium
  573. } else if compressionQuality > 0.6 && compressionQuality <= 1.0 {
  574. formRow.title = NSLocalizedString("_quality_high_", comment: "")
  575. dpiQuality = typeDpiQuality.hight
  576. }
  577. self.updateFormRow(formRow)
  578. self.form.delegate = self
  579. }
  580. if formRow.tag == "password" {
  581. let stringPassword = newValue as? String
  582. if stringPassword != nil {
  583. password = PDFPassword(stringPassword!)
  584. } else {
  585. password = PDFPassword("")
  586. }
  587. }
  588. if formRow.tag == "filetype" {
  589. fileType = newValue as! String
  590. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  591. let rowPassword : XLFormRowDescriptor = self.form.formRow(withTag: "password")!
  592. // rowFileName
  593. guard var name = rowFileName.value else {
  594. return
  595. }
  596. if name as! String == "" {
  597. name = CCUtility.createFileNameDate("scan", extension: "pdf")
  598. }
  599. let ext = (name as! NSString).pathExtension.uppercased()
  600. var newFileName = ""
  601. if (ext == "") {
  602. newFileName = name as! String + "." + fileType.lowercased()
  603. } else {
  604. newFileName = (name as! NSString).deletingPathExtension + "." + fileType.lowercased()
  605. }
  606. rowFileName.value = newFileName
  607. self.updateFormRow(rowFileName)
  608. // rowPassword
  609. if fileType == "JPG" {
  610. rowPassword.value = ""
  611. password = PDFPassword("")
  612. rowPassword.disabled = true
  613. } else {
  614. rowPassword.disabled = false
  615. }
  616. self.updateFormRow(rowPassword)
  617. }
  618. }
  619. // MARK: - View Life Cycle
  620. override func viewDidLoad() {
  621. super.viewDidLoad()
  622. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  623. self.navigationItem.rightBarButtonItem = saveButton
  624. self.navigationController?.navigationBar.isTranslucent = false
  625. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  626. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  627. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  628. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  629. // self.tableView.sectionHeaderHeight = 10
  630. // self.tableView.sectionFooterHeight = 10
  631. // self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  632. // let row : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  633. // let rowCell = row.cell(forForm: self)
  634. // rowCell.becomeFirstResponder()
  635. }
  636. // MARK: - Action
  637. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  638. self.serverUrl = serverUrlTo
  639. if let title = title {
  640. self.titleServerUrl = title
  641. } else {
  642. self.titleServerUrl = "/"
  643. }
  644. // Update
  645. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  646. row.title = self.titleServerUrl
  647. self.updateFormRow(row)
  648. }
  649. @objc func save() {
  650. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  651. guard let name = rowFileName.value else {
  652. return
  653. }
  654. if name as! String == "" {
  655. return
  656. }
  657. let ext = (name as! NSString).pathExtension.uppercased()
  658. var fileNameSave = ""
  659. if (ext == "") {
  660. fileNameSave = name as! String + "." + fileType.lowercased()
  661. } else {
  662. fileNameSave = (name as! NSString).deletingPathExtension + "." + fileType.lowercased()
  663. }
  664. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
  665. return
  666. }
  667. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
  668. if (metadata != nil) {
  669. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  670. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  671. }
  672. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  673. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave), clearDateReadDirectoryID: directoryID)
  674. self.dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  675. }
  676. alertController.addAction(cancelAction)
  677. alertController.addAction(overwriteAction)
  678. self.present(alertController, animated: true, completion:nil)
  679. } else {
  680. let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
  681. dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  682. }
  683. }
  684. func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
  685. guard let fileNameGenerateExport = CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave) else {
  686. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  687. return
  688. }
  689. if fileType == "PDF" {
  690. var pdfPages = [PDFPage]()
  691. //Generate PDF
  692. for var image in self.arrayImages {
  693. image = changeImageFromQuality(image, dpiQuality: dpiQuality)
  694. guard let data = image.jpegData(compressionQuality: 0.5) else {
  695. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  696. return
  697. }
  698. let page = PDFPage.image(UIImage(data: data)!)
  699. pdfPages.append(page)
  700. }
  701. do {
  702. try PDFGenerator.generate(pdfPages, to: fileNameGenerateExport, password: password)
  703. } catch {
  704. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  705. return
  706. }
  707. }
  708. if fileType == "JPG" {
  709. let image = changeImageFromQuality(self.arrayImages[0], dpiQuality: dpiQuality)
  710. guard let data = image.jpegData(compressionQuality: CGFloat(0.5)) else {
  711. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  712. return
  713. }
  714. do {
  715. try data.write(to: NSURL.fileURL(withPath: fileNameGenerateExport), options: .atomic)
  716. } catch {
  717. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  718. return
  719. }
  720. }
  721. //Create metadata for upload
  722. let metadataForUpload = tableMetadata()
  723. metadataForUpload.account = self.appDelegate.activeAccount
  724. metadataForUpload.date = NSDate()
  725. metadataForUpload.directoryID = directoryID
  726. metadataForUpload.fileID = fileID
  727. metadataForUpload.fileName = fileNameSave
  728. metadataForUpload.fileNameView = fileNameSave
  729. metadataForUpload.session = k_upload_session
  730. metadataForUpload.sessionSelector = selectorUploadFile
  731. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  732. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  733. self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
  734. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  735. self.dismiss(animated: true, completion: nil)
  736. }
  737. func cancel() {
  738. self.dismiss(animated: true, completion: nil)
  739. }
  740. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  741. self.deselectFormRow(sender)
  742. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  743. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  744. let viewController : CCMove = navigationController.topViewController as! CCMove
  745. viewController.delegate = self;
  746. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  747. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  748. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  749. viewController.move.title = NSLocalizedString("_select_", comment: "");
  750. viewController.networkingOperationQueue = appDelegate.netQueue
  751. // E2EE
  752. viewController.includeDirectoryE2EEncryption = true;
  753. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  754. self.present(navigationController, animated: true, completion: nil)
  755. }
  756. func changeImageFromQuality(_ image: UIImage, dpiQuality: typeDpiQuality) -> UIImage {
  757. let imageWidthInPixels = image.size.width * image.scale
  758. let imageHeightInPixels = image.size.height * image.scale
  759. switch dpiQuality {
  760. case typeDpiQuality.low: // 72 DPI
  761. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  762. return CCGraphics.scale(image, to: CGSize(width: 595, height: 842), isAspectRation: true)
  763. }
  764. case typeDpiQuality.medium: // 150 DPI
  765. if imageWidthInPixels > 1240 || imageHeightInPixels > 1754 {
  766. return CCGraphics.scale(image, to: CGSize(width: 1240, height: 1754), isAspectRation: true)
  767. }
  768. case typeDpiQuality.hight: // 200 DPI
  769. if imageWidthInPixels > 1654 || imageHeightInPixels > 2339 {
  770. return CCGraphics.scale(image, to: CGSize(width: 1654, height: 2339), isAspectRation: true)
  771. }
  772. }
  773. return image
  774. }
  775. }
  776. class NCCreateScanDocument : NSObject, ImageScannerControllerDelegate {
  777. @objc static let sharedInstance: NCCreateScanDocument = {
  778. let instance = NCCreateScanDocument()
  779. return instance
  780. }()
  781. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  782. var viewController: UIViewController?
  783. var openScan: Bool = false
  784. @available(iOS 10, *)
  785. func openScannerDocument(viewController: UIViewController, openScan: Bool) {
  786. self.viewController = viewController
  787. self.openScan = openScan
  788. let scannerVC = ImageScannerController()
  789. scannerVC.imageScannerDelegate = self
  790. self.viewController?.present(scannerVC, animated: true, completion: nil)
  791. }
  792. @available(iOS 10, *)
  793. func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
  794. let fileName = CCUtility.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)!
  795. let fileNamePath = CCUtility.getDirectoryScan() + "/" + fileName
  796. do {
  797. try results.scannedImage.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath), options: .atomic)
  798. } catch { }
  799. scanner.dismiss(animated: true, completion: {
  800. if (self.openScan) {
  801. let storyboard = UIStoryboard(name: "Scan", bundle: nil)
  802. let controller = storyboard.instantiateInitialViewController()!
  803. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  804. self.viewController?.present(controller, animated: true, completion: nil)
  805. }
  806. })
  807. }
  808. @available(iOS 10, *)
  809. func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
  810. scanner.dismiss(animated: true, completion: nil)
  811. }
  812. @available(iOS 10, *)
  813. func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
  814. appDelegate.messageNotification("_error_", description: error.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  815. }
  816. }