Skip to content

Commit

Permalink
Attachment downloads work again
Browse files Browse the repository at this point in the history
  • Loading branch information
BjarniRunar committed Jul 10, 2013
1 parent d03e11c commit 543aa5c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
4 changes: 2 additions & 2 deletions mailpile/plugins/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ class Search(Command):
TEMPLATE_ID = 'search'
class CommandResult(Command.CommandResult):
def _fixup(self):
for result in self.result:
for result in (self.result or []):
for msg in result.get('messages', []):
msg['tag_classes'] = ' '.join(['tid_%s' % t for t in msg['tag_ids']] +
['t_%s' % t.lower() for t in msg['tags']])
return self
def as_text(self):
return '\n'.join([r.as_text() for r in self.result])
return '\n'.join([r.as_text() for r in (self.result or [])])
def as_html(self):
return Command.CommandResult.as_html(self._fixup())
def as_dict(self):
Expand Down
69 changes: 36 additions & 33 deletions mailpile/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,38 @@ def display_result(self, result):
else:
return self._display_result(unicode(result))

# Creating output files
DEFAULT_DATA_NAME_FMT = '%(msg_idx)s.%(count)s_%(att_name)s.%(att_ext)s'
DEFAULT_DATA_ATTRS = {
'msg_idx': 'file',
'mimetype': 'application/octet-stream',
'att_name': 'unnamed',
'att_ext': 'dat',
'rand': '0000'
}
DEFAULT_DATA_EXTS = {
# FIXME: Add more!
'text/plain': 'txt',
'text/html': 'html',
'image/gif': 'gif',
'image/jpeg': 'jpg',
'image/png': 'png'
}
def _make_data_filename(self, name_fmt, attributes):
return (name_fmt or self.DEFAULT_DATA_NAME_FMT) % attributes
def _make_data_attributes(self, attributes={}):
attrs = self.DEFAULT_DATA_ATTRS.copy()
attrs.update(attributes)
attrs['rand'] = '%4.4x' % random.randint(0, 0xffff)
if attrs['att_ext'] == self.DEFAULT_DATA_ATTRS['att_ext']:
if attrs['mimetype'] in self.DEFAULT_DATA_EXTS:
attrs['att_ext'] = self.DEFAULT_DATA_EXTS[attrs['mimetype']]
return attrs
def open_for_data(self, name_fmt=None, attributes={}):
filename = self._make_data_filename(name_fmt,
self._make_data_attributes(attributes))
return filename, open(filename, 'w')

# Rendering helpers for templating and such
def render_json(self, data):
"""Render data as JSON"""
Expand Down Expand Up @@ -219,6 +251,10 @@ def _display_log(self, text, level=UserInteraction.LOG_URGENT):
def _display_result(self, result):
self.results.append(result)

# Stream raw data to the client on open_for_data
def open_for_data(self, name_fmt=None, attributes={}):
return 'HTTP Client', RawHttpResponder(self.request, attributes)

# Render to HTML/JSON/...
def _render_jhtml_response(self, config):
return json.dumps(default_dict(self.html_variables, {
Expand Down Expand Up @@ -422,39 +458,6 @@ def display_message(self, email, tree, raw=False, sep='', fd=None):
self.say(' [Attachment #%s]' % desc, fd=fd)
self.say('', fd=fd)

DEFAULT_DATA_NAME_FMT = '%(msg_idx)s.%(count)s_%(att_name)s.%(att_ext)s'
DEFAULT_DATA_ATTRS = {
'msg_idx': 'file',
'mimetype': 'application/octet-stream',
'att_name': 'unnamed',
'att_ext': 'dat',
'rand': '0000'
}
DEFAULT_DATA_EXTS = {
# FIXME: Add more!
'text/plain': 'txt',
'text/html': 'html',
'image/gif': 'gif',
'image/jpeg': 'jpg',
'image/png': 'png'
}
def _make_data_filename(self, name_fmt, attributes):
return (name_fmt or self.DEFAULT_DATA_NAME_FMT) % attributes

def _make_data_attributes(self, attributes={}):
attrs = self.DEFAULT_DATA_ATTRS.copy()
attrs.update(attributes)
attrs['rand'] = '%4.4x' % random.randint(0, 0xffff)
if attrs['att_ext'] == self.DEFAULT_DATA_ATTRS['att_ext']:
if attrs['mimetype'] in self.DEFAULT_DATA_EXTS:
attrs['att_ext'] = self.DEFAULT_DATA_EXTS[attrs['mimetype']]
return attrs

def open_for_data(self, name_fmt=None, attributes={}):
filename = self._make_data_filename(name_fmt,
self._make_data_attributes(attributes))
return filename, open(filename, 'w')

def edit_messages(self, emails):
self.say('Sorry, this UI cannot edit messages.')

Expand Down
1 change: 1 addition & 0 deletions static/default/html/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#debug {padding: 0; margin: 1em; text-align: right; line-height: 1em; font-size: 0.75em;}
tr.message-body {margin-top: -1em;}
div.message-body {margin-top: 1em;}
#debug {white-space: pre-wrap;}
p.text-part-text {white-space: pre-wrap;}
p.text-part-quote {white-space: pre-wrap; color: #777; max-height: 5em; overflow: hidden;}
</style>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<li><a href="./att:%(count)s"
type="%(mimetype)s">%(filename)s</a> (%(mimetype)s %(length)s bytes)</li>
1 change: 1 addition & 0 deletions static/default/html/view-result-messages-message.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<tr class='message-body'><td colspan=3>
<div class='message-headers'>%(summary)s</div>
<div class='message-body'>%(text_parts)s</div>
<ul class="message-attachments">%(attachments)s</ul>
</td><td></td></tr>

0 comments on commit 543aa5c

Please sign in to comment.