@@ -52,12 +52,15 @@ public static async Task DownloadFiles(Uri[] uris, string localFile, Cancellatio
5252 throw new Exception ( $ "Failed to download { localFile } from all uris: { string . Join ( ", " , uris . Select ( x => x . ToString ( ) ) ) } ") ;
5353 }
5454
55- public static async Task DownloadAndExtractAsync ( string name , Uri uri , string rootDir , CancellationToken cancellationToken )
55+ public static async Task DownloadAndExtractAsync ( string name , Uri uri , string rootDir , CancellationToken cancellationToken , params string [ ] expectedFileNames )
5656 {
5757 Directory . CreateDirectory ( rootDir ) ;
58- string paramsFile = Path . Combine ( rootDir , "inference.pdiparams" ) ;
58+ if ( expectedFileNames == null || expectedFileNames . Length == 0 )
59+ {
60+ expectedFileNames = new [ ] { "inference.pdiparams" , "inference.pdmodel" } ;
61+ }
5962
60- if ( ! File . Exists ( paramsFile ) )
63+ if ( ! CheckLocalModel ( rootDir , expectedFileNames , throwOnError : false ) )
6164 {
6265 string localTarFile = Path . Combine ( rootDir , uri . Segments . Last ( ) ) ;
6366 if ( ! File . Exists ( localTarFile ) || new FileInfo ( localTarFile ) . Length == 0 )
@@ -94,7 +97,7 @@ public static async Task DownloadAndExtractAsync(string name, Uri uri, string ro
9497 archive . WriteToDirectory ( rootDir ) ;
9598 }
9699
97- CheckLocalOCRModel ( rootDir ) ;
100+ CheckLocalModel ( rootDir , expectedFileNames , throwOnError : true ) ;
98101 }
99102
100103 File . Delete ( localTarFile ) ;
@@ -193,26 +196,29 @@ private static string GetDestinationPath(string rootDir, string relativePath)
193196
194197 public static void CheckLocalOCRModel ( string rootDir )
195198 {
196- string [ ] filesToCheck = new [ ]
197- {
198- Path . Combine ( rootDir , "inference.pdiparams" ) ,
199- Path . Combine ( rootDir , "inference.pdmodel" ) ,
200- } ;
199+ CheckLocalModel ( rootDir , new [ ] { "inference.pdiparams" , "inference.pdmodel" } , throwOnError : true ) ;
200+ }
201201
202- foreach ( string path in filesToCheck )
202+ private static bool CheckLocalModel ( string rootDir , string [ ] expectedFileNames , bool throwOnError )
203+ {
204+ foreach ( string fileName in expectedFileNames )
203205 {
204- string fileName = Path . GetFileName ( path ) ;
206+ string path = Path . Combine ( rootDir , fileName ) ;
205207
206208 if ( ! File . Exists ( path ) )
207209 {
210+ if ( ! throwOnError ) return false ;
208211 throw new Exception ( $ "{ fileName } not found in { rootDir } , model error?") ;
209212 }
210213
211214 if ( new FileInfo ( path ) . Length == 0 )
212215 {
216+ if ( ! throwOnError ) return false ;
213217 throw new Exception ( $ "{ fileName } invalid(length = 0), model error?") ;
214218 }
215219 }
220+
221+ return true ;
216222 }
217223
218224 public readonly static Type RootType = typeof ( Settings ) ;
0 commit comments