@@ -4,23 +4,23 @@ use actix_multipart::Field;
44use futures:: StreamExt ;
55use opendal:: { services:: S3 , Operator } ;
66
7- use crate :: { appstate :: AppState , config:: Config , errors:: AtomicServerResult } ;
7+ use crate :: { config:: Config , errors:: AtomicServerResult } ;
88
9- #[ derive( Clone , Debug ) ]
9+ #[ derive( Clone , Debug , PartialEq ) ]
1010pub enum FileStore {
1111 S3 ( S3Config ) ,
1212 FS ( FSConfig ) ,
1313}
1414
15- #[ derive( Clone , Debug ) ]
15+ #[ derive( Clone , Debug , PartialEq ) ]
1616pub struct S3Config {
1717 bucket : String ,
1818 path : String ,
1919 endpoint : Option < String > ,
2020 region : Option < String > ,
2121}
2222
23- #[ derive( Clone , Debug ) ]
23+ #[ derive( Clone , Debug , PartialEq ) ]
2424pub struct FSConfig {
2525 pub path : PathBuf ,
2626}
@@ -50,11 +50,11 @@ impl FileStore {
5050 }
5151 }
5252
53- pub fn get_subject_file_store < ' a > ( appstate : & ' a AppState , subject : & str ) -> & ' a FileStore {
53+ pub fn get_subject_file_store < ' a > ( file_store : & ' a FileStore , fs_file_store : & ' a FileStore , subject : & str ) -> & ' a FileStore {
5454 if subject. contains ( Self :: S3_PREFIX ) {
55- & appstate . file_store
55+ file_store
5656 } else {
57- & appstate . fs_file_store
57+ fs_file_store
5858 }
5959 }
6060
@@ -172,3 +172,66 @@ pub async fn get_s3_signed_url(
172172
173173 Ok ( uri)
174174}
175+
176+ #[ cfg( test) ]
177+ mod test {
178+ use super :: * ;
179+
180+ use crate :: config:: { self , Opts } ;
181+
182+ #[ actix_rt:: test]
183+ async fn file_store_tests ( ) {
184+ let unique_string = atomic_lib:: utils:: random_string ( 10 ) ;
185+ use clap:: Parser ;
186+ let mut opts = Opts :: parse_from ( [
187+ "atomic-server" ,
188+ "--initialize" ,
189+ "--data-dir" ,
190+ & format ! ( "./.temp/{}/db" , unique_string) ,
191+ "--config-dir" ,
192+ & format ! ( "./.temp/{}/config" , unique_string) ,
193+ ] ) ;
194+
195+ let mut config = config:: build_config ( opts. clone ( ) )
196+ . map_err ( |e| format ! ( "Initialization failed: {}" , e) )
197+ . expect ( "failed init config" ) ;
198+
199+ let appstate = crate :: appstate:: init ( config. clone ( ) ) . expect ( "failed init appstate" ) ;
200+
201+ let fs_store = FileStore :: init_fs_from_config ( & config) ;
202+ if let FileStore :: FS ( fs_config) = fs_store. clone ( ) {
203+ assert ! ( fs_config. path. to_str( ) . unwrap( ) . contains( "uploads" ) ) ;
204+ } else {
205+ panic ! ( "fs FileStore not initiated" ) ;
206+ }
207+ let store = FileStore :: init_from_config ( & config, fs_store. clone ( ) ) ;
208+ assert_eq ! ( fs_store, store) ;
209+ assert_eq ! ( "fs:" , fs_store. prefix( ) ) ;
210+ assert_eq ! ( "fs%3A" , fs_store. encoded( ) ) ;
211+
212+ assert ! ( fs_store. get_fs_file_path( "my-great-file" ) . unwrap( ) . to_str( ) . unwrap( ) . contains( "uploads/my-great-file" ) ) ;
213+ assert ! ( fs_store. get_fs_file_path( "fs:my-great-file" ) . unwrap( ) . to_str( ) . unwrap( ) . contains( "uploads/my-great-file" ) ) ;
214+ //assert_eq!(file_path, fs_store.get_fs_file_path("my-great-file").unwrap());
215+ //assert_eq!(file_path, fs_store.get_fs_file_path("fs:my-great-file").unwrap());
216+
217+ // FileStore::S3 init
218+ opts. s3_bucket = Some ( "test-bucket" . to_string ( ) ) ;
219+ opts. s3_path = Some ( "uploads" . to_string ( ) ) ;
220+ config. opts = opts;
221+
222+ let s3_store = FileStore :: init_from_config ( & config, fs_store. clone ( ) ) ;
223+ if let FileStore :: S3 ( s3_config) = s3_store. clone ( ) {
224+ assert_eq ! ( s3_config. bucket, "test-bucket" ) ;
225+ assert_eq ! ( s3_config. path, "uploads" ) ;
226+ } else {
227+ panic ! ( "s3 FileStore not initiated" ) ;
228+ }
229+
230+ assert_eq ! ( "s3:" , s3_store. prefix( ) ) ;
231+ assert_eq ! ( "s3%3A" , s3_store. encoded( ) ) ;
232+
233+ assert_eq ! ( & fs_store, FileStore :: get_subject_file_store( & s3_store, & fs_store, "my-great-file" ) ) ;
234+ assert_eq ! ( & fs_store, FileStore :: get_subject_file_store( & s3_store, & fs_store, "fs:my-great-file" ) ) ;
235+ assert_eq ! ( & s3_store, FileStore :: get_subject_file_store( & s3_store, & fs_store, "s3:my-great-file" ) ) ;
236+ }
237+ }
0 commit comments