pub fn update_share(
        &self,
        user_id: &UserID,
        path: &std::path::Path,
        enabled: bool,
        mut upload_limit: Option<u32>,
    ) -> Option<SharedID> {
        let conn = self.conn();
        let path_str = path.to_str().unwrap();
        let mut shared_id: Option<SharedID> = None;

        if enabled {
            // first check if share allready exists
            shared_id = match conn
                .query_row(
                    "SELECT ID FROM SHARED WHERE USER = ? AND BASE_PATH = ?",
                    params![&user_id.0, path_str],
                    |row| row.get(0).map(|s| SharedID::from_string_unchecked(s)),
                )
                .ok()
            {
                Some(id) => Some(id),
                None => {
                    // generate new unique id
                    let mut id = None;
                    for _ in 0..100 {
                        let share_id: String = crate::utils::get_rand_token::<16>()
                            .iter()
                            .map(|e| *e as char)
                            .collect();
                        println!("Generated share id {}", share_id);
                        match conn.query_row(
                            "SELECT EXISTS(SELECT 1 FROM SHARED WHERE ID = ?)",
                            &[&share_id],
                            |r| r.get::<usize, u32>(0).map(|e| e == 1),
                        ) {
                            Ok(false) => {
                                // shared id doesn't exist
                                id = Some(SharedID::from_string_unchecked(share_id));
                                break;
                            }
                            Ok(true) => warn!("Generated existing shared id, retry..."),
                            Err(e) => {
                                error!("{:?}", e);
                                break;
                            }
                        }
                    }
                    id
                }
            };

            if let Some(0) = upload_limit {
                upload_limit = None;
            }

            dbg!(&shared_id);

            // no share exists, create new
            // TODO check if share id allready exisits?
        }
        match shared_id {
            Some(id) => {
                // very unperformant
                let upload_limit: Box<dyn ToSql> = upload_limit
                    .map(|ul| Box::new(ul) as Box<dyn ToSql>)
                    .unwrap_or_else(|| Box::new(rusqlite::types::Null));
                conn.execute(
                    "INSERT OR REPLACE INTO SHARED (ID, USER, BASE_PATH, CREATED_AT, UPLOAD_LIMIT) VALUES (?, ?, ?, datetime('now'), ?)", 
                    params![id.as_ref(), &user_id.0, path_str, upload_limit]).map_err(|e| error!("{:?}", e)).ok()?;
                Some(id)
            }
            None => {
                conn.execute("DELETE FROM SHARED WHERE BASE_PATH = ?", params![path_str])
                    .ok();
                None
            }
        }
    }
/// if enabled, returns the share id
/// expects path to be valid
/// `upload_limit` in megabytes
if enabled, returns the share id
expects path to be valid
`upload_limit` in megabytes
if enabled returns the share id expects path to be valid ` upload_limit ` in megabytes
pub fn update_share(
        &self,
        user_id: &UserID,
        path: &std::path::Path,
        enabled: bool,
        mut upload_limit: Option<u32>,
    ) -> Option<SharedID> {
        let conn = self.conn();
        let path_str = path.to_str().unwrap();
        let mut shared_id: Option<SharedID> = None;
        if enabled {
            // first check if share allready exists
            shared_id = match conn
                .query_row(
                    "SELECT ID FROM SHARED WHERE USER = ? AND BASE_PATH = ?",
                    params![&user_id.0, path_str],
                    |row| row.get(0).map(|s| SharedID::from_string_unchecked(s)),
                )
                .ok()
            {
                Some(id) => Some(id),
                None => {
                    // generate new unique id
                    let mut id = None;
                    for _ in 0..100 {
                        let share_id: String = crate::utils::get_rand_token::<16>()
                            .iter()
                            .map(|e| *e as char)
                            .collect();
                        println!("Generated share id {}", share_id);
                        match conn.query_row(
                            "SELECT EXISTS(SELECT 1 FROM SHARED WHERE ID = ?)",
                            &[&share_id],
                            |r| r.get::<usize, u32>(0).map(|e| e == 1),
                        ) {
                            Ok(false) => {
                                // shared id doesn't exist
                                id = Some(SharedID::from_string_unchecked(share_id));
                                break;
                            }
                            Ok(true) => warn!("Generated existing shared id, retry..."),
                            Err(e) => {
                                error!("{:?}", e);
                                break;
                            }
                        }
                    }
                    id
                }
            };
            if let Some(0) = upload_limit {
                upload_limit = None;
            }
            dbg!(&shared_id);
            // no share exists, create new
            // TODO check if share id allready exisits?
        }
        match shared_id {
            Some(id) => {
                // very unperformant
                let upload_limit: Box<dyn ToSql> = upload_limit
                    .map(|ul| Box::new(ul) as Box<dyn ToSql>)
                    .unwrap_or_else(|| Box::new(rusqlite::types::Null));
                conn.execute(
                    "INSERT OR REPLACE INTO SHARED (ID, USER, BASE_PATH, CREATED_AT, UPLOAD_LIMIT) VALUES (?, ?, ?, datetime('now'), ?)", 
                    params![id.as_ref(), &user_id.0, path_str, upload_limit]).map_err(|e| error!("{:?}", e)).ok()?;
                Some(id)
            }
            None => {
                conn.execute("DELETE FROM SHARED WHERE BASE_PATH = ?", params![path_str])
                    .ok();
                None
            }
        }
    }
pub fn update_share ( & self , user_id : & UserID , path : & std :: path :: Path , enabled : bool , mut upload_limit : Option < u32 > , ) -> Option < SharedID > { let conn = self . conn ( ) ; let path_str = path . to_str ( ) . unwrap ( ) ; let mut shared_id : Option < SharedID > = None ; if enabled { // first check if share allready exists shared_id = match conn . query_row ( " " , params ! [ user_id 0 path_str ] , | row | row . get ( 0 ) . map ( | s | SharedID :: from_string_unchecked ( s ) ) , ) . ok ( ) { Some ( id ) => Some ( id ) , None => { // generate new unique id let mut id = None ; for _ in 0 .. 100 { let share_id : String = crate :: utils :: get_rand_token :: < 16 > ( ) . iter ( ) . map ( | e | * e as char ) . collect ( ) ; println ! ( " " share_id ) ; match conn . query_row ( " " , & [ & share_id ] , | r | r . get :: < usize , u32 > ( 0 ) . map ( | e | e == 1 ) , ) { Ok ( false ) => { // shared id doesn't exist id = Some ( SharedID :: from_string_unchecked ( share_id ) ) ; break ; } Ok ( true ) => warn ! ( " " ) , Err ( e ) => { error ! ( " " e ) ; break ; } } } id } } ; if let Some ( 0 ) = upload_limit { upload_limit = None ; } dbg ! ( shared_id ) ; // no share exists, create new // TODO check if share id allready exisits? } match shared_id { Some ( id ) => { // very unperformant let upload_limit : Box < dyn ToSql > = upload_limit . map ( | ul | Box :: new ( ul ) as Box < dyn ToSql > ) . unwrap_or_else ( | | Box :: new ( rusqlite :: types :: Null ) ) ; conn . execute ( " " , params ! [ id as_ref ( ) user_id 0 path_str upload_limit ] ) . map_err ( | e | error ! ( " " e ) ) . ok ( ) ? ; Some ( id ) } None => { conn . execute ( " " , params ! [ path_str ] ) . ok ( ) ; None } } }
if enabled, returns the share id
expects path to be valid
`upload_limit` in megabytes
if enabled returns the share id expects path to be valid ` upload_limit ` in megabytes
&self
user_id
&UserID
path
&std::path::Path
enabled
bool
upload_limit
Option<u32>
&self
user_id
&UserID
path
&std::path::Path
enabled
bool
upload_limit
Option<u32>