Introduce a BlobObject type for blobs

This creates a specific type for blobs, with well defined conversions
at the borders.  It also introduces a strong type for the Param::File
value since that param is often used used by the public API to set
filenames using absolute paths, but then core changes the param to a
blob before it gets to the database.

This eliminates a few more functions with very mallable C-like
arguments behaviour which combine a number of operations in one.
Because blob filenames are stored so often in arbitrary strings this
does add more code when receiving those, until the storage is fixed.

File name sanitisation is now deletated to the sanitize-filename crate
which should do a slightly better job at this.
This commit is contained in:
Floris Bruynooghe
2019-10-18 00:19:49 +02:00
committed by holger krekel
parent f0fc50d5a9
commit 6c9e16d31a
16 changed files with 909 additions and 316 deletions

View File

@@ -147,7 +147,11 @@ impl Message {
pub fn get_file(&self, context: &Context) -> Option<PathBuf> {
self.param
.get(Param::File)
.map(|f| dc_get_abs_path(context, f))
.map_or(None, |param| ParamsFile::from_param(context, param).ok())
.map(|file| match file {
ParamsFile::FsPath(path) => path,
ParamsFile::Blob(blob) => blob.to_abs_path(),
})
}
/// Check if a message has a location bound to it.
@@ -238,7 +242,12 @@ impl Message {
pub fn get_filebytes(&self, context: &Context) -> u64 {
self.param
.get(Param::File)
.map(|file| dc_get_filebytes(context, &file))
.and_then(|param| ParamsFile::from_param(context, param).ok())
.map(|file| match file {
ParamsFile::FsPath(path) => path,
ParamsFile::Blob(blob) => blob.to_abs_path(),
})
.map(|path| dc_get_filebytes(context, &path))
.unwrap_or_default()
}
@@ -321,6 +330,14 @@ impl Message {
|| cmd != SystemMessage::Unknown && cmd != SystemMessage::AutocryptSetupMessage
}
/// Whether the message is still being created.
///
/// Messages with attachments might be created before the
/// attachment is ready. In this case some more restrictions on
/// the attachment apply, e.g. if the file to be attached is still
/// being written to or otherwise will still change it can not be
/// copied to the blobdir. Thus those attachments need to be
/// created immediately in the blobdir with a valid filename.
pub fn is_increation(&self) -> bool {
chat::msgtype_has_file(self.type_0) && self.state == MessageState::OutPreparing
}
@@ -812,12 +829,17 @@ pub fn get_summarytext_by_raw(
.stock_str(StockMessage::AcSetupMsgSubject)
.to_string()
} else {
let file_name: String = if let Some(file_path) = param.get(Param::File) {
if let Some(file_name) = Path::new(file_path).file_name() {
Some(file_name.to_string_lossy().into_owned())
} else {
None
}
let file_name: String = if let Some(param) = param.get(Param::File) {
ParamsFile::from_param(context, param)
.ok()
.map(|file| match file {
ParamsFile::FsPath(path) => path,
ParamsFile::Blob(blob) => blob.to_abs_path(),
})
.and_then(|path| {
path.file_name()
.map(|fname| fname.to_string_lossy().into_owned())
})
} else {
None
}