Skip to content

Commit

Permalink
Fix attachment saving process
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Oct 21, 2024
1 parent aba808d commit 3f9cc61
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
83 changes: 48 additions & 35 deletions application/apps/indexer/session/src/state/attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,62 @@ pub struct AttachmentInfo {
}

const FILE_NAME_INDEXES_LIMIT: usize = 1000;
const ALLOWED_FILENAME_CHARS: &[char] = &['-', '_'];

fn get_valid_file_path(dest: &Path, origin: &str) -> Result<PathBuf, io::Error> {
fn sanitize<S: AsRef<str>>(input: S) -> String {
input
.as_ref()
.chars()
.map(|ch| {
if ch.is_alphanumeric() || ALLOWED_FILENAME_CHARS.contains(&ch) {
ch
} else {
'_'
}
})
.collect()
}
let origin_path = PathBuf::from(origin);
let origin_file_name = PathBuf::from(origin_path.file_name().ok_or(io::Error::new(
io::ErrorKind::Other,
format!("Cannot get file name from {origin:?}"),
))?);
if let Some(basename) = origin_file_name.file_stem() {
let extension = origin_file_name.extension();
let mut index: usize = 0;
loop {
let mut suggestion = if index == 0 {
dest.join(PathBuf::from(basename))
} else {
dest.join(PathBuf::from(format!(
"{}_{index}",
basename.to_string_lossy()
)))
};
if let Some(extension) = extension {
suggestion = PathBuf::from(format!(
"{}.{}",
suggestion.to_string_lossy(),
extension.to_string_lossy()
));
}
if !suggestion.exists() {
return Ok(suggestion);
} else {
index += 1;
}
if index > FILE_NAME_INDEXES_LIMIT {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Cannot find suitable file name for {origin}"),
));
}
let basename = sanitize(
origin_file_name
.file_stem()
.ok_or(io::Error::new(
io::ErrorKind::Other,
"Fail to parse origin attachment path",
))?
.to_string_lossy(),
);
let extension = origin_file_name.extension();
let mut index: usize = 0;
loop {
let mut suggestion = if index == 0 {
dest.join(PathBuf::from(&basename))
} else {
dest.join(PathBuf::from(format!("{basename}_{index}")))
};
if let Some(extension) = extension {
suggestion = PathBuf::from(format!(
"{}.{}",
suggestion.to_string_lossy(),
sanitize(extension.to_string_lossy())
));
}
if !suggestion.exists() {
return Ok(suggestion);
} else {
index += 1;
}
if index > FILE_NAME_INDEXES_LIMIT {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Cannot find suitable file name for {origin}"),
));
}
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"Fail to parse origin attachment path",
))
}
}

Expand Down
7 changes: 6 additions & 1 deletion application/apps/indexer/session/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,12 @@ pub async fn run(
state.cancelling_operations.remove(&uuid);
}
Api::AddAttachment(attachment) => {
state.handle_add_attachment(attachment, tx_callback_events.clone())?;
let at_name = attachment.name.clone();
if let Err(err) =
state.handle_add_attachment(attachment, tx_callback_events.clone())
{
error!("Fail to process attachment {at_name:?}; error: {err:?}");
}
}
Api::GetAttachments(tx_response) => {
tx_response.send(state.attachments.get()).map_err(|_| {
Expand Down

0 comments on commit 3f9cc61

Please sign in to comment.