fixes in handling file name with special characters
authorMaciej Tronowski <mtro@man.poznan.pl>
Wed, 2 Sep 2015 15:24:55 +0000 (17:24 +0200)
committerDawid Jagieła <lightnir@gmail.com>
Sat, 12 Sep 2015 10:08:49 +0000 (12:08 +0200)
filex/ftp.py

index 3ccd8c6..85161a4 100644 (file)
@@ -6,7 +6,7 @@ import re
 from threading import Event
 from urlparse import urlparse, urlunparse
 
-from django.utils.http import urlunquote
+from django.utils.http import urlquote
 from django.utils.timezone import localtime, UTC
 
 
@@ -190,15 +190,22 @@ class FTPOperation:
         self._check_disk_stack_args(*([path, archive] + files))
 
         if self.match_ext(archive, '.tar.gz', '.tgz'):
-            cmd, args = 'tar', ['cvzf', archive, '-C', path] + files
+            cmd, args = 'tar', ['czf', archive, '-C', path] + files
         elif self.match_ext(archive, '.tar.bz2', '.tbz'):
-            cmd, args = 'tar', ['cvjf', archive, '-C', path] + files
+            cmd, args = 'tar', ['cjf', archive, '-C', path] + files
         elif self.match_ext(archive, '.zip'):
-            cmd, args = 'jar', (['cvMf', archive] + list(chain.from_iterable(('-C', path, f) for f in files)))
+            # zip doesn't support unicode file names
+            for arg in files:
+                try:
+                    arg.encode('ascii')
+                except UnicodeEncodeError as e:
+                    raise ValueError(u'Unsupported character `{}` in `{}`!'.format(arg[e.start:e.start + 1], arg))
+
+            cmd, args = 'jar', (['cMf', archive] + list(chain.from_iterable(('-C', path, f) for f in files)))
         else:
             raise ValueError('Unknown archive type: {}'.format(archive))
 
-        self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + args))
+        self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + map(urlquote, args)))
 
         return self.get(server)
 
@@ -206,21 +213,21 @@ class FTPOperation:
         self._check_disk_stack_args(*[archive, dst])
 
         if self.match_ext(archive, '.tar.gz', '.tgz'):
-            cmd, args = 'tar', ('xvzf', archive, '-C', dst)
+            cmd, args = 'tar', ('xzf', archive, '-C', dst)
         elif self.match_ext(archive, '.tar.bz2', '.tbz'):
-            cmd, args = 'tar', ('xvjf', archive, '-C', dst)
+            cmd, args = 'tar', ('xjf', archive, '-C', dst)
         elif self.match_ext(archive, '.zip'):
-            cmd, args = 'unzip', (archive, '-d', dst)
+            cmd, args = 'unzip', ('-qo', archive, '-d', dst)
         else:
             raise ValueError('Unknown archive type: {}'.format(archive))
 
-        self.op_attr.set_disk_stack('#'.join(("popen:argv=", cmd) + args))
+        self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + map(urlquote, args)))
 
         return self.get(server)
 
     @staticmethod
     def _check_disk_stack_args(*args):
-        for char in ['#', ',', ';', '%23', '%3B']:
+        for char in ['#', ';']:
             for arg in args:
                 if char in arg:
-                    raise ValueError('Unsupported character `{}` in `{}`!'.format(urlunquote(char), urlunquote(arg)))
+                    raise ValueError(u'Unsupported character `{}` in `{}`!'.format(char, arg))