@@ -5,13 +5,13 @@ use crate::repository::bplustree::{BPlusTree, BPlusTreeQuery};
55use crate :: repository:: m3u_playlist_iterator:: M3uPlaylistM3uTextIterator ;
66use crate :: repository:: playlist_repository:: get_input_m3u_playlist_file_path;
77use crate :: repository:: storage:: { get_input_storage_path, get_target_storage_path} ;
8- use crate :: repository:: storage_const;
8+ use crate :: repository:: { storage_const} ;
99use crate :: repository:: xtream_repository:: CategoryKey ;
1010use crate :: utils;
1111use crate :: utils:: { async_file_writer, file_exists_async, FileReadGuard , IO_BUFFER_SIZE } ;
1212use indexmap:: IndexMap ;
1313use log:: error;
14- use shared:: concat_string;
14+ use shared:: { concat_string, notify_err_res } ;
1515use shared:: error:: { notify_err, str_to_io_error, string_to_io_error, TuliproxError } ;
1616use shared:: model:: { M3uPlaylistItem , PlaylistGroup } ;
1717use shared:: model:: { PlaylistItem , PlaylistItemType , XtreamCluster } ;
@@ -29,21 +29,41 @@ macro_rules! cant_write_result {
2929 }
3030}
3131
32+ macro_rules! await_playlist_write {
33+ ( $expr: expr, $fmt: literal $( , $args: expr) * ) => { {
34+ $expr. await . map_err( |err| {
35+ notify_err!( $fmt $( , $args) * , err)
36+ } ) ?
37+ } } ;
38+ }
39+
3240pub fn m3u_get_file_path_for_db ( target_path : & Path ) -> PathBuf {
33- target_path. join ( PathBuf :: from ( concat_string ! ( storage_const:: FILE_M3U , "." , storage_const:: FILE_SUFFIX_DB ) ) )
41+ target_path. join ( storage_const :: PATH_M3U ) . join ( concat_string ! ( storage_const:: FILE_M3U , "." , storage_const:: FILE_SUFFIX_DB ) )
3442}
3543
3644pub fn m3u_get_epg_file_path_for_target ( target_path : & Path ) -> PathBuf {
37- let path = target_path. join ( PathBuf :: from ( concat_string ! ( storage_const:: FILE_M3U , "." , storage_const:: FILE_SUFFIX_DB ) ) ) ;
45+ let path = target_path. join ( storage_const :: PATH_M3U ) . join ( concat_string ! ( storage_const:: FILE_M3U , "." , storage_const:: FILE_SUFFIX_DB ) ) ;
3846 utils:: add_prefix_to_filename ( & path, "epg_" , Some ( storage_const:: FILE_SUFFIX_DB ) )
3947}
4048
41- macro_rules! await_playlist_write {
42- ( $expr: expr, $fmt: literal $( , $args: expr) * ) => { {
43- $expr. await . map_err( |err| {
44- notify_err!( $fmt $( , $args) * , err)
45- } ) ?
46- } } ;
49+ pub fn m3u_get_storage_path ( cfg : & Config , target_name : & str ) -> Option < PathBuf > {
50+ get_target_storage_path ( cfg, target_name) . map ( |target_path| target_path. join ( PathBuf :: from ( storage_const:: PATH_M3U ) ) )
51+ }
52+
53+ pub async fn ensure_m3u_storage_path ( cfg : & Config , target_name : & str ) -> Result < PathBuf , TuliproxError > {
54+ if let Some ( path) = m3u_get_storage_path ( cfg, target_name) {
55+ if tokio:: fs:: create_dir_all ( & path) . await . is_err ( ) {
56+ let msg = format ! (
57+ "Failed to save m3u data, can't create directory {}" ,
58+ & path. display( )
59+ ) ;
60+ return notify_err_res ! ( "{msg}" ) ;
61+ }
62+ Ok ( path)
63+ } else {
64+ let msg = format ! ( "Failed to save m3u data, can't create directory for target {target_name}" ) ;
65+ notify_err_res ! ( "{msg}" )
66+ }
4767}
4868
4969async fn persist_m3u_playlist_as_text (
@@ -90,6 +110,9 @@ pub async fn m3u_write_playlist(
90110 return Ok ( ( ) ) ;
91111 }
92112
113+ let config = cfg. config . load ( ) ;
114+ let _m3u_path = ensure_m3u_storage_path ( & config, target. name . as_str ( ) ) . await ?;
115+
93116 let m3u_path = m3u_get_file_path_for_db ( target_path) ;
94117 let m3u_playlist = Arc :: new (
95118 new_playlist
@@ -169,7 +192,7 @@ pub async fn iter_raw_m3u_target_playlist(config: &AppConfig, target: &ConfigTar
169192
170193pub async fn iter_raw_m3u_input_playlist ( app_config : & AppConfig , input : & ConfigInput , cluster : Option < XtreamCluster > ) -> Option < ( FileReadGuard , Box < dyn Iterator < Item =M3uPlaylistItem > + Send > ) > {
171194 let working_dir = & app_config. config . load ( ) . working_dir ;
172- let storage_path = get_input_storage_path ( & input. name , working_dir) . ok ( ) ?;
195+ let storage_path = get_input_storage_path ( & input. name , working_dir) . await . ok ( ) ?;
173196 let m3u_path = get_input_m3u_playlist_file_path ( & storage_path, & input. name ) ;
174197
175198 iter_raw_m3u_playlist :: < u32 , Arc < str > > ( app_config, & m3u_path, cluster) . await
0 commit comments