|
| 1 | +import json |
1 | 2 | import os |
2 | 3 |
|
3 | 4 | from mako.lookup import TemplateLookup |
@@ -67,16 +68,30 @@ def _check_file(self, zip_file): |
67 | 68 | """ |
68 | 69 | # Extract manifest file content |
69 | 70 | try: |
70 | | - with zip_file.open('META-INF/MANIFEST.MF') as manifest_data: |
71 | | - manifest = manifest_data.read().decode('utf-8') |
| 71 | + try: |
| 72 | + # new manifest location |
| 73 | + with zip_file.open('manifest.json') as manifest_data: |
| 74 | + manifest, flavor = manifest_data.read().decode('utf-8'), 'json' |
| 75 | + except KeyError: |
| 76 | + # old manifest location |
| 77 | + with zip_file.open('META-INF/MANIFEST.MF') as manifest_data: |
| 78 | + manifest, flavor = manifest_data.read().decode('utf-8'), 'java' |
72 | 79 | except KeyError: |
73 | 80 | raise exceptions.JaspFileCorruptError( |
74 | | - '{} Missing META-INF/MANIFEST.MF'.format(self.MESSAGE_FILE_CORRUPT), |
| 81 | + '{} Missing manifest'.format(self.MESSAGE_FILE_CORRUPT), |
75 | 82 | extension=self.metadata.ext, |
76 | 83 | corruption_type='key_error', |
77 | | - reason='zip missing ./META-INF/MANIFEST.MF', |
| 84 | + reason='zip missing manifest', |
78 | 85 | ) |
79 | 86 |
|
| 87 | + if flavor == 'java': |
| 88 | + self._verify_java_manifest(manifest) |
| 89 | + else: |
| 90 | + self._verify_json_manifest(manifest) |
| 91 | + |
| 92 | + return True |
| 93 | + |
| 94 | + def _verify_java_manifest(self, manifest): |
80 | 95 | lines = manifest.split('\n') |
81 | 96 |
|
82 | 97 | # Search for Data-Archive-Version |
@@ -121,4 +136,41 @@ def _check_file(self, zip_file): |
121 | 136 | reason='Data-Archive-Version ({}) not parsable.'.format(dataArchiveVersionStr), |
122 | 137 | ) |
123 | 138 |
|
124 | | - return True |
| 139 | + return |
| 140 | + |
| 141 | + def _verify_json_manifest(self, manifest): |
| 142 | + |
| 143 | + manifest_data = json.loads(manifest) |
| 144 | + |
| 145 | + jasp_archive_version_str = manifest_data.get('jaspArchiveVersion', None) |
| 146 | + if not jasp_archive_version_str: |
| 147 | + raise exceptions.JaspFileCorruptError( |
| 148 | + '{} jaspArchiveVersion not found.'.format(self.MESSAGE_FILE_CORRUPT), |
| 149 | + extension=self.metadata.ext, |
| 150 | + corruption_type='manifest_parse_error', |
| 151 | + reason='jaspArchiveVersion not found.', |
| 152 | + ) |
| 153 | + |
| 154 | + # Check that the file is new enough (contains preview content) |
| 155 | + jasp_archive_version = LooseVersion(jasp_archive_version_str) |
| 156 | + try: |
| 157 | + if jasp_archive_version < self.MINIMUM_VERSION: |
| 158 | + minimum_version = self.MINIMUM_VERSION.vstring |
| 159 | + data_archive_version = jasp_archive_version.vstring |
| 160 | + raise exceptions.JaspVersionError( |
| 161 | + 'This JASP file was created with an older data archive ' |
| 162 | + 'version ({}) and cannot be previewed. Minimum data archive ' |
| 163 | + 'version is {}.'.format(data_archive_version, minimum_version), |
| 164 | + extension=self.metadata.ext, |
| 165 | + actual_version=data_archive_version, |
| 166 | + required_version=minimum_version, |
| 167 | + ) |
| 168 | + except TypeError: |
| 169 | + raise exceptions.JaspFileCorruptError( |
| 170 | + '{} jaspArchiveVersion not parsable.'.format(self.MESSAGE_FILE_CORRUPT), |
| 171 | + extension=self.metadata.ext, |
| 172 | + corruption_type='manifest_parse_error', |
| 173 | + reason='jaspArchiveVersion ({}) not parsable.'.format(jasp_archive_version_str), |
| 174 | + ) |
| 175 | + |
| 176 | + return |
0 commit comments