@@ -59,7 +59,7 @@ def load_config():
5959 raise
6060
6161
62- load_config ()
62+ load_config ()
6363VIDEO_DIR = os .path .expanduser (settings ["VIDEO_DIR" ])
6464SERVER_PORT = settings ["SERVER_PORT" ]
6565
@@ -153,6 +153,12 @@ def edit(filename):
153153
154154@app .route ("/videos" )
155155def videos ():
156+
157+ global settings
158+ load_config ()
159+ VIDEO_DIR = os .path .expanduser (settings ["VIDEO_DIR" ])
160+
161+
156162 """List video files in the video directory."""
157163 video_files = [
158164 f for f in os .listdir (VIDEO_DIR ) if f .endswith ((".mp4" , ".mkv" , ".avi" ))
@@ -274,25 +280,49 @@ def settings_view():
274280
275281 if request .method == "POST" :
276282 try :
277- config_files_names = request .form .getlist ("config_files_names" )
278- config_files_paths = request .form .getlist ("config_files_paths" )
283+ current_config_files = settings .get ("config_files" , [])
284+ updated_config_files = current_config_files .copy () # Start with the current files
285+
286+ # Log all form data for debugging
287+ logger .debug (f"Form Data: { request .form } " )
288+
289+ # Handle deletion
290+ files_to_delete = request .form .getlist ("delete_files" )
291+ logger .debug (f"Files to delete: { files_to_delete } " )
292+ if files_to_delete :
293+ updated_config_files = [
294+ file for file in updated_config_files if file ["name" ] not in files_to_delete
295+ ]
296+
297+ # Count how many config file inputs there are
298+ new_file_count = sum (1 for key in request .form .keys () if key .startswith ("config_files[" ) and "][name]" in key )
299+ logger .debug (f"New config file count: { new_file_count } " )
300+
301+ for i in range (new_file_count ):
302+ name = request .form .get (f'config_files[{ i } ][name]' )
303+ path = request .form .get (f'config_files[{ i } ][path]' )
304+
305+ # Only add if both fields are filled, the name does not already exist, and it's not marked for deletion
306+ if name and path and name not in files_to_delete and not any (file ['name' ] == name for file in updated_config_files ):
307+ updated_config_files .append ({"name" : name , "path" : path })
308+ logger .debug (f"Added new config file: { name } , { path } " )
309+
310+ # Additional settings
279311 video_dir = request .form .get ("VIDEO_DIR" )
280312 server_port = request .form .get ("SERVER_PORT" )
281-
282- config_files = [
283- {"name" : name , "path" : path }
284- for name , path in zip (config_files_names , config_files_paths )
285- ]
286-
313+
314+ # Update the settings data
287315 settings_data = {
288316 "VIDEO_DIR" : video_dir ,
289317 "SERVER_PORT" : server_port ,
290- "config_files" : config_files ,
318+ "config_files" : updated_config_files ,
291319 }
292-
320+
321+ # Save the updated settings to the settings file
293322 with open (SETTINGS_FILE , "w" ) as f :
294323 json .dump (settings_data , f , indent = 4 )
295324
325+ logger .debug ("Settings saved successfully." )
296326 flash ("Settings updated successfully." )
297327 except Exception as e :
298328 flash (f"Error saving settings: { e } " , "error" )
0 commit comments