refactor: Remove Message.set_file() / dc_msg_set_file() and related code (#6558)

Now that we are deduplicating everywhere, we can get rid of some code.

The old python bindings did not get an optional `name` parameter because
they are deprecated anyway, but it would be easy to add it.
This commit is contained in:
Hocuri
2025-02-22 10:47:52 +01:00
committed by GitHub
parent 253331b7fd
commit a49dfeca6e
9 changed files with 77 additions and 108 deletions

View File

@@ -288,16 +288,6 @@ impl<'a> BlobObject<'a> {
&self.name
}
/// Returns the filename of the blob.
pub fn as_file_name(&self) -> &str {
self.name.rsplit('/').next().unwrap_or_default()
}
/// The path relative in the blob directory.
pub fn as_rel_path(&self) -> &Path {
Path::new(self.as_file_name())
}
/// Returns the extension of the blob.
///
/// If a blob's filename has an extension, it is always guaranteed

View File

@@ -2,6 +2,7 @@ use std::time::Duration;
use super::*;
use crate::message::{Message, Viewtype};
use crate::param::Param;
use crate::sql;
use crate::test_utils::{self, TestContext};
use crate::tools::SystemTime;
@@ -44,20 +45,6 @@ async fn test_lowercase_ext() {
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_as_file_name() {
let t = TestContext::new().await;
let blob = BlobObject::create_and_deduplicate_from_bytes(&t, FILE_BYTES, "foo.txt").unwrap();
assert_eq!(blob.as_file_name(), FILE_DEDUPLICATED);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_as_rel_path() {
let t = TestContext::new().await;
let blob = BlobObject::create_and_deduplicate_from_bytes(&t, FILE_BYTES, "foo.txt").unwrap();
assert_eq!(blob.as_rel_path(), Path::new(FILE_DEDUPLICATED));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_suffix() {
let t = TestContext::new().await;
@@ -655,6 +642,12 @@ impl SendImageCheckMediaquality<'_> {
alice_msg.save_file(&alice, &file_saved).await?;
check_image_size(file_saved, compressed_width, compressed_height);
if original_width == compressed_width {
assert_extension(&alice, alice_msg, extension).await;
} else {
assert_extension(&alice, alice_msg, "jpg").await;
}
let bob_msg = bob.recv_msg(&sent).await;
assert_eq!(bob_msg.get_viewtype(), Viewtype::Image);
assert_eq!(bob_msg.get_width() as u32, compressed_width);
@@ -673,10 +666,53 @@ impl SendImageCheckMediaquality<'_> {
assert!(exif.is_none());
let img = check_image_size(file_saved, compressed_width, compressed_height);
if original_width == compressed_width {
assert_extension(&bob, bob_msg, extension).await;
} else {
assert_extension(&bob, bob_msg, "jpg").await;
}
Ok(img)
}
}
async fn assert_extension(context: &TestContext, msg: Message, extension: &str) {
assert!(msg
.param
.get(Param::File)
.unwrap()
.ends_with(&format!(".{extension}")));
assert!(msg
.param
.get(Param::Filename)
.unwrap()
.ends_with(&format!(".{extension}")));
assert!(msg
.get_filename()
.unwrap()
.ends_with(&format!(".{extension}")));
assert_eq!(
msg.get_file(context)
.unwrap()
.extension()
.unwrap()
.to_str()
.unwrap(),
extension
);
assert_eq!(
msg.param
.get_blob(Param::File, context)
.await
.unwrap()
.unwrap()
.suffix()
.unwrap(),
extension
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_send_big_gif_as_image() -> Result<()> {
let bytes = include_bytes!("../../test-data/image/screenshot.gif");

View File

@@ -890,12 +890,6 @@ impl ChatId {
}
}
_ => {
let blob = msg
.param
.get_blob(Param::File, context)
.await?
.context("no file stored in params")?;
msg.param.set(Param::File, blob.as_name());
if msg.viewtype == Viewtype::File {
if let Some((better_type, _)) = message::guess_msgtype_from_suffix(msg)
// We do not do an automatic conversion to other viewtypes here so that
@@ -908,6 +902,11 @@ impl ChatId {
}
}
if msg.viewtype == Viewtype::Vcard {
let blob = msg
.param
.get_blob(Param::File, context)
.await?
.context("no file stored in params")?;
msg.try_set_vcard(context, &blob.to_abs_path()).await?;
}
}
@@ -2801,20 +2800,12 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
.recode_to_image_size(context, msg.get_filename(), &mut maybe_sticker)
.await?;
msg.param.set(Param::Filename, new_name);
msg.param.set(Param::File, blob.as_name());
if !maybe_sticker {
msg.viewtype = Viewtype::Image;
}
}
msg.param.set(Param::File, blob.as_name());
if let (Some(filename), Some(blob_ext)) = (msg.param.get(Param::Filename), blob.suffix()) {
let stem = match filename.rsplit_once('.') {
Some((stem, _)) => stem,
None => filename,
};
msg.param
.set(Param::Filename, stem.to_string() + "." + blob_ext);
}
if !msg.param.exists(Param::MimeType) {
if let Some((_, mime)) = message::guess_msgtype_from_suffix(msg) {

View File

@@ -1075,21 +1075,6 @@ impl Message {
self.subject = subject;
}
/// Sets the file associated with a message.
///
/// This function does not use the file or check if it exists,
/// the file will only be used when the message is prepared
/// for sending.
pub fn set_file(&mut self, file: impl ToString, filemime: Option<&str>) {
if let Some(name) = Path::new(&file.to_string()).file_name() {
if let Some(name) = name.to_str() {
self.param.set(Param::Filename, name);
}
}
self.param.set(Param::File, file);
self.param.set_optional(Param::MimeType, filemime);
}
/// Sets the file associated with a message, deduplicating files with the same name.
///
/// If `name` is Some, it is used as the file name
@@ -2167,12 +2152,12 @@ pub enum Viewtype {
/// Image message.
/// If the image is a GIF and has the appropriate extension, the viewtype is auto-changed to
/// `Gif` when sending the message.
/// File, width and height are set via dc_msg_set_file(), dc_msg_set_dimension
/// and retrieved via dc_msg_set_file(), dc_msg_set_dimension().
/// File, width and height are set via dc_msg_set_file_and_deduplicate(), dc_msg_set_dimension()
/// and retrieved via dc_msg_get_file(), dc_msg_get_height(), dc_msg_get_width().
Image = 20,
/// Animated GIF message.
/// File, width and height are set via dc_msg_set_file(), dc_msg_set_dimension()
/// File, width and height are set via dc_msg_set_file_and_deduplicate(), dc_msg_set_dimension()
/// and retrieved via dc_msg_get_file(), dc_msg_get_width(), dc_msg_get_height().
Gif = 21,
@@ -2185,26 +2170,26 @@ pub enum Viewtype {
Sticker = 23,
/// Message containing an Audio file.
/// File and duration are set via dc_msg_set_file(), dc_msg_set_duration()
/// File and duration are set via dc_msg_set_file_and_deduplicate(), dc_msg_set_duration()
/// and retrieved via dc_msg_get_file(), dc_msg_get_duration().
Audio = 40,
/// A voice message that was directly recorded by the user.
/// For all other audio messages, the type #DC_MSG_AUDIO should be used.
/// File and duration are set via dc_msg_set_file(), dc_msg_set_duration()
/// File and duration are set via dc_msg_set_file_and_deduplicate(), dc_msg_set_duration()
/// and retrieved via dc_msg_get_file(), dc_msg_get_duration()
Voice = 41,
/// Video messages.
/// File, width, height and durarion
/// are set via dc_msg_set_file(), dc_msg_set_dimension(), dc_msg_set_duration()
/// are set via dc_msg_set_file_and_deduplicate(), dc_msg_set_dimension(), dc_msg_set_duration()
/// and retrieved via
/// dc_msg_get_file(), dc_msg_get_width(),
/// dc_msg_get_height(), dc_msg_get_duration().
Video = 50,
/// Message containing any file, eg. a PDF.
/// The file is set via dc_msg_set_file()
/// The file is set via dc_msg_set_file_and_deduplicate()
/// and retrieved via dc_msg_get_file().
File = 60,

View File

@@ -547,7 +547,7 @@ mod tests {
fs::write(fname, b"boo").await.unwrap();
let blob = p.get_blob(Param::File, &t).await.unwrap().unwrap();
assert!(blob.as_file_name().starts_with("foo"));
assert!(blob.as_name().starts_with("$BLOBDIR/foo"));
// Blob in blobdir, expect blob.
let bar_path = t.get_blobdir().join("bar");