@@ -76,6 +76,10 @@ def get_choice(completion):
7676
7777
7878def get_image_local_path (url , image_cache_dir = None , project_dir = None ):
79+ return get_local_path (url , image_cache_dir , project_dir )
80+
81+
82+ def get_local_path (url , cache_dir = None , project_dir = None , hostname = None ):
7983 is_local_file = url .startswith ('/data/' ) and '?d=' in url
8084 is_uploaded_file = url .startswith ('/data/upload' )
8185
@@ -86,27 +90,32 @@ def get_image_local_path(url, image_cache_dir=None, project_dir=None):
8690 filepath = os .path .join (dir_path , filename )
8791 if not os .path .exists (filepath ):
8892 raise FileNotFoundError (filepath )
93+ return filepath
8994
9095 # File uploaded via import UI
91- elif is_uploaded_file :
92- if not project_dir or not os .path .exists (project_dir ):
96+ elif is_uploaded_file and project_dir is not None :
97+ if not os .path .exists (project_dir ):
9398 raise FileNotFoundError (
9499 "Can't find uploaded file by URL {url}: you need to pass a valid project_dir" .format (url = url ))
95100 filepath = os .path .join (project_dir , 'upload' , os .path .basename (url ))
101+ return filepath
102+
103+ elif is_uploaded_file and hostname :
104+ url = hostname + url
105+ logger .info ('Resolving url using hostname [' + hostname + '] from LSB: ' + url )
96106
97107 # File specified by remote URL - download and cache it
98- else :
99- image_cache_dir = image_cache_dir or get_cache_dir ()
100- parsed_url = urlparse (url )
101- url_filename = os .path .basename (parsed_url .path )
102- url_hash = hashlib .md5 (url .encode ()).hexdigest ()[:6 ]
103- filepath = os .path .join (image_cache_dir , url_hash + '__' + url_filename )
104- if not os .path .exists (filepath ):
105- logger .info ('Download {url} to {filepath}' .format (url = url , filepath = filepath ))
106- r = requests .get (url , stream = True )
107- r .raise_for_status ()
108- with io .open (filepath , mode = 'wb' ) as fout :
109- fout .write (r .content )
108+ cache_dir = cache_dir or get_cache_dir ()
109+ parsed_url = urlparse (url )
110+ url_filename = os .path .basename (parsed_url .path )
111+ url_hash = hashlib .md5 (url .encode ()).hexdigest ()[:6 ]
112+ filepath = os .path .join (cache_dir , url_hash + '__' + url_filename )
113+ if not os .path .exists (filepath ):
114+ logger .info ('Download {url} to {filepath}' .format (url = url , filepath = filepath ))
115+ r = requests .get (url , stream = True )
116+ r .raise_for_status ()
117+ with io .open (filepath , mode = 'wb' ) as fout :
118+ fout .write (r .content )
110119 return filepath
111120
112121
0 commit comments