-
Notifications
You must be signed in to change notification settings - Fork 9
/
flaskapp.py
154 lines (120 loc) · 4.11 KB
/
flaskapp.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
143
144
145
146
147
148
149
150
151
152
153
# Version: MPL 1.1
#
# This file is part of the LibreOffice project.
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# Contributor(s):
# Rasmus P J <wasmus@zom.bi>
#
import os
from flask import Flask, request, render_template, redirect, send_file
import libresign.config as config
from libresign.request import Request
app = Flask(__name__)
web = None
def run (web_):
global web
web = web_
app.run(debug=True, use_reloader=False, threaded=True, port=config.HTTP_PORT, host="0.0.0.0")
# in case we want to add some more bells and whistles
def file_request (request_type):
file_id = None
if request.form.get("file"):
file_id = request.form.get("file")
push_request({"type" : request_type,
"file" : file_id})
def push_request (request):
web.push_request(request)
def upload_file (file):
name = safe_filename(file.filename)
if check_filetype(file) != 0:
return
if name == '':
print("no filename")
return
file.save(os.path.join(config.SAVE_FOLDER, name))
file_request(Request.ADD_FILE)
print ("uploaded", name)
def file_exists (filename):
return 0
# replace all non-alphanumerics with _ which is quite extreme but:
# TODO this ruins a lot of file names, especially parentheses, brackets and hyphens
# are likely to be used in the names of files that we are handling, i think
# https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations
def safe_filename (filename):
def clean(c):
if c.isalnum() or c == '.':
return c
else:
return '_'
parts = ''.join([clean(c) for c in filename.rstrip(' ')]).split('.')
end = ''
if len(parts) > 1:
end = '.' + parts[-1]
parts = parts[0:-1]
safe = ''.join(parts) + end
return safe
def check_filetype (filename):
# TODO
return 0
@app.route('/', methods=['GET'])
def index():
playlist = web.get_playlist()
files = web.get_all_files()
playing = web.get_current_playlist_item()
return render_template('index.html', playlist=playlist, files=files, currently_playing=playing)
@app.route('/upload', methods=['POST'])
def upload():
if request.method == 'POST':
if 'file' in request.files:
upload_file(request.files['file'])
else:
print("no file")
return redirect('/')
@app.route('/download/<filename>', methods=['GET'])
def download(filename):
return send_file(os.path.join(config.SAVE_FOLDER, filename))
@app.route('/remove_file', methods=['POST'])
def remove():
file_request(Request.REMOVE_FILE)
return redirect('/')
@app.route('/play_file', methods=['POST'])
def play_file():
file_request(Request.PLAY_FILE)
return redirect('/')
# Change playlist order of a file already in the playlist
@app.route('/order', methods=['POST'])
def order():
from_index = request.form.get("from")
to_index = request.form.get("to")
if from_index and to_index:
push_request({"type" : Request.ORDER,
"from" : int(from_index),
"to" : int(to_index)})
return redirect('/')
@app.route('/queue_file', methods=['POST'])
def queue_file():
to_index = request.form.get("to")
filename = request.form.get("file")
if to_index and filename:
push_request({"type" : Request.QUEUE_FILE,
"to" : int(to_index),
"file" : filename})
return redirect('/')
@app.route('/play', methods=['POST'])
def player():
file_request(Request.PLAY)
return redirect('/')
@app.route('/pause', methods=['POST'])
def pause():
file_request(Request.PAUSE)
return redirect('/')