Commits

Jun Omae committed b770204

In the physical filename of a attachment, we should keep the only alphanumeric extension.

Comments (0)

Files changed (2)

trac/attachment.py

             path = os.path.join(path, self._get_hashed_filename(filename))
         return os.path.normpath(path)
 
+    _extension_re = re.compile(r'\.[A-Za-z0-9]+\Z')
+
     def _get_hashed_filename(self, filename):
         hash = sha1(filename.encode('utf-8')).hexdigest()
-        parts = os.path.splitext(filename)
-        return hash + parts[1] if parts[1] else hash
+        match = self._extension_re.search(filename)
+        return hash + match.group(0) if match else hash
     
     def _get_path_old(self, parent_realm, parent_id, filename):
         path = os.path.join(self.env.path, 'attachments', parent_realm,

trac/tests/attachment.py

 
 hashes = {
     '42': '92cfceb39d57d914ed8b14d0e37643de0797ae56',
+    'Foo.Mp3': '95797b6eb253337ff2c54e0881e2b747ec394f51',
     'SomePage': 'd7e80bae461ca8568e794792f5520b603f540e06',
     'Teh bar.jpg': 'ed9102c4aa099e92baf1073f824d21c5e4be5944',
     'Teh foo.txt': 'ab97ba98d98fcf72b92e33a66b07077010171f70',
+    'bar.7z': '6c9600ad4d59ac864e6f0d2030c1fc76b4b406cb',
     'bar.jpg': 'ae0faa593abf2b6f8871f6f32fe5b28d1c6572be',
+    'foo.$$$': 'eefc6aa745dbe129e8067a4a57637883edd83a8a',
+    'foo.2.txt': 'a8fcfcc2ef4e400ee09ae53c1aabd7f5a5fda0c7',
     'foo.txt': '9206ac42b532ef8e983470c251f4e1a365fd636c',
-    'foo.2.txt': 'a8fcfcc2ef4e400ee09ae53c1aabd7f5a5fda0c7',
+    u'bar.aäc': '70d0e3b813fdc756602d82748719a3ceb85cbf29',
     u'ÜberSicht': 'a16c6837f6d3d2cc3addd68976db1c55deb694c8',
 }
 
                                       hashes['bar.jpg'] + '.jpg'),
                          attachment.path)
 
+    def test_path_extension(self):
+        attachment = Attachment(self.env, 'ticket', 42)
+        attachment.filename = 'Foo.Mp3'
+        self.assertEqual(os.path.join(self.attachments_dir, 'ticket',
+                                      hashes['42'][0:3], hashes['42'],
+                                      hashes['Foo.Mp3'] + '.Mp3'),
+                         attachment.path)
+        attachment = Attachment(self.env, 'wiki', 'SomePage')
+        attachment.filename = 'bar.7z'
+        self.assertEqual(os.path.join(self.attachments_dir, 'wiki',
+                                      hashes['SomePage'][0:3],
+                                      hashes['SomePage'],
+                                      hashes['bar.7z'] + '.7z'),
+                         attachment.path)
+        attachment = Attachment(self.env, 'ticket', 42)
+        attachment.filename = 'foo.$$$'
+        self.assertEqual(os.path.join(self.attachments_dir, 'ticket',
+                                      hashes['42'][0:3], hashes['42'],
+                                      hashes['foo.$$$']),
+                         attachment.path)
+        attachment = Attachment(self.env, 'wiki', 'SomePage')
+        attachment.filename = u'bar.aäc'
+        self.assertEqual(os.path.join(self.attachments_dir, 'wiki',
+                                      hashes['SomePage'][0:3],
+                                      hashes['SomePage'],
+                                      hashes[u'bar.aäc']),
+                         attachment.path)
+
     def test_get_path_encoded(self):
         attachment = Attachment(self.env, 'ticket', 42)
         attachment.filename = 'Teh foo.txt'