Skip to content

Commit 1ba5dcf

Browse files
committed
minor reorg
1 parent eb3d1b1 commit 1ba5dcf

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

ipfsApi/filestream.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from uuid import uuid4
1212
from cStringIO import StringIO
1313

14-
from .utils import guess_mimetype
14+
from . import utils
1515

1616

1717
CRLF = '\r\n'
@@ -37,13 +37,12 @@ def open(self, **kwargs):
3737
self.buf.write(CRLF)
3838
return MultipartWriter(self.buf, **kwargs)
3939

40-
def add(self, fn, content):
40+
def add(self, fn, content, headers={}):
4141
self.buf.write('--')
4242
self.buf.write(self.boundary)
4343
self.buf.write(CRLF)
4444

45-
headers = {'Content-Type': guess_mimetype(fn)}
46-
headers.update(content_disposition_header(fn))
45+
headers['Content-Type'] = utils.guess_mimetype(fn)
4746

4847
self._write_headers(headers)
4948
if content:
@@ -96,20 +95,19 @@ def recursive(dirname, fnpattern='*'):
9695
def walk(dirname, part):
9796
subpart = part.open(headers=content_disposition_header(dirname))
9897
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)
10398

99+
files, subdirs = utils.ls_dir(dirname)
100+
104101
for fn in files:
105102
if not fnmatch.fnmatch(fn, fnpattern):
106103
continue
107104
fullpath = os.path.join(dirname, fn)
108-
109105
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))
111109

112-
for subdir in dirs:
110+
for subdir in subdirs:
113111
fullpath = os.path.join(dirname, subdir)
114112
walk(fullpath, subpart)
115113

ipfsApi/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ def parse_pyobj(pickled):
2727
def guess_mimetype(filename):
2828
fn = os.path.basename(filename)
2929
return mimetypes.guess_type(fn)[0] or 'application/octet-stream'
30+
31+
32+
def ls_dir(dirname):
33+
ls = os.listdir(dirname)
34+
files = filter(lambda p: os.path.isfile(os.path.join(dirname, p)), ls)
35+
dirs = filter(lambda p: os.path.isdir(os.path.join(dirname, p)), ls)
36+
return files, dirs
37+
38+

0 commit comments

Comments
 (0)