@@ -117,11 +117,11 @@ def disconnect(self):
117117
118118 logging .info ("Client disconnected." )
119119
120- def publish (self , local_path , alias ):
120+ def publish (self , local_path , alias , allow_overwrite = False ):
121121 if not self .connected or not self .client :
122122 raise RuntimeError ("Client is not connected." )
123123 with self ._socket_lock :
124- self .client ._do_publish (local_path , alias )
124+ return self .client ._do_publish (local_path , alias , allow_overwrite = allow_overwrite )
125125
126126 def fetch_peer_list (self , fname ):
127127 if not self .connected or not self .client :
@@ -412,18 +412,70 @@ def publish_file(self):
412412
413413 threading .Thread (
414414 target = self ._publish_task ,
415- args = (local_path , alias ),
415+ args = (local_path , alias , False ),
416416 daemon = True ,
417417 ).start ()
418418
419- def _publish_task (self , local_path , alias ):
419+ def _publish_task (self , local_path , alias , allow_overwrite ):
420420 try :
421- self .controller .publish (local_path , alias )
421+ response = self .controller .publish (local_path , alias , allow_overwrite = allow_overwrite )
422422 except Exception as exc :
423423 logging .error ("Publish failed: %s" , exc )
424424 self .root .after (0 , lambda : messagebox .showerror ("Publish error" , str (exc )))
425425 return
426- logging .info ("Publish request sent for %s -> alias '%s'" , local_path , alias )
426+ logging .info (
427+ "Publish response received for %s -> alias '%s': %s" ,
428+ local_path ,
429+ alias ,
430+ response ,
431+ )
432+ self .root .after (
433+ 0 ,
434+ lambda : self ._handle_publish_response (local_path , alias , response , allow_overwrite ),
435+ )
436+
437+ def _handle_publish_response (self , local_path , alias , response , allow_overwrite ):
438+ status = (response or {}).get ("status" )
439+ message = response .get ("message" ) if isinstance (response , dict ) else None
440+ if not isinstance (response , dict ):
441+ messagebox .showerror ("Publish error" , "Unexpected response from server." )
442+ return
443+
444+ if status == "conflict" and not allow_overwrite :
445+ existing_path = response .get ("existing_lname" ) or "unknown location"
446+ prompt = (
447+ "Alias '{alias}' is already published for this client.\n \n "
448+ "Existing path: {existing}\n "
449+ "New path: {new}\n \n "
450+ "Do you want to overwrite the previous file entry?"
451+ ).format (alias = alias , existing = existing_path , new = local_path )
452+ overwrite = messagebox .askyesno ("Overwrite alias?" , prompt )
453+ if overwrite :
454+ logging .info ("User confirmed overwrite for alias '%s'." , alias )
455+ threading .Thread (
456+ target = self ._publish_task ,
457+ args = (local_path , alias , True ),
458+ daemon = True ,
459+ ).start ()
460+ else :
461+ logging .info ("User declined to overwrite alias '%s'." , alias )
462+ messagebox .showinfo ("Publish" , f"Publish cancelled for alias '{ alias } '." )
463+ return
464+
465+ if status in ("created" , "updated" ):
466+ title = "Publish" if status == "created" else "Publish Updated"
467+ messagebox .showinfo (title , message or f"Alias '{ alias } ' published successfully." )
468+ return
469+
470+ if status == "unchanged" :
471+ messagebox .showinfo ("Publish" , message or f"Alias '{ alias } ' is already up to date." )
472+ return
473+
474+ if status == "error" :
475+ messagebox .showerror ("Publish error" , message or f"Failed to publish '{ alias } '." )
476+ return
477+
478+ messagebox .showinfo ("Publish" , message or f"Alias '{ alias } ' publish result: { status } " )
427479
428480 def fetch_file (self ):
429481 fname = self .fetch_name_var .get ().strip ()
@@ -537,11 +589,11 @@ def _show_peer_selection(self, fname, peer_list):
537589 listbox .pack (padx = 10 , pady = 5 , fill = tk .BOTH , expand = True )
538590
539591 for idx , peer in enumerate (peer_list , start = 1 ):
540- hostname = peer .get ("hostname" ) or " Unknown"
541- original_name = os . path . basename (peer .get ("lname" ) or fname )
592+ client_label = peer .get ("hostname" ) or peer . get ( "ip" ) or " Unknown client "
593+ size_label = self . _format_file_size (peer .get ("file_size" ) )
542594 listbox .insert (
543595 tk .END ,
544- f"{ idx } . { hostname } ({ original_name } )" ,
596+ f"{ idx } . { client_label } ({ size_label } )" ,
545597 )
546598
547599 button_frame = tk .Frame (dialog )
@@ -600,7 +652,7 @@ def on_cancel():
600652 all_button = tk .Button (button_frame , text = "Download All" , command = on_select_all , bg = PASTEL_BUTTON )
601653 all_button .grid (row = 0 , column = 1 , padx = 5 , sticky = "ew" )
602654
603- custom_button = tk .Button (button_frame , text = "Custom... " , command = on_custom , bg = PASTEL_BUTTON )
655+ custom_button = tk .Button (button_frame , text = "Custom" , command = on_custom , bg = PASTEL_BUTTON )
604656 custom_button .grid (row = 0 , column = 2 , padx = 5 , sticky = "ew" )
605657
606658 cancel_button = tk .Button (button_frame , text = "Cancel" , command = on_cancel , bg = PASTEL_BUTTON )
@@ -668,6 +720,20 @@ def _on_multi_download_finished(self, successes, failures):
668720 messagebox .showinfo ("Fetch summary" , summary )
669721 self .fetch_button .config (state = tk .NORMAL )
670722
723+ def _format_file_size (self , size_value ):
724+ try :
725+ size = int (size_value )
726+ except (TypeError , ValueError ):
727+ return "unknown size"
728+ units = ["B" , "KB" , "MB" , "GB" , "TB" ]
729+ for unit in units :
730+ if size < 1024 or unit == "TB" :
731+ if unit == "B" :
732+ return f"{ size } { unit } "
733+ return f"{ size :.1f} { unit } "
734+ size /= 1024
735+ return f"{ size :.1f} TB"
736+
671737 def _get_preferred_filename (self , peer_info , fallback_name ):
672738 original = peer_info .get ("lname" )
673739 if original :
0 commit comments