deltachat::blob

Struct BlobObject

Source
pub struct BlobObject<'a> {
    blobdir: &'a Path,
    name: String,
}
Expand description

Represents a file in the blob directory.

The object has a name, which will always be valid UTF-8. Having a blob object does not imply the respective file exists, however when using one of the create*() methods a unique file is created.

Fields§

§blobdir: &'a Path§name: String

The name of the file on the disc. Note that this is NOT the user-visible filename, which is only stored in Param::Filename on the message.

Implementations§

Source§

impl<'a> BlobObject<'a>

Source

pub fn create_and_deduplicate( context: &'a Context, src: &Path, original_name: &Path, ) -> Result<BlobObject<'a>>

Creates a blob object by copying or renaming an existing file. If the source file is already in the blobdir, it will be renamed, otherwise it will be copied to the blobdir first.

In order to deduplicate files that contain the same data, the file will be named <hash>.<extension>, e.g. ce940175885d7b78f7b7e9f1396611f.jpg. The original_name param is only used to get the extension.

This is done in a in way which avoids race-conditions when multiple files are concurrently created.

Source

pub fn create_and_deduplicate_from_bytes( context: &'a Context, data: &[u8], original_name: &str, ) -> Result<BlobObject<'a>>

Creates a new blob object with the file contents in data. In order to deduplicate files that contain the same data, the file will be named <hash>.<extension>, e.g. ce940175885d7b78f7b7e9f1396611f.jpg. The original_name param is only used to get the extension.

The data will be written into the file without race-conditions.

This function does blocking I/O, but it can still be called from an async context because block_in_place() is used to leave the async runtime if necessary.

Source

pub fn from_path(context: &'a Context, path: &Path) -> Result<BlobObject<'a>>

Returns a BlobObject for an existing blob from a path.

The path must designate a file directly in the blobdir and must use a valid blob name. That is after sanitisation the name must still be the same, that means it must be valid UTF-8 and not have any special characters in it.

Source

pub fn from_name(context: &'a Context, name: &str) -> Result<BlobObject<'a>>

Returns a BlobObject for an existing blob.

The name may optionally be prefixed with the $BLOBDIR/ prefixed, as returned by BlobObject::as_name. This is how you want to create a BlobObject for a filename read from the database.

Source

pub fn to_abs_path(&self) -> PathBuf

Returns the absolute path to the blob in the filesystem.

Source

pub fn as_name(&self) -> &str

Returns the blob name, as stored in the database.

This returns the blob in the $BLOBDIR/<name> format used in the database. Do not use this unless you’re about to store this string in the database or Params. Eventually even those conversions should be handled by the type system.

Note that this is NOT the user-visible filename, which is only stored in Param::Filename on the message.

Source

pub fn suffix(&self) -> Option<&str>

Returns the extension of the blob.

If a blob’s filename has an extension, it is always guaranteed to be lowercase.

Source

fn is_acceptible_blob_name(name: &str) -> bool

Checks whether a name is a valid blob name.

This is slightly less strict than stanitise_name, presumably someone already created a file with such a name so we just ensure it’s not actually a path in disguise.

Acceptible blob name always have to be valid utf-8.

Source

pub(crate) fn store_from_base64(context: &Context, data: &str) -> Result<String>

Returns path to the stored Base64-decoded blob.

If data represents an image of known format, this adds the corresponding extension.

Even though this function is not async, it’s OK to call it from an async context.

Source

pub async fn recode_to_avatar_size(&mut self, context: &Context) -> Result<()>

Source

pub async fn recode_to_image_size( &mut self, context: &Context, name: Option<String>, maybe_sticker: &mut bool, ) -> Result<String>

Recodes an image pointed by a BlobObject so that it fits into limits on the image width, height and file size specified by the config.

On some platforms images are passed to the core as crate::message::Viewtype::Sticker in which case maybe_sticker flag should be set. We recheck if an image is a true sticker assuming that it must have at least one fully transparent corner, otherwise this flag is reset.

Source

fn recode_to_size( &mut self, context: &Context, name: Option<String>, maybe_sticker: &mut bool, img_wh: u32, max_bytes: usize, is_avatar: bool, ) -> Result<String>

Recodes the image so that it fits into limits on width/height and byte size.

If !is_avatar, then if max_bytes is exceeded, reduces the image to img_wh and proceeds with the result without rechecking.

This modifies the blob object in-place.

Additionally, if you pass the user-visible filename as name then the updated user-visible filename will be returned; this may be necessary because the format may be changed to JPG, i.e. “image.png” -> “image.jpg”.

Trait Implementations§

Source§

impl<'a> Clone for BlobObject<'a>

Source§

fn clone(&self) -> BlobObject<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for BlobObject<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> PartialEq for BlobObject<'a>

Source§

fn eq(&self, other: &BlobObject<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for BlobObject<'a>

Source§

impl<'a> StructuralPartialEq for BlobObject<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for BlobObject<'a>

§

impl<'a> RefUnwindSafe for BlobObject<'a>

§

impl<'a> Send for BlobObject<'a>

§

impl<'a> Sync for BlobObject<'a>

§

impl<'a> Unpin for BlobObject<'a>

§

impl<'a> UnwindSafe for BlobObject<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T