-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
26 lines (22 loc) · 778 Bytes
/
app.py
File metadata and controls
26 lines (22 loc) · 778 Bytes
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
import os
import tempfile
import subprocess as sp
from werkzeug.utils import secure_filename
from flask import Flask, request, send_file
app = Flask(__name__)
@app.route("/")
def index():
return 'Welcome to chanmo/unoserver'
@app.route("/convert/<filetype>", methods=['POST'])
def convert_to(filetype):
if 'file' not in request.files:
return {
'success': False,
'message': 'file is required.'
}
file = request.files['file']
infile = tempfile.NamedTemporaryFile()
file.save(infile.name)
outfile = tempfile.NamedTemporaryFile(suffix=f'.{filetype}')
sp.run(['unoconvert', infile.name, '--convert-to', filetype, outfile.name], check=True)
return send_file(outfile.name, mimetype=f'application/{filetype}')