@@ -24,7 +24,8 @@ use mime::Mime;
2424use path_slash:: PathExt ;
2525use serde_with:: { DeserializeFromStr , SerializeDisplay } ;
2626use std:: {
27- fmt, fs,
27+ fmt,
28+ fs:: { self , File } ,
2829 io:: { self , BufReader } ,
2930 num:: ParseIntError ,
3031 ops:: RangeInclusive ,
@@ -569,6 +570,33 @@ impl AsyncStorage {
569570 Ok ( alg)
570571 }
571572
573+ #[ instrument( skip( self ) ) ]
574+ pub ( crate ) async fn store_path (
575+ & self ,
576+ target_path : impl Into < String > + std:: fmt:: Debug ,
577+ source_path : impl AsRef < Path > + std:: fmt:: Debug ,
578+ ) -> Result < CompressionAlgorithm > {
579+ let target_path = target_path. into ( ) ;
580+ let source_path = source_path. as_ref ( ) ;
581+
582+ let alg = CompressionAlgorithm :: default ( ) ;
583+ let content = compress ( BufReader :: new ( File :: open ( source_path) ?) , alg) ?;
584+
585+ let mime = detect_mime ( & target_path) . to_owned ( ) ;
586+
587+ self . store_inner ( vec ! [ Blob {
588+ path: target_path,
589+ mime,
590+ content,
591+ compression: Some ( alg) ,
592+ // this field is ignored by the backend
593+ date_updated: Utc :: now( ) ,
594+ } ] )
595+ . await ?;
596+
597+ Ok ( alg)
598+ }
599+
572600 async fn store_inner ( & self , batch : Vec < Blob > ) -> Result < ( ) > {
573601 match & self . backend {
574602 StorageBackend :: Database ( db) => db. store_batch ( batch) . await ,
@@ -777,6 +805,18 @@ impl Storage {
777805 self . runtime . block_on ( self . inner . store_one ( path, content) )
778806 }
779807
808+ // Store file into the backend at the given path (also used to detect mime type), returns the
809+ // chosen compression algorithm
810+ #[ instrument( skip( self ) ) ]
811+ pub ( crate ) fn store_path (
812+ & self ,
813+ target_path : impl Into < String > + std:: fmt:: Debug ,
814+ source_path : impl AsRef < Path > + std:: fmt:: Debug ,
815+ ) -> Result < CompressionAlgorithm > {
816+ self . runtime
817+ . block_on ( self . inner . store_path ( target_path, source_path) )
818+ }
819+
780820 /// sync wrapper for the list_prefix function
781821 /// purely for testing purposes since it collects all files into a Vec.
782822 #[ cfg( test) ]
@@ -843,7 +883,7 @@ pub(crate) fn rustdoc_json_path(
843883 format_version : RustdocJsonFormatVersion ,
844884) -> String {
845885 format ! (
846- "rustdoc-json/{name}/{version}/{target}/{name}_{version}_{target}_{format_version}.json.zst "
886+ "rustdoc-json/{name}/{version}/{target}/{name}_{version}_{target}_{format_version}.json"
847887 )
848888}
849889
0 commit comments