-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.py
396 lines (345 loc) · 16.8 KB
/
webserver.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'''
Flask webserver for uploading and deleting audio files.
User can upload audio files to the server and delete them. Auth is required.
Uploaded audio files are converted to .wav format and saved to the data/audio folder.
'''
from flask import Flask, request, Response, render_template, redirect, url_for, session, send_from_directory, jsonify
from werkzeug.utils import secure_filename
import os
import subprocess
import secrets
import glob
import shutil
from pydub import AudioSegment
from pydub.playback import play
from dotenv import load_dotenv
load_dotenv()
class WebApp:
def __init__(self):
self.credentials = {
'username': os.getenv('WEBPAGE_USERNAME'),
'password': os.getenv('WEBPAGE_PASSWORD')
}
self.allowed_extentions = {'mp3', 'wav'}
self.upload_folder = os.getenv('UPLOAD_FOLDER') if os.getenv('UPLOAD_FOLDER') else './data/audio'
self.greetings_folder = './data/greetings'
self.host = os.getenv('WEBPAGE_HOST') if os.getenv('WEBPAGE_HOST') else 'localhost'
self.port = int(os.getenv('WEBPAGE_PORT')) if os.getenv('WEBPAGE_PORT') else 5100
self.ssl_context = None
# Setup SSL if certificates are provided
cert_path = os.getenv('SSL_CERT')
key_path = os.getenv('SSL_KEY')
if cert_path and key_path and os.path.exists(cert_path) and os.path.exists(key_path):
self.ssl_context = (cert_path, key_path)
self.app = Flask(__name__)
self.app.config['UPLOAD_FOLDER'] = self.upload_folder
self.app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB
self.app.add_url_rule('/', 'index', self.index, methods=['GET', 'POST'])
self.app.add_url_rule('/upload', 'upload', self.upload, methods=['GET', 'POST'])
self.app.add_url_rule('/upload_greeting', 'upload_greeting', self.upload_greeting, methods=['POST'])
self.app.add_url_rule('/set_greeting', 'set_greeting', self.set_greeting, methods=['POST'])
self.app.add_url_rule('/set_version_as_current', 'set_version_as_current', self.set_version_as_current, methods=['POST'])
self.app.add_url_rule('/delete', 'delete', self.delete, methods=['GET', 'POST'])
self.app.add_url_rule('/login', 'login', self.login, methods=['GET', 'POST'])
self.app.add_url_rule('/logout', 'logout', self.logout, methods=['GET', 'POST'])
self.app.add_url_rule('/play', 'play_audio', self.play_audio, methods=['GET'])
self.app.add_url_rule('/get_greetings', 'get_greetings', self.get_greetings, methods=['GET'])
# set secret key
self.app.secret_key = secrets.token_hex(16)
# add template location
self.app.template_folder = './templates'
# Create necessary directories if they don't exist
os.makedirs(self.upload_folder, exist_ok=True)
os.makedirs(self.greetings_folder, exist_ok=True)
def run(self):
'''
Starts the webserver with optional SSL support.
'''
self.app.run(
host=self.host,
port=self.port,
debug=False,
threaded=True,
ssl_context=self.ssl_context
)
def get_greeting_versions(self, username):
'''
Get all versions of greeting files for a username
'''
pattern = os.path.join(self.greetings_folder, f"{username}.*.wav")
files = glob.glob(pattern)
return sorted(files, key=lambda x: int(x.split('.')[-2]) if x.split('.')[-2].isdigit() else 0)
def get_next_version(self, username):
'''
Get the next version number for a user's greeting file
'''
existing_versions = self.get_greeting_versions(username)
if not existing_versions:
return 1
last_version = max([int(f.split('.')[-2]) for f in existing_versions if f.split('.')[-2].isdigit()])
return last_version + 1
def backup_existing_greeting(self, username):
'''
Backup existing greeting file with versioning
'''
current_file = os.path.join(self.greetings_folder, f"{username}.wav")
if os.path.exists(current_file):
next_version = self.get_next_version(username)
backup_file = os.path.join(self.greetings_folder, f"{username}.{next_version}.wav")
os.rename(current_file, backup_file)
return True
return False
def set_version_as_current(self):
'''
Sets a specific version as the current greeting for a user
'''
if not session.get('logged_in'):
return jsonify({'error': 'Not authorized'}), 401
data = request.get_json()
if not data or 'username' not in data or 'version' not in data:
return jsonify({'error': 'Missing required fields'}), 400
username = secure_filename(data['username'])
version = data['version']
# Source file is the versioned file
source_file = os.path.join(self.greetings_folder, f"{username}.{version}.wav")
if not os.path.exists(source_file):
return jsonify({'error': 'Source version not found'}), 404
# Backup current if it exists
current_file = os.path.join(self.greetings_folder, f"{username}.wav")
if os.path.exists(current_file):
self.backup_existing_greeting(username)
# Copy the versioned file as the new current
try:
shutil.copy2(source_file, current_file)
return jsonify({'success': True, 'message': f'Version {version} set as current greeting for {username}'})
except Exception as e:
return jsonify({'error': f'Failed to set version as current: {str(e)}'}), 500
def filelist(self, filter=('wav'), folder=None):
'''
Returns a list of all files in the specified folder, while filtering for the given file extension.
'''
target_folder = folder if folder else self.app.config['UPLOAD_FOLDER']
files = []
for file in os.listdir(target_folder):
if file.endswith(filter):
files.append(file)
files.sort()
return files
def get_greetings(self):
'''
Returns a list of all greeting files with their associated usernames
'''
if not session.get('logged_in'):
return jsonify({'error': 'Not authorized'}), 401
greetings = []
for file in os.listdir(self.greetings_folder):
if file.endswith('.wav'):
parts = file.rsplit('.', 2)
if len(parts) == 2: # Current greeting file
username = parts[0]
greetings.append({
'username': username,
'filename': file,
'is_current': True
})
elif len(parts) == 3 and parts[1].isdigit(): # Versioned backup
username = parts[0]
version = parts[1]
greetings.append({
'username': username,
'filename': file,
'version': version,
'is_current': False
})
return jsonify(greetings)
def index(self):
'''
Renders index.html with a list of all uploaded files.
'''
if not session.get('logged_in'):
return redirect(url_for('login'))
else:
soundboard_files = self.filelist()
greeting_files = self.filelist(folder=self.greetings_folder)
return render_template('index.html',
uploaded_files=soundboard_files,
greeting_files=greeting_files)
def login(self):
'''
Displays login page when user is not logged in.
'''
if request.method == 'POST':
if request.form['username'] == self.credentials['username'] and request.form['password'] == self.credentials['password']:
session['logged_in'] = True
return redirect(url_for('index'))
else:
return render_template('login.html', error='Invalid credentials.')
else:
return render_template('login.html')
def logout(self):
'''
Logs user out.
'''
session['logged_in'] = False
return redirect(url_for('index'))
def upload(self):
'''
Lets user upload audio files to the soundboard.
'''
if not session.get('logged_in'):
return redirect(url_for('login'))
else:
if request.method == 'POST':
if 'file' not in request.files:
return render_template('index.html', error='No file selected.')
file = request.files['file']
if file.filename == '':
return render_template('index.html', error='No file selected.')
if file and self.allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(self.app.config['UPLOAD_FOLDER'], filename))
success = self.convert(filename)
if success:
return render_template('index.html',
success='File uploaded.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
os.remove(os.path.join(self.app.config['UPLOAD_FOLDER'], filename))
return render_template('index.html',
error='Something went wrong while converting the file.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
return render_template('index.html',
error='Invalid file extension.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
return render_template('index.html')
def set_greeting(self):
'''
Sets an existing sound as a user's greeting sound
'''
if not session.get('logged_in'):
return jsonify({'error': 'Not authorized'}), 401
data = request.get_json()
if not data or 'username' not in data or 'filename' not in data:
return jsonify({'error': 'Missing required fields'}), 400
username = secure_filename(data['username'])
source_file = os.path.join(self.upload_folder, data['filename'])
if not os.path.exists(source_file):
return jsonify({'error': 'Source file not found'}), 404
# Backup existing greeting if it exists
self.backup_existing_greeting(username)
# Copy the selected sound as the new greeting
target_file = os.path.join(self.greetings_folder, f"{username}.wav")
try:
shutil.copy2(source_file, target_file)
return jsonify({'success': True, 'message': f'Greeting set for {username}'})
except Exception as e:
return jsonify({'error': f'Failed to set greeting: {str(e)}'}), 500
def upload_greeting(self):
'''
Lets user upload greeting audio files for specific Discord users.
'''
if not session.get('logged_in'):
return redirect(url_for('login'))
if request.method == 'POST':
if 'file' not in request.files:
return render_template('index.html', error='No file selected.')
file = request.files['file']
discord_username = request.form.get('discord_username', '').strip()
if file.filename == '' or not discord_username:
return render_template('index.html', error='Both file and Discord username are required.')
if file and self.allowed_file(file.filename):
# Backup existing greeting if it exists
self.backup_existing_greeting(discord_username)
# Save new greeting
filename = secure_filename(f"{discord_username}.wav")
file_path = os.path.join(self.greetings_folder, filename)
file.save(file_path)
success = self.convert(filename, folder=self.greetings_folder)
if success:
return render_template('index.html',
success=f'Greeting sound for {discord_username} uploaded successfully.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
if os.path.exists(file_path):
os.remove(file_path)
return render_template('index.html',
error='Something went wrong while converting the file.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
return render_template('index.html',
error='Invalid file extension.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
def play_audio(self):
'''
Serves the audio file with the given filename.
'''
if not session.get('logged_in'):
return redirect(url_for('login'))
else:
if request.method == 'GET':
filename = request.args.get('filename')
folder = request.args.get('folder', 'soundboard')
target_folder = self.greetings_folder if folder == 'greetings' else self.app.config['UPLOAD_FOLDER']
if filename in self.filelist(folder=target_folder):
return send_from_directory(target_folder, filename)
else:
return 'File not found', 404
else:
return 'Invalid request', 400
def delete(self):
'''
Deletes audio file from server.
'''
if not session.get('logged_in'):
return redirect(url_for('login'))
else:
if request.method == 'GET':
filename = request.args.get('filename')
folder = request.args.get('folder', 'soundboard')
target_folder = self.greetings_folder if folder == 'greetings' else self.app.config['UPLOAD_FOLDER']
file_path = os.path.join(target_folder, filename)
if os.path.exists(file_path):
os.remove(file_path)
return render_template('index.html',
success='File deleted.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
return render_template('index.html',
error='File not found.',
uploaded_files=self.filelist(),
greeting_files=self.filelist(folder=self.greetings_folder))
else:
return render_template('index.html')
def allowed_file(self, filename):
'''
Checks if file extension is allowed.
'''
return '.' in filename and filename.rsplit('.', 1)[1].lower() in self.allowed_extentions
def convert(self, filename, folder=None, loudness=-16):
'''
Converts audio file to .wav format with volume normalization.
'''
target_folder = folder if folder else self.app.config['UPLOAD_FOLDER']
file_path = os.path.join(target_folder, filename)
if os.path.exists(file_path):
output_path = os.path.join(target_folder, filename.replace('.mp3', '.wav'))
subprocess.call(
['ffmpeg', '-i', file_path, '-af', f'loudnorm=I={loudness}', '-ar', '48000', '-ac', '2', '-y', output_path]
)
# Remove original file if it's different from the output
if file_path != output_path:
os.remove(file_path)
return True
else:
return False
if __name__ == "__main__":
WebApp()