-
Notifications
You must be signed in to change notification settings - Fork 2
/
msgconvert.py
142 lines (111 loc) · 4.2 KB
/
msgconvert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
"""
msgconvert server
A tiny aiohttp based web server that wraps the msgconvert Perl script.
It expects a multipart/form-data upload containing a .msg file with the name
msg and returns the converted eml file.
"""
from aiohttp import web
import asyncio
import tempfile
import os.path
import logging
CHUNK_SIZE = 65536
logger = logging.getLogger('msgconvert')
async def msgconvert(request):
form_data = {}
temp_dir = None
if not request.content_type == 'multipart/form-data':
logger.info(
'Bad request. Received content type %s instead of multipart/form-data.',
request.content_type,
)
return web.Response(status=400, text="Multipart request required.")
reader = await request.multipart()
with tempfile.TemporaryDirectory() as temp_dir:
while True:
part = await reader.next()
if part is None:
break
if part.name == 'msg':
form_data['msg'] = await save_part_to_file(part, temp_dir)
if 'msg' in form_data:
outfilename = os.path.join(
temp_dir, os.path.basename(form_data['msg']) + '.eml')
proc, stdout, stderr = await run(
'msgconvert', '--outfile', outfilename, form_data['msg'].encode(),
timeout=request.app['config']['call_timeout'],
)
if proc is not None and proc.returncode == 0:
response = web.StreamResponse(
status=200,
reason='OK',
headers={'Content-Type': 'message/rfc822'},
)
await response.prepare(request)
with open(outfilename, 'rb') as outfile:
while True:
data = outfile.read(CHUNK_SIZE)
if not data:
break
await response.write(data)
await response.write_eof()
return response
else:
if proc is None:
logger.error('Conversion failed.')
return web.Response(
status=500, text='Conversion failed.')
else:
logger.error('Conversion failed. %s', stderr)
return web.Response(
status=500, text=f"Conversion failed. {stderr}")
logger.info('Bad request. No msg provided.')
return web.Response(status=400, text="No msg provided.")
async def save_part_to_file(part, directory):
filename = os.path.join(directory, part.filename)
with open(os.path.join(directory, filename), 'wb') as file_:
while True:
chunk = await part.read_chunk(CHUNK_SIZE)
if not chunk:
break
file_.write(chunk)
return filename
async def healthcheck(request):
return web.Response(status=200, text="OK")
async def run(*cmd, input=None, timeout=30, encoding='utf8'):
try:
proc = await asyncio.create_subprocess_exec(
*cmd,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await asyncio.wait_for(
proc.communicate(input=input), timeout=timeout)
except asyncio.exceptions.TimeoutError:
logger.error('Calling %s timed out.', cmd)
return None
except Exception:
logger.exception('Calling %s failed', cmd)
return None
return proc, stdout.decode(encoding), stderr.decode(encoding)
def get_config():
config = {}
try:
call_timeout = int(os.environ.get('MSGCONVERT_CALL_TIMEOUT', '30'))
except (ValueError, TypeError):
call_timeout = 30
config['call_timeout'] = call_timeout
return config
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s %(levelname)s %(name)s %(message)s',
level=logging.INFO,
)
app = web.Application()
app['config'] = get_config()
logger.info('Using config=%s', app['config'])
app.add_routes([web.post('/', msgconvert)])
app.add_routes([web.get('/healthcheck', healthcheck)])
web.run_app(app)