11use std:: collections:: HashMap ;
22use std:: fs;
33use std:: path:: Path ;
4- use std:: time :: Duration ;
4+ use std:: str :: FromStr as _ ;
55
66use anyhow:: { Context as _, Result } ;
77use clap:: { Arg , ArgMatches , Command } ;
@@ -16,9 +16,7 @@ use walkdir::WalkDir;
1616
1717use crate :: api:: Api ;
1818use crate :: config:: { Auth , Config } ;
19- use crate :: utils:: api:: get_org_project_id;
2019use crate :: utils:: args:: ArgExt as _;
21- use crate :: utils:: objectstore:: get_objectstore_url;
2220use http:: { self , HeaderValue } ;
2321
2422const EXPERIMENTAL_WARNING : & str =
@@ -294,35 +292,26 @@ fn read_jpeg_dimensions(data: &[u8]) -> Option<(u32, u32)> {
294292}
295293
296294fn upload_images ( images : & [ ImageInfo ] , org : & str , project : & str ) -> Result < ( ) > {
297- let config = Config :: current ( ) ;
298- let auth = config
299- . get_auth ( )
300- . ok_or_else ( || anyhow:: anyhow!( "Authentication required" ) ) ?;
301- let token = match auth {
302- Auth :: Token ( token) => token. raw ( ) . expose_secret ( ) ,
303- } ;
304-
305295 let api = Api :: current ( ) ;
306296 let authenticated_api = api. authenticated ( ) ?;
307- let retention = authenticated_api. fetch_preprod_retention ( org) ?;
308- let expiration =
309- ExpirationPolicy :: TimeToLive ( Duration :: from_secs ( retention. snapshots * 24 * 60 * 60 ) ) ;
310-
311- let url = get_objectstore_url ( & authenticated_api, org) ?;
312- let header_value = HeaderValue :: from_str ( & format ! ( "Bearer {token}" ) )
313- . context ( "Auth token contains invalid characters for HTTP header" ) ?;
314- let client = ClientBuilder :: new ( url)
297+ let options = authenticated_api. fetch_snapshots_upload_options ( org, project) ?;
298+
299+ let expiration = ExpirationPolicy :: from_str ( & options. objectstore . expiration_policy )
300+ . context ( "Failed to parse expiration policy from upload options" ) ?;
301+
302+ let client = ClientBuilder :: new ( options. objectstore . url )
315303 . configure_reqwest ( move |r| {
316304 let mut headers = http:: HeaderMap :: new ( ) ;
317- headers. insert ( AUTHORIZATION , header_value ) ;
305+ headers. insert ( AUTHORIZATION , HeaderValue :: from_static ( "placeholder" ) ) ; // TODO: get token from upload options endpoint
318306 r. default_headers ( headers)
319307 } )
320308 . build ( ) ?;
321309
322- let ( org_id, project_id) = get_org_project_id ( Api :: current ( ) , org, project) ?;
323- let session = Usecase :: new ( "preprod" )
324- . for_project ( org_id, project_id)
325- . session ( & client) ?;
310+ let mut scope = Usecase :: new ( "preprod" ) . scope ( ) ;
311+ for ( key, value) in & options. objectstore . scopes {
312+ scope = scope. push ( key, value) ;
313+ }
314+ let session = scope. session ( & client) ?;
326315
327316 let runtime = tokio:: runtime:: Builder :: new_current_thread ( )
328317 . enable_all ( )
@@ -337,14 +326,12 @@ fn upload_images(images: &[ImageInfo], org: &str, project: &str) -> Result<()> {
337326 let contents = fs:: read ( & image. path )
338327 . with_context ( || format ! ( "Failed to read image: {}" , image. path. display( ) ) ) ?;
339328
340- let obj_key = format ! ( "{org_id}/{project_id}/{}" , image. hash) ;
341-
342- info ! ( "Queueing {} as {obj_key}" , image. path. display( ) ) ;
329+ info ! ( "Queueing {} as {}" , image. path. display( ) , image. hash) ;
343330
344331 many_builder = many_builder. push (
345332 session
346333 . put ( contents)
347- . key ( & obj_key )
334+ . key ( & image . hash )
348335 . expiration_policy ( expiration) ,
349336 ) ;
350337 }
0 commit comments