@@ -81,37 +81,49 @@ async fn fetch(url: &Url, output: &Path) -> Result<String, Error> {
8181}
8282
8383async fn extract ( archive : & Path , destination : & Path ) -> Result < ( ) , Error > {
84- let extension = archive
85- . extension ( )
86- . map ( |e| e. to_string_lossy ( ) . to_string ( ) )
87- . unwrap_or_else ( || "tar" . to_owned ( ) ) ;
88-
89- // If we can't specialise (.zip, etc) assume its a tar
90- let result = match extension. as_str ( ) {
91- "zip" => {
92- Command :: new ( "unzip" )
93- . arg ( archive)
94- . arg ( "-d" )
95- . arg ( destination)
96- . output ( )
97- . await ?
84+ if let Some ( kind) = infer:: get_from_path ( archive) ? {
85+ println ! ( "Detected type: {} ({})" , kind. mime_type( ) , kind. extension( ) ) ;
86+ // If we can't specialise (.zip, etc) assume its a tar
87+ let result = match kind. extension ( ) {
88+ "zip" => {
89+ Command :: new ( "unzip" )
90+ . arg ( archive)
91+ . arg ( "-d" )
92+ . arg ( destination)
93+ . output ( )
94+ . await ?
95+ }
96+ _ => {
97+ Command :: new ( "tar" )
98+ . arg ( "xf" )
99+ . arg ( archive)
100+ . arg ( "-C" )
101+ . arg ( destination)
102+ . output ( )
103+ . await ?
104+ }
105+ } ;
106+ if result. status . success ( ) {
107+ Ok ( ( ) )
108+ } else {
109+ eprintln ! ( "Command exited with: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
110+ Err ( Error :: Extract ( result. status ) )
98111 }
99- _ => {
100- Command :: new ( "tar" )
101- . arg ( "xf" )
102- . arg ( archive)
103- . arg ( "-C" )
104- . arg ( destination)
105- . output ( )
106- . await ?
107- }
108- } ;
109-
110- if result. status . success ( ) {
111- Ok ( ( ) )
112112 } else {
113- eprintln ! ( "Command exited with: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
114- Err ( Error :: Extract ( result. status ) )
113+ println ! ( "Unknown file type, attempting tar extraction" ) ;
114+ let result = Command :: new ( "tar" )
115+ . arg ( "xf" )
116+ . arg ( archive)
117+ . arg ( "-C" )
118+ . arg ( destination)
119+ . output ( )
120+ . await ?;
121+ if result. status . success ( ) {
122+ Ok ( ( ) )
123+ } else {
124+ eprintln ! ( "Command exited with: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
125+ Err ( Error :: Extract ( result. status ) )
126+ }
115127 }
116128}
117129
0 commit comments