remove dc_get_fine_* method and validate_filename

This commit is contained in:
holger krekel
2019-09-29 04:35:18 +02:00
committed by Floris Bruynooghe
parent 7a9fdb4acd
commit d72e9bb05b
3 changed files with 43 additions and 76 deletions

View File

@@ -10,7 +10,7 @@ use libc::uintptr_t;
use crate::chat::*;
use crate::constants::*;
use crate::contact::*;
use crate::dc_tools::dc_derive_safe_stem_ext;
use crate::dc_tools::{dc_copy_file, dc_derive_safe_stem_ext};
use crate::error::*;
use crate::events::Event;
use crate::imap::*;
@@ -162,6 +162,22 @@ impl Context {
self.blobdir.as_path()
}
pub fn copy_to_blobdir(&self, orig_filename: impl AsRef<str>) -> Result<String> {
// return a $BLOBDIR/<filename> with the content of orig_filename
// copied into it. The <filename> will be safely derived from
// orig_filename, and will not clash with existing filenames.
let dest = self.new_blob_file(&orig_filename, b"")?;
if dc_copy_file(
&self,
PathBuf::from(orig_filename.as_ref()),
PathBuf::from(&dest),
) {
Ok(dest)
} else {
bail!("could not copy {} to {}", orig_filename.as_ref(), dest);
}
}
pub fn new_blob_file(&self, orig_filename: impl AsRef<str>, data: &[u8]) -> Result<String> {
// return a $BLOBDIR/<FILENAME> string which corresponds to the
// respective file in the blobdir, and which contains the data.
@@ -526,16 +542,15 @@ mod tests {
let y = &context.new_blob_file("hello", b"data").unwrap();
assert!(dc_file_exist(&context, y));
assert!(y.starts_with("$BLOBDIR/data-"));
assert!(y.starts_with("$BLOBDIR/hello-"));
let x = &context.new_blob_file("hello", b"data.png").unwrap();
let x = &context.new_blob_file("xyz/hello.png", b"data").unwrap();
assert!(dc_file_exist(&context, x));
assert!(x.starts_with("$BLOBDIR"));
assert_eq!(x, "$BLOBDIR/hello.png");
let y = &context.new_blob_file("hello", b"data.png").unwrap();
let y = &context.new_blob_file("hello\\world.png", b"data").unwrap();
assert!(dc_file_exist(&context, y));
assert!(y.starts_with("$BLOBDIR/data-"));
assert!(y.ends_with(".png"));
assert_eq!(y, "$BLOBDIR/world.png");
}
#[test]