← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4216: local vn - Made the validation for uploading file is more robust.

 

------------------------------------------------------------
revno: 4216
committer: Hieu <hieu.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-07-27 16:05:57 +0700
message:
  local vn - Made the validation for uploading file is more robust.
modified:
  local/vn/dhis-service-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/DefaultExportReportService.java
  local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/filemanager/action/ValidateUploadExcelFile.java
  local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module.properties
  local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module_vi_VN.properties


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'local/vn/dhis-service-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/DefaultExportReportService.java'
--- local/vn/dhis-service-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/DefaultExportReportService.java	2011-07-26 07:21:49 +0000
+++ local/vn/dhis-service-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/DefaultExportReportService.java	2011-07-27 09:05:57 +0000
@@ -78,9 +78,9 @@
         this.i18nService = i18nService;
     }
 
-    // --------------------------------------
+    // -------------------------------------------------------------------------
     // Service of Report
-    // --------------------------------------
+    // -------------------------------------------------------------------------
 
     public int addExportReport( ReportExcel report )
     {
@@ -189,9 +189,9 @@
         exportReportStore.updateReportWithExcelTemplate( curName, newName );
     }
 
-    // --------------------------------------
+    // -------------------------------------------------------------------------
     // Service of Report Item
-    // --------------------------------------
+    // -------------------------------------------------------------------------
 
     public void addExportItem( ReportExcelItem reportItem )
     {
@@ -234,9 +234,9 @@
         return exportReportStore.getSheets( reportId );
     }
 
-    // --------------------------------------
+    // -------------------------------------------------------------------------
     // Report DataElement Order
-    // --------------------------------------
+    // -------------------------------------------------------------------------
 
     public DataElementGroupOrder getDataElementGroupOrder( Integer id )
     {

=== modified file 'local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/filemanager/action/ValidateUploadExcelFile.java'
--- local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/filemanager/action/ValidateUploadExcelFile.java	2011-05-24 03:49:29 +0000
+++ local/vn/dhis-web-excel-reporting/src/main/java/org/hisp/dhis/reportexcel/filemanager/action/ValidateUploadExcelFile.java	2011-07-27 09:05:57 +0000
@@ -30,9 +30,12 @@
 import static org.apache.commons.io.FilenameUtils.getExtension;
 
 import java.io.File;
+import java.io.FileInputStream;
 
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.reportexcel.ReportLocationManager;
+import org.hisp.dhis.system.util.StreamUtils;
 
 import com.opensymphony.xwork2.Action;
 
@@ -127,6 +130,13 @@
             return ERROR;
         }
 
+        if ( isFormatBroken( uploadFileName, upload ) )
+        {
+            message = i18n.getString( "file_format_structure_broken" );
+
+            return ERROR;
+        }
+
         // Use for importing file
 
         if ( isDraft )
@@ -166,4 +176,31 @@
         return SUCCESS;
     }
 
+    // -------------------------------------------------------------------------
+    // Supportive method
+    // -------------------------------------------------------------------------
+
+    private boolean isFormatBroken( String fileName, File file )
+    {
+        if ( getExtension( fileName ).equals( "xlsx" ) )
+        {
+            try
+            {
+                File output = new File( file.getParent(), (Math.random() * 1000) + fileName );
+
+                StreamUtils.write( file, output );
+
+                new XSSFWorkbook( new FileInputStream( output ) );
+
+                return false;
+            }
+            catch ( Exception e )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
 }

=== modified file 'local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module.properties'
--- local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module.properties	2011-07-26 07:21:49 +0000
+++ local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module.properties	2011-07-27 09:05:57 +0000
@@ -291,4 +291,5 @@
 missing_template			= Warning\! Missing template.
 the_specified_report_is_not_exist = Warning\! The specified report is not available/non-existance.
 select_sheet_for_filter		= Select sheet for filter
-remove_item					= Remove item
\ No newline at end of file
+remove_item					= Remove item
+file_format_structure_broken= Warning! File format structure is broken.
\ No newline at end of file

=== modified file 'local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module_vi_VN.properties'
--- local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module_vi_VN.properties	2011-07-26 07:21:49 +0000
+++ local/vn/dhis-web-excel-reporting/src/main/resources/org/hisp/dhis/reportexcel/i18n_module_vi_VN.properties	2011-07-27 09:05:57 +0000
@@ -670,4 +670,5 @@
 PERIOD_COLUMN_LISTING		=Li\u1ec7t k\u00ea th\u1eddi \u0111i\u1ec3m theo c\u1ed9t
 upload_successful			=T\u1ea3i t\u1eadp tin th\u00e0nh c\u00f4ng \!
 override_confirm			=B\u1ea3ng m\u1eabu \u0111\u00e3 t\u1ed3n t\u1ea1i. B\u1ea1n c\u00f3 mu\u1ed1n ghi \u0111\u00e8 hay kh\u00f4ng ?
-formula_is_invalid			=Bi\u1ec3u th\u1ee9c kh\u00f4ng h\u1ee3p l\u1ec7 \!
\ No newline at end of file
+formula_is_invalid			=Bi\u1ec3u th\u1ee9c kh\u00f4ng h\u1ee3p l\u1ec7 \!
+file_format_structure_broken=C\u1ea3nh b\u00e1o! C\u1ea5u tr\u00fac \u0111\u1ecbnh d\u1ea1ng t\u1eadp tin kh\u00f4ng b\u1ecb ph\u00e1 v\u1ee1. 
\ No newline at end of file