[extensions-web] extensions: Fix handling large files



commit a6a9ec6a88a8d2ab15362d60bd28e101bdb8d951
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Sat Mar 3 05:33:20 2012 -0500

    extensions: Fix handling large files
    
    5*1024**3 is 5GB, note 5MB. Whoops.

 sweettooth/extensions/models.py                    |    2 +-
 sweettooth/extensions/testdata/TooLarge/README     |    3 +++
 .../extensions/testdata/TooLarge/TooLarge.zip      |  Bin 0 -> 6145 bytes
 .../extensions/testdata/TooLarge/metadata.json     |  Bin 0 -> 6144000 bytes
 sweettooth/extensions/tests.py                     |    5 +++++
 5 files changed, 9 insertions(+), 1 deletions(-)
---
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 72ec02b..d107134 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -248,7 +248,7 @@ def parse_zipfile_metadata(uploaded_file):
         raise InvalidExtensionData("Invalid zip file")
 
     total_uncompressed = sum(i.file_size for i in zipfile.infolist())
-    if total_uncompressed > 5*1024**3: # 5 MB
+    if total_uncompressed > 5*1024*1024: # 5 MB
         raise InvalidExtensionData("Zip file is too large")
 
     try:
diff --git a/sweettooth/extensions/testdata/TooLarge/README b/sweettooth/extensions/testdata/TooLarge/README
new file mode 100644
index 0000000..c614678
--- /dev/null
+++ b/sweettooth/extensions/testdata/TooLarge/README
@@ -0,0 +1,3 @@
+metadata.json is about 6MB worth of zeroes, just over the size limit for the
+zipfile. The idea is that any sort of basic compression, even the ones in git,
+should make the file relatively small -- the limit is the uncompressed size.
\ No newline at end of file
diff --git a/sweettooth/extensions/testdata/TooLarge/TooLarge.zip b/sweettooth/extensions/testdata/TooLarge/TooLarge.zip
new file mode 100644
index 0000000..e42c8e5
Binary files /dev/null and b/sweettooth/extensions/testdata/TooLarge/TooLarge.zip differ
diff --git a/sweettooth/extensions/testdata/TooLarge/metadata.json b/sweettooth/extensions/testdata/TooLarge/metadata.json
new file mode 100644
index 0000000..bd230ce
Binary files /dev/null and b/sweettooth/extensions/testdata/TooLarge/metadata.json differ
diff --git a/sweettooth/extensions/tests.py b/sweettooth/extensions/tests.py
index eddd8d2..2091598 100644
--- a/sweettooth/extensions/tests.py
+++ b/sweettooth/extensions/tests.py
@@ -74,6 +74,11 @@ class ParseZipfileTest(BasicUserTestCase, TestCase):
         bad_data = StringIO("deadbeef")
         self.assertRaises(models.InvalidExtensionData, models.parse_zipfile_metadata, bad_data)
 
+        with get_test_zipfile('TooLarge') as f:
+            with self.assertRaises(models.InvalidExtensionData) as cm:
+                models.parse_zipfile_metadata(f)
+            self.assertEquals(cm.exception.message, "Zip file is too large")
+
 class ReplaceMetadataTest(BasicUserTestCase, TestCase):
     @expectedFailure
     def test_replace_metadata(self):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]