@@ -52,29 +52,43 @@ def get_file_items(self, *args):
5252
5353 return items
5454
55+ def _get_rclone_file (self , file_path ):
56+ try :
57+ relative_path = os .path .relpath (file_path , self .RCLONE_MOUNT_PATH )
58+ drive_name = relative_path .split (os .sep )[0 ]
59+ # remove drive_name and the last part of the path
60+ file_path = os .path .join ("" , * relative_path .split (os .sep )[1 :- 1 ])
61+ file_name = os .path .basename (relative_path )
62+
63+ cmd = ['rclone' , 'lsjson' , f'{ drive_name } :{ file_path } ' ]
64+ # special case for shared_with_me
65+ if drive_name .lower () == "shared_with_me" :
66+ cmd .append ('--drive-shared-with-me' )
67+
68+ result = subprocess .run (cmd , capture_output = True , text = True , check = True )
69+ files = json .loads (result .stdout )
70+
71+ if not files :
72+ raise FileNotFoundError ("No files returned from rclone lsjson" )
73+
74+ # find matching file by name
75+ files = [f for f in files if f ['Path' ] == file_name ]
76+ if not files :
77+ raise FileNotFoundError (f"File '{ file_name } ' not found in Google Drive" )
78+
79+ # assuming the first file is the one we want
80+ file = files [0 ]
81+ return file
82+
83+ except subprocess .CalledProcessError as e :
84+ subprocess .Popen (["zenity" , "--error" , "--text" , f"rclone failed:\n { e .stderr } " ])
85+ except Exception as e :
86+ subprocess .Popen (["zenity" , "--error" , "--text" , f"Unexpected error:\n { str (e )} " ])
87+
5588 def open_rclone_url (self , menu , file_paths ):
5689 for file_path in file_paths :
5790 try :
58- relative_path = os .path .relpath (file_path , self .RCLONE_MOUNT_PATH )
59- drive_name = relative_path .split (os .sep )[0 ]
60- # remove drive_name and the last part of the path
61- file_path = os .path .join ("" , * relative_path .split (os .sep )[1 :- 1 ])
62- file_name = os .path .basename (relative_path )
63-
64- cmd = ['rclone' , 'lsjson' , f'{ drive_name } :{ file_path } ' ]
65- result = subprocess .run (cmd , capture_output = True , text = True , check = True )
66- files = json .loads (result .stdout )
67-
68- if not files :
69- raise FileNotFoundError ("No files returned from rclone lsjson" )
70-
71- # find matching file by name
72- files = [f for f in files if f ['Path' ] == file_name ]
73- if not files :
74- raise FileNotFoundError (f"File '{ file_name } ' not found in Google Drive" )
75-
76- # assuming the first file is the one we want
77- file = files [0 ]
91+ file = self ._get_rclone_file (file_path )
7892 file_id = file ['ID' ]
7993
8094 # send warning if file is open document format
@@ -84,8 +98,6 @@ def open_rclone_url(self, menu, file_paths):
8498 url = f'https://drive.google.com/open?id={ file_id } '
8599 webbrowser .get ("xdg-open" ).open (url )
86100
87- except subprocess .CalledProcessError as e :
88- subprocess .Popen (["zenity" , "--error" , "--text" , f"rclone failed:\n { e .stderr } " ])
89101 except Exception as e :
90102 subprocess .Popen (["zenity" , "--error" , "--text" , f"Unexpected error:\n { str (e )} " ])
91103
@@ -99,32 +111,9 @@ def _copy_to_clipboard(self, text):
99111 def copy_file_link (self , menu , file_paths ):
100112 file_path = file_paths [0 ]
101113 try :
102- relative_path = os .path .relpath (file_path , self .RCLONE_MOUNT_PATH )
103- drive_name = relative_path .split (os .sep )[0 ]
104- # remove drive_name and the last part of the path
105- file_path = os .path .join ("" , * relative_path .split (os .sep )[1 :- 1 ])
106- file_name = os .path .basename (relative_path )
107-
108- cmd = ['rclone' , 'lsjson' , f'{ drive_name } :{ file_path } ' ]
109- result = subprocess .run (cmd , capture_output = True , text = True , check = True )
110- files = json .loads (result .stdout )
111-
112- if not files :
113- raise FileNotFoundError ("No files returned from rclone lsjson" )
114-
115- # find matching file by name
116- files = [f for f in files if f ['Path' ] == file_name ]
117- if not files :
118- raise FileNotFoundError (f"File '{ file_name } ' not found in Google Drive" )
119-
120- # assuming the first file is the one we want
121- file = files [0 ]
114+ file = self ._get_rclone_file (file_path )
122115 file_id = file ['ID' ]
123-
124116 url = f'https://drive.google.com/open?id={ file_id } '
125117 self ._copy_to_clipboard (url )
126-
127- except subprocess .CalledProcessError as e :
128- subprocess .Popen (["zenity" , "--error" , "--text" , f"rclone failed:\n { e .stderr } " ])
129118 except Exception as e :
130- subprocess .Popen (["zenity" , "--error" , "--text" , f"Unexpected error:\n { str (e )} " ])
119+ subprocess .Popen (["zenity" , "--error" , "--text" , f"Unexpected error:\n { str (e )} " ])
0 commit comments