Skip to content

Commit

Permalink
fix: Limit chars on filename path
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomtir committed Dec 21, 2024
1 parent 6eb5b03 commit 188476e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/common/fs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use crate::error::Error;
use std::path::PathBuf;
use regex::Regex;

pub fn local_path(partial_path: &str) -> Result<PathBuf, Error> {
let exe_path = std::env::current_exe().map_err(|_| Error::Default)?;
let exe_folder = exe_path.parent().ok_or(Error::Default)?;
Ok(exe_folder.join(partial_path))
}

pub fn validate_path(partial_path: &str) -> Result<(), Error> {
match Regex::new(r"^[a-zA-Z0-9_.+\-]+$") {
Err(..) => Err(Error::RegexError),
Ok(regex) => match regex.is_match(partial_path) {
false => Err(Error::Default),
true => Ok(()),
},
}
}
8 changes: 8 additions & 0 deletions src/db/club/club.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub fn club_info(club_id: u32) -> Result<Club, Error> {
}

pub fn club_create(club: &Club) -> Result<u32, Error> {
if let Some(image_url) = &club.image_url {
crate::common::fs::validate_path(image_url)?;
}

let mut conn: PooledConn = get_pool_conn();
let stmt = conn.prep(
"INSERT INTO clubs (club_key, name, description, disciplines, image_url, chairman)
Expand All @@ -71,6 +75,10 @@ pub fn club_create(club: &Club) -> Result<u32, Error> {
}

pub fn club_edit(club_id: u32, club: &Club) -> Result<(), Error> {
if let Some(image_url) = &club.image_url {
crate::common::fs::validate_path(image_url)?;
}

let mut conn: PooledConn = get_pool_conn();
let stmt = conn.prep(
"UPDATE clubs SET
Expand Down

0 comments on commit 188476e

Please sign in to comment.