@@ -3,9 +3,8 @@ use std::time::Duration;
33use actix_files:: NamedFile ;
44use actix_web:: { web, HttpRequest , HttpResponse , Responder } ;
55use atomic_lib:: { urls, Storelike } ;
6- use opendal:: { services:: S3 , Operator } ;
76
8- use crate :: { appstate:: AppState , errors:: AtomicServerResult , helpers:: get_client_agent} ;
7+ use crate :: { appstate:: AppState , errors:: AtomicServerResult , files :: { self , FileStore } , helpers:: get_client_agent} ;
98
109/// Downloads the File of the Resource that matches the same URL minus the `/download` path.
1110#[ tracing:: instrument( skip( appstate, req) ) ]
@@ -29,13 +28,15 @@ pub async fn handle_download(
2928
3029 let for_agent = get_client_agent ( headers, & appstate, subject. clone ( ) ) ?;
3130 tracing:: info!( "handle_download: {}" , subject) ;
32- let resource = store. get_resource_extended ( & urlencoding:: encode ( & subject) . into_owned ( ) , false , & for_agent) ?;
31+ let file_store = FileStore :: get_subject_file_store ( & subject) ;
32+ let encoded = subject. replace ( & format ! ( "{file_store}:" ) , & format ! ( "{file_store}%3A" ) ) ;
33+ let resource = store. get_resource_extended ( & encoded, false , & for_agent) ?;
3334 let file_id = resource
3435 . get ( urls:: INTERNAL_ID )
3536 . map_err ( |e| format ! ( "Internal ID of file could not be resolved. {}" , e) ) ?
3637 . to_string ( ) ;
37- let is_s3 = file_id . starts_with ( "s3:" ) ;
38- if is_s3 {
38+
39+ if let FileStore :: S3 = file_store {
3940 signed_url_redirect_handler ( file_id. as_str ( ) , & req, & appstate) . await
4041 } else {
4142 download_file_handler_partial ( file_id. as_str ( ) , & req, & appstate)
@@ -47,7 +48,7 @@ pub fn download_file_handler_partial(
4748 req : & HttpRequest ,
4849 appstate : & AppState ,
4950) -> AtomicServerResult < HttpResponse > {
50- println ! ( "{file_id:?}" ) ;
51+ tracing :: info !( "downloading from fs: {file_id:?}" ) ;
5152 let fs_file_id = file_id. strip_prefix ( "fs:" ) . unwrap_or ( file_id) ;
5253 let mut file_path = appstate. config . uploads_path . clone ( ) ;
5354 file_path. push ( fs_file_id. to_string ( ) ) ;
@@ -60,45 +61,8 @@ async fn signed_url_redirect_handler(
6061 req : & HttpRequest ,
6162 appstate : & AppState ,
6263) -> AtomicServerResult < HttpResponse > {
63- let mut builder = S3 :: default ( ) ;
64-
65- let bucket = appstate
66- . config
67- . opts
68- . s3_bucket
69- . as_ref ( )
70- . ok_or ( "s3 file found but no s3 bucket" ) ?;
71- builder. bucket ( bucket) ;
72-
73- appstate
74- . config
75- . opts
76- . s3_region
77- . as_ref ( )
78- . map ( |r| builder. region ( & r) ) ;
79-
80- appstate
81- . config
82- . opts
83- . s3_endpoint
84- . as_ref ( )
85- . map ( |e| builder. endpoint ( & e) ) ;
86-
87- let default_path = & "uploads" . to_string ( ) ;
88- let path = appstate
89- . config
90- . opts
91- . s3_path
92- . as_ref ( )
93- . unwrap_or ( default_path) ;
94-
95- let op: Operator = Operator :: new ( builder) ?. finish ( ) ;
96-
97- let signed_url = op
98- . presign_read ( & format ! ( "{}/{}" , & path, file_id) , Duration :: from_secs ( 3600 ) )
99- . await ?
100- . uri ( )
101- . to_string ( ) ;
64+ let signed_url = files:: get_s3_signed_url ( appstate, Duration :: from_secs ( 3600 ) , file_id)
65+ . await ?;
10266 Ok ( web:: Redirect :: to ( signed_url)
10367 . respond_to ( req)
10468 . map_into_boxed_body ( ) )
0 commit comments