feat: Add cross-platform file path selection for password files
This commit is contained in:
parent
b8698d41b8
commit
ac5306ed30
@ -33,6 +33,43 @@ def save_uploaded_file(uploaded_file):
|
|||||||
tmp_file.write(uploaded_file.getvalue())
|
tmp_file.write(uploaded_file.getvalue())
|
||||||
return tmp_file.name
|
return tmp_file.name
|
||||||
|
|
||||||
|
def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
|
||||||
|
"""Get file path using native file dialog when possible"""
|
||||||
|
try:
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import filedialog
|
||||||
|
root = tk.Tk()
|
||||||
|
root.withdraw() # Hide the main window
|
||||||
|
root.attributes('-topmost', True) # Bring dialog to front
|
||||||
|
path = filedialog.askopenfilename(title=title, filetypes=file_types)
|
||||||
|
return path if path else None
|
||||||
|
elif platform.system() == "Linux":
|
||||||
|
# Use zenity for Linux systems
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
['zenity', '--file-selection', '--title', title],
|
||||||
|
capture_output=True,
|
||||||
|
text=True
|
||||||
|
)
|
||||||
|
return result.stdout.strip() if result.returncode == 0 else None
|
||||||
|
except FileNotFoundError:
|
||||||
|
return None
|
||||||
|
elif platform.system() == "Darwin": # macOS
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
['osascript', '-e', f'choose file with prompt "{title}"'],
|
||||||
|
capture_output=True,
|
||||||
|
text=True
|
||||||
|
)
|
||||||
|
return result.stdout.strip() if result.returncode == 0 else None
|
||||||
|
except FileNotFoundError:
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
st.error(f"Error opening file dialog: {str(e)}")
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
def get_directory_path(title="Select Directory"):
|
def get_directory_path(title="Select Directory"):
|
||||||
"""Get directory path using native file dialog when possible"""
|
"""Get directory path using native file dialog when possible"""
|
||||||
try:
|
try:
|
||||||
@ -157,20 +194,10 @@ with col2:
|
|||||||
value=st.session_state.get('password_path', ''))
|
value=st.session_state.get('password_path', ''))
|
||||||
password_browse = st.button("Browse Password File")
|
password_browse = st.button("Browse Password File")
|
||||||
if password_browse:
|
if password_browse:
|
||||||
try:
|
path = get_file_path("Select Password File", [("Text Files", "*.txt")])
|
||||||
import tkinter as tk
|
if path:
|
||||||
from tkinter import filedialog
|
st.session_state['password_path'] = path
|
||||||
root = tk.Tk()
|
|
||||||
root.withdraw()
|
|
||||||
file_path = filedialog.askopenfilename(
|
|
||||||
title="Select Password File",
|
|
||||||
filetypes=[("Text Files", "*.txt")]
|
|
||||||
)
|
|
||||||
if file_path:
|
|
||||||
st.session_state['password_path'] = file_path
|
|
||||||
st.experimental_rerun()
|
st.experimental_rerun()
|
||||||
except Exception as e:
|
|
||||||
st.error(f"Error opening file dialog: {str(e)}")
|
|
||||||
|
|
||||||
if password_path and os.path.exists(password_path):
|
if password_path and os.path.exists(password_path):
|
||||||
with open(password_path, 'r', encoding='utf-8') as pf:
|
with open(password_path, 'r', encoding='utf-8') as pf:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user