Skip to content

Commit 4473b09

Browse files
committed
Save attachment data as binary (fixes #246)
1 parent 3dd954d commit 4473b09

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

couchdb/tools/dump.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
BULK_SIZE = 1000
2424

25-
def dump_docs(envelope, docs):
25+
def dump_docs(envelope, db, docs):
2626
for doc in docs:
2727

2828
print >> sys.stderr, 'Dumping document %r' % doc.id
@@ -35,14 +35,19 @@ def dump_docs(envelope, docs):
3535
'ETag': '"%s"' % doc.rev
3636
})
3737
parts.add('application/json', jsondoc)
38-
3938
for name, info in attachments.items():
39+
4040
content_type = info.get('content_type')
4141
if content_type is None: # CouchDB < 0.8
4242
content_type = info.get('content-type')
43-
parts.add(content_type, b64decode(info['data']), {
44-
'Content-ID': name
45-
})
43+
44+
if 'data' not in info:
45+
data = db.get_attachment(doc, name).read()
46+
else:
47+
data = b64decode(info['data'])
48+
49+
parts.add(content_type, data, {'Content-ID': name})
50+
4651
parts.close()
4752

4853
else:
@@ -62,7 +67,8 @@ def dump_db(dburl, username=None, password=None, boundary=None,
6267
start, num = 0, db.info()['doc_count']
6368
while start < num:
6469
opts = {'limit': bulk_size, 'skip': start, 'include_docs': True}
65-
dump_docs(envelope, [row.doc for row in db.view('_all_docs', **opts)])
70+
docs = (row.doc for row in db.view('_all_docs', **opts))
71+
dump_docs(envelope, db, docs)
6672
start += bulk_size
6773

6874
envelope.close()

0 commit comments

Comments
 (0)