77from WebServerLib .BaseRequestHandler import BaseRequestHandler , BaseRequestLoggingFunction
88
99import os
10+ import platform
1011import subprocess
1112import time
1213import logging
1516import qt
1617import slicer
1718import ctk
18- # IDCRequestHandler.py
19-
20- from WebServerLib .BaseRequestHandler import BaseRequestHandler , BaseRequestLoggingFunction
21- import urllib
22- import os
23- from typing import Optional
2419from idc_index import index
2520
26-
2721class IDCRequestHandler (BaseRequestHandler ):
2822
2923 def __init__ (self , logMessage : Optional [BaseRequestLoggingFunction ] = None ):
@@ -33,7 +27,7 @@ def __init__(self, logMessage: Optional[BaseRequestLoggingFunction] = None):
3327
3428 def canHandleRequest (self , uri : bytes , ** _kwargs ) -> float :
3529 parsedURL = urllib .parse .urlparse (uri )
36- if parsedURL .path .startswith (b"/idc" ):
30+ if ( parsedURL .path .startswith (b"/idc" ) ):
3731 return 0.5
3832 return 0.0
3933
@@ -126,4 +120,81 @@ def onStartupCompleted(self):
126120
127121 logic .start ()
128122 print ("IDC Request Handler has been registered and server started." )
123+
124+ self .writeResolverScript ()
125+ self .registerCustomProtocol ()
126+
127+ def writeResolverScript (self ):
128+ current_dir = os .path .dirname (os .path .realpath (__file__ ))
129+ resolver_script_path = os .path .join (current_dir ,'Resources' , 'resolver.py' )
130+
131+ resolver_script_content = '''import sys
132+ import urllib.parse
133+ import requests
134+ import webbrowser
135+
136+ def resolve_url(url):
137+ # Parse the URL
138+ parsed_url = urllib.parse.urlparse(url)
139+
140+ # Remove the scheme (idcbrowser://) from the URL and split the path
141+ path_parts = parsed_url.netloc.split('/') + parsed_url.path.split('/')[1:]
142+
143+ # Check the first part of the path to determine the endpoint
144+ if path_parts[0] == 'collections':
145+ new_url = "http://localhost:2042/idc/collections"
146+ # Open the new URL in a web browser
147+ webbrowser.open(new_url)
148+ elif path_parts[0] == 'series':
149+ new_url = f"http://localhost:2042/idc/download/seriesInstanceUID/{path_parts[1]}"
150+ elif path_parts[0] == 'studies':
151+ new_url = f"http://localhost:2042/idc/download/studyInstanceUID/{path_parts[1]}"
152+ else:
153+ print(f"Unhandled path: {path_parts[0]}")
154+ return
155+
156+ # Make the request to the new URL
157+ response = requests.get(new_url)
158+
159+ # Print the response
160+ print(response.text)
161+
162+ if __name__ == "__main__":
163+ # The URL is passed as the first argument
164+ url = sys.argv[1]
165+
166+ # Resolve the URL
167+ resolve_url(url)
168+ '''
169+
170+ with open (resolver_script_path , 'w' ) as f :
171+ f .write (resolver_script_content )
172+ print (f"Resolver script written to { resolver_script_path } " )
173+
174+ def registerCustomProtocol (self ):
175+ if platform .system () == "Linux" :
176+ # Check if the protocol is already registered
177+ if os .path .exists (os .path .expanduser ("~/.local/share/applications/idcbrowser.desktop" )):
178+ print ("IDC Browser URL protocol is already registered." )
179+ return
180+
181+ # Get the current directory
182+ current_dir = os .path .dirname (os .path .realpath (__file__ ))
183+ python_script_path = os .path .join (current_dir , 'resolver.py' )
184+
185+ # Register IDC Browser URL protocol
186+ with open (os .path .expanduser ("~/.local/share/applications/idcbrowser.desktop" ), "w" ) as f :
187+ f .write (f"""[Desktop Entry]
188+ Name=IDC Browser
189+ Exec=python3 { python_script_path } %u
190+ Type=Application
191+ Terminal=false
192+ MimeType=x-scheme-handler/idcbrowser;
193+ """ )
194+
195+ # Update MIME database
196+ os .system ("update-desktop-database ~/.local/share/applications/" )
197+ os .system ("xdg-mime default idcbrowser.desktop x-scheme-handler/idcbrowser" )
198+ else :
199+ print ("IDC Browser URL protocol registration is not supported on this operating system." )
129200
0 commit comments