-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
103 lines (79 loc) · 3.13 KB
/
app.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
import logging
import ntpath
import os
import socket
import webbrowser
from io import BytesIO
from zipfile import ZipFile
import magic
from flask import Flask, redirect, render_template, request, send_file, send_from_directory, session
from waitress import serve
import utility
app = Flask(__name__)
app.secret_key = 'WiPi'
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)
@app.route('/')
def index():
return render_template('layout_main.html', Sections=utility.Sections)
@app.route('/<section_name>')
def layout_item(section_name):
sections = [section.name for section in utility.Sections]
for section in utility.Sections:
if section.name == section_name:
if len(section.item_list) > 0:
return render_template('layout_items.html', section_name=section_name,
item_list=section.item_list, sections=sections, get_name=ntpath.basename)
else:
return render_template('layout_error.html', sections=sections, message='This Section Is Empty')
return render_template('layout_error.html', sections=sections, message='No Such Section')
@app.route('/select', methods=['GET', 'POST'])
def select():
if request.method == 'POST':
session['item'] = request.form.getlist('item')
if session['item']:
return render_template('layout_download_open.html')
else:
return render_template('layout_error.html', message='Error')
else:
return render_template('layout_error.html', message='Error')
@app.route('/download_open/<download_open>')
def download_play(download_open):
items = session['item']
if download_open == 'download':
if len(items) == 1:
item = items[0]
return send_file(item, as_attachment=True)
else:
stream = BytesIO()
with ZipFile(stream, 'w') as zipfile:
for item in items:
zipfile.write(item, ntpath.basename(item))
stream.seek(0)
return send_file(stream, as_attachment=True, download_name='items.zip')
elif download_open == 'open':
return redirect('/open')
else:
return render_template('layout_error.html', message='Error')
@app.route('/open')
def open_app():
items = session['item']
mime = magic.Magic(mime=True)
sections = [section.name for section in utility.Sections]
return render_template('layout_open.html', items=items, mime_from_file=mime.from_file,
sections=sections, get_name=ntpath.basename,
get_text=utility.get_text, get_text_pdf=utility.get_text_pdf)
@app.route('/item_image/<path:item_name>')
def get_item(item_name):
return send_from_directory(utility.PATH, item_name, as_attachment=True)
@app.route('/close')
def close_app():
os._exit(0)
if __name__ == '__main__':
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
port = 8080
url = f'http://{local_ip}:{port}/'
webbrowser.open_new(url)
serve(app, host=local_ip, port=port)
# app.run(host='0.0.0.0', port=port) # for docker/testing