11
11
from uuid import uuid4
12
12
from cStringIO import StringIO
13
13
14
- from .utils import guess_mimetype
14
+ from . import utils
15
15
16
16
17
17
CRLF = '\r \n '
@@ -37,13 +37,12 @@ def open(self, **kwargs):
37
37
self .buf .write (CRLF )
38
38
return MultipartWriter (self .buf , ** kwargs )
39
39
40
- def add (self , fn , content ):
40
+ def add (self , fn , content , headers = {} ):
41
41
self .buf .write ('--' )
42
42
self .buf .write (self .boundary )
43
43
self .buf .write (CRLF )
44
44
45
- headers = {'Content-Type' : guess_mimetype (fn )}
46
- headers .update (content_disposition_header (fn ))
45
+ headers ['Content-Type' ] = utils .guess_mimetype (fn )
47
46
48
47
self ._write_headers (headers )
49
48
if content :
@@ -96,20 +95,19 @@ def recursive(dirname, fnpattern='*'):
96
95
def walk (dirname , part ):
97
96
subpart = part .open (headers = content_disposition_header (dirname ))
98
97
subpart .write_headers ()
99
-
100
- ls = os .listdir (dirname )
101
- files = filter (lambda p : os .path .isfile (os .path .join (dirname , p )), ls )
102
- dirs = filter (lambda p : os .path .isdir (os .path .join (dirname , p )), ls )
103
98
99
+ files , subdirs = utils .ls_dir (dirname )
100
+
104
101
for fn in files :
105
102
if not fnmatch .fnmatch (fn , fnpattern ):
106
103
continue
107
104
fullpath = os .path .join (dirname , fn )
108
-
109
105
with open (fullpath , 'rb' ) as fp :
110
- subpart .add (fullpath , fp .read ())
106
+ subpart .add (fullpath ,
107
+ fp .read (),
108
+ headers = content_disposition_header (fullpath ))
111
109
112
- for subdir in dirs :
110
+ for subdir in subdirs :
113
111
fullpath = os .path .join (dirname , subdir )
114
112
walk (fullpath , subpart )
115
113
0 commit comments