@@ -3,7 +3,7 @@ use actix_web_httpauth::headers::authorization::{Authorization, Basic};
3
3
4
4
use crate :: AppState ;
5
5
use crate :: authentication:: { ArtifactPermission , AuthenticationResult } ;
6
- use crate :: coordinates:: Parser ;
6
+ use crate :: coordinates:: { Artifact as ArtifactDTO , Parser } ;
7
7
use crate :: model:: maven:: File ;
8
8
9
9
fn is_metadata ( uri : & str ) -> Result < bool , regex:: Error > {
@@ -118,23 +118,43 @@ async fn deploy_metadata(uri: String, app_state: web::Data<AppState>, authentica
118
118
#[ get( "{path:.*}" ) ]
119
119
async fn read ( path : web:: Path < String > , app_state : web:: Data < AppState > , authentication : Option < web:: Header < Authorization < Basic > > > ) -> impl Responder {
120
120
let uri = path. into_inner ( ) ;
121
+ match is_metadata ( uri. as_ref ( ) ) {
122
+ Ok ( true ) => return read_metadata ( uri, app_state, authentication) . await ,
123
+ Err ( _) => return HttpResponse :: InternalServerError ( ) . body ( "Failed to check if the file is metadata." ) ,
124
+ _ => { }
125
+ } ;
126
+
121
127
let coordinates = match Parser :: parse_to_file ( uri. as_str ( ) ) {
122
128
Ok ( coordinates) => coordinates,
123
129
Err ( _) => return HttpResponse :: BadRequest ( ) . body ( "Coordinates could not be parsed." ) ,
124
130
} ;
125
131
132
+ return read_file ( uri, app_state, authentication, coordinates. to_version ( ) . to_artifact ( ) ) . await ;
133
+ }
134
+
135
+ // For now, we assume that we're just serving metadata for artifacts
136
+ async fn read_metadata ( uri : String , app_state : web:: Data < AppState > , authentication : Option < web:: Header < Authorization < Basic > > > ) -> HttpResponse {
137
+ let coordinates = match Parser :: parse_to_version ( uri. as_str ( ) ) {
138
+ Ok ( coordinates) => coordinates,
139
+ Err ( _) => return HttpResponse :: BadRequest ( ) . body ( "Coordinates could not be parsed." ) ,
140
+ } ;
141
+
142
+ return read_file ( uri, app_state, authentication, coordinates. to_artifact ( ) ) . await ;
143
+ }
144
+
145
+ async fn read_file ( uri : String , app_state : web:: Data < AppState > , authentication : Option < web:: Header < Authorization < Basic > > > , artifact : ArtifactDTO ) -> HttpResponse {
146
+ match app_state. artifact_authenticator . authenticate_read ( artifact, authentication, & app_state. pool ) . await {
147
+ ArtifactPermission :: Granted => { } ,
148
+ permission => return permission. as_response ( ) ,
149
+ } ;
150
+
126
151
let file_result = sqlx:: query_as!( File , "SELECT file.id, file.version_id, file.name, file.uri, file.path FROM maven_file file WHERE file.uri = ?" , uri) . fetch_optional ( & app_state. pool ) . await ;
127
152
let file = match file_result {
128
153
Ok ( Some ( file) ) => file,
129
154
Ok ( None ) => return HttpResponse :: NotFound ( ) . body ( "The requested file does not exist in this registry." ) ,
130
155
Err ( _) => return HttpResponse :: InternalServerError ( ) . body ( "Querying for the requested file failed." ) ,
131
156
} ;
132
157
133
- match app_state. artifact_authenticator . authenticate_read ( coordinates. to_version ( ) . to_artifact ( ) , authentication, & app_state. pool ) . await {
134
- ArtifactPermission :: Granted => { } ,
135
- permission => return permission. as_response ( ) ,
136
- } ;
137
-
138
158
return match app_state. files . read ( & file. path ) {
139
159
Ok ( contents) => HttpResponse :: Ok ( ) . body ( contents) ,
140
160
Err ( _) => HttpResponse :: InternalServerError ( ) . body ( "File contents could not be obtained." ) ,
0 commit comments