Browse Source

When the document has only one page it's possible select the export as JPG

Marino Faggiana 6 years ago
parent
commit
d8b9def24c

+ 88 - 25
iOSClient/Create/CCCreateCloud.swift

@@ -696,6 +696,7 @@ class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
     var fileName = "scan.pdf"
     var password : PDFPassword = ""
     var compressionQuality: Double = 0.5
+    var fileType = "PDF"
     
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
     
@@ -765,27 +766,43 @@ class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
 
         // Section: Password
         
-        section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_password_", comment: ""))
+        section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_pdf_password_", comment: ""))
         form.addFormSection(section)
         
-        row = XLFormRowDescriptor(tag: "password", rowType: XLFormRowDescriptorTypeAccount)
+        row = XLFormRowDescriptor(tag: "password", rowType: XLFormRowDescriptorTypePassword)
         
-        row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 14.0)
-        row.cellConfig["textLabel.textColor"] = UIColor.black
+        row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
+        row.cellConfig["textField.textColor"] = UIColor.black
         
         section.addFormRow(row)
         
-        // Section: File Name
+        // Section: File
         
-        section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
+        section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_file_", comment: ""))
         form.addFormSection(section)
         
-        row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount)
+        if arrayImages.count == 1 {
+            row = XLFormRowDescriptor(tag: "filetype", rowType: XLFormRowDescriptorTypeSelectorSegmentedControl, title: NSLocalizedString("_file_type_", comment: ""))
+            row.selectorOptions = ["PDF","JPG"]
+            row.value = "PDF"
+            
+            row.cellConfig["tintColor"] = NCBrandColor.sharedInstance.brand
+            row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
+            row.cellConfig["textLabel.textColor"] = UIColor.black
+            
+            section.addFormRow(row)
+        }
+        
+        row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
         row.value = self.fileName
 
-        row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 14.0)
+        row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
         row.cellConfig["textLabel.textColor"] = UIColor.black
         
+        row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
+        row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
+        row.cellConfig["textField.textColor"] = UIColor.black
+
         section.addFormRow(row)
        
         self.form = form
@@ -799,8 +816,12 @@ class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
             
             self.form.delegate = nil
             
-            if let fileNameNew = formRow.value {
-                self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
+            let fileNameNew = newValue as? String
+            
+            if fileNameNew != nil {
+                self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew)
+            } else {
+                self.fileName = ""
             }
             
             formRow.value = self.fileName
@@ -842,6 +863,26 @@ class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
                 password = PDFPassword("")
             }
         }
+        
+        if formRow.tag == "filetype" {
+            fileType = newValue as! String
+            
+            let rowFileName : XLFormRowDescriptor  = self.form.formRow(withTag: "fileName")!
+            guard let name = rowFileName.value else {
+                return
+            }
+            let ext = (name as! NSString).pathExtension.uppercased()
+            var newFileName = ""
+            
+            if (ext == "") {
+                newFileName = name as! String + "." + fileType.lowercased()
+            } else {
+                newFileName = (name as! NSString).deletingPathExtension + "." + fileType.lowercased()
+            }
+            
+            rowFileName.value = newFileName
+            self.updateFormRow(rowFileName)
+        }
     }
     
     // MARK: - View Life Cycle
@@ -896,13 +937,17 @@ class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
         guard let name = rowFileName.value else {
             return
         }
+        if name as! String == "" {
+            return
+        }
+        
         let ext = (name as! NSString).pathExtension.uppercased()
         var fileNameSave = ""
         
         if (ext == "") {
-            fileNameSave = name as! String + ".pdf"
+            fileNameSave = name as! String + "." + fileType.lowercased()
         } else {
-            fileNameSave = (name as! NSString).deletingPathExtension + ".pdf"
+            fileNameSave = (name as! NSString).deletingPathExtension + "." + fileType.lowercased()
         }
         
         guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
@@ -935,28 +980,46 @@ class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
     
     func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
         
-        var pdfPages = [PDFPage]()
-
-        guard let fileNameGeneratePDF = CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave) else {
+        guard let fileNameGenerateExport = CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave) else {
             self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
             return
         }
         
-        //Generate PDF
-        for image in self.arrayImages {
-            guard let data = UIImageJPEGRepresentation(image, CGFloat(compressionQuality)) else {
+        if fileType == "PDF" {
+        
+            var pdfPages = [PDFPage]()
+
+            //Generate PDF
+            for image in self.arrayImages {
+                guard let data = UIImageJPEGRepresentation(image, CGFloat(compressionQuality)) else {
+                    self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
+                    return
+                }
+                let page = PDFPage.image(UIImage(data: data)!)
+                pdfPages.append(page)
+            }
+            
+            do {
+                try PDFGenerator.generate(pdfPages, to: fileNameGenerateExport, password: password)
+            } catch {
                 self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
                 return
             }
-            let page = PDFPage.image(UIImage(data: data)!)
-            pdfPages.append(page)
         }
         
-        do {
-            try PDFGenerator.generate(pdfPages, to: fileNameGeneratePDF, password: password)
-        } catch {
-            self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
-            return
+        if fileType == "JPG" {
+            
+            guard let data = UIImageJPEGRepresentation(self.arrayImages[0], CGFloat(compressionQuality)) else {
+                self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
+                return
+            }
+            
+            do {
+                try data.write(to: NSURL.fileURL(withPath: fileNameGenerateExport), options: .atomic)
+            } catch {
+                self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
+                return
+            }
         }
         
         //Create metadata for upload

+ 1 - 1
iOSClient/Scan/ScanCollectionView.swift

@@ -71,7 +71,7 @@ class DragDropViewController: UIViewController {
         self.navigationItem.title = NSLocalizedString("_scanned_images_", comment: "")
         cancel.title = NSLocalizedString("_cancel_", comment: "")
         save.title = NSLocalizedString("_save_", comment: "")
-        labelTitlePDFzone.text = NSLocalizedString("_scan_label_PDF_zone_", comment: "")
+        labelTitlePDFzone.text = NSLocalizedString("_scan_label_document_zone_", comment: "")
         segmentControlFilter.setTitle(NSLocalizedString("_filter_grayscale_", comment: ""), forSegmentAt: 0)
         segmentControlFilter.setTitle(NSLocalizedString("_filter_bn_", comment: ""), forSegmentAt: 1)
         segmentControlFilter.setTitle(NSLocalizedString("_filter_original_", comment: ""), forSegmentAt: 2)

+ 4 - 1
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -91,6 +91,7 @@
 "_mode_filename_"           = "Filename mode";
 "_warning_owncloud_"        = "You are connected to an ownCloud server. This is untested and unsupported, use at your own risk. To upgrade to Nextcloud, see https://nextcloud.com/migration";
 "_warning_unsupported_"     = "You are using an unsupported version of Nextcloud. For the safety of your data we strongly recommend updating to the latest stable Nextcloud";
+"_file_"                    = "File";
 
 // App
 
@@ -569,7 +570,7 @@
 "_scans_document_"                  = "Scans document";
 "_scanned_images_"                  = "Scanned images";
 "_scan_document_pdf_page_"          = "Page";
-"_scan_label_PDF_zone_"             = "Drag below the images for make your PDF document";
+"_scan_label_document_zone_"        = "Drag images down for document creation";
 "_filter_original_"                 = "Original";
 "_filter_bn_"                       = "Black & White";
 "_filter_grayscale_"                = "Grayscale";
@@ -577,6 +578,8 @@
 "_quality_high_"                    = "Large file size of high quality";
 "_quality_medium_"                  = "Average file size of medium quality";
 "_quality_low_"                     = "Small file size of low quality";
+"_file_type_"                       = "File type";
+"_pdf_password_"                    = "PDF Password";
 
 /* The title on the navigation bar of the Scanning screen. */
 "wescan.scanning.title"             = "Scanning";