Struct deltachat::blob::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

Implementations§

source§

impl<'a> BlobObject<'a>

source

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

Creates a new blob object with a unique name.

Creates a new file in the blob directory. The name will be derived from the platform-agnostic basename of the suggested name, followed by a random number and followed by a possible extension. The data will be written into the file without race-conditions.

source

async fn create_new_file( context: &Context, dir: &Path, stem: &str, ext: &str ) -> Result<(String, File)>

source

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

Creates a new blob object with unique name by copying an existing file.

This creates a new blob as described in BlobObject::create but also copies an existing file into it. This is done in a in way which avoids race-conditions when multiple files are concurrently created.

source

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

Creates a blob from a file, possibly copying it to the blobdir.

If the source file is not a path to into the blob directory the file will be copied into the blob directory first. If the source file is already in the blobdir it will not be copied and only be created if it is a valid blobname, that is no subdirectory is used and BlobObject::sanitise_name does not modify the filename.

Paths into the blob directory may be either defined by an absolute path or by the relative prefix $BLOBDIR.

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: String) -> 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.

source

pub fn as_file_name(&self) -> &str

Returns the filename of the blob.

source

pub fn as_rel_path(&self) -> &Path

The path relative in the blob directory.

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 sanitise_name(name: &str) -> (String, String)

Create a safe name based on a messy input string.

The safe name will be a valid filename on Unix and Windows and not contain any path separators. The input can contain path segments separated by either Unix or Windows path separators, the rightmost non-empty segment will be used as name, sanitised for special characters.

The resulting name is returned as a tuple, the first part being the stem or basename and the second being an extension, including the dot. E.g. “foo.txt” is returned as ("foo", ".txt") while “bar” is returned as ("bar", "").

The extension part will always be lowercased.

source

fn is_acceptible_blob_name(name: impl AsRef<OsStr>) -> 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 is actually utf-8.

source

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

Returns path to the stored Base64-decoded blob.

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

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, maybe_sticker: &mut bool ) -> Result<()>

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, blob_abs: PathBuf, maybe_sticker: &mut bool, img_wh: u32, max_bytes: usize, strict_limits: bool ) -> Result<Option<String>>

If !strict_limits, then if max_bytes is exceeded, reduce the image to img_wh and just proceed with the result.

source

pub fn metadata(&self) -> Result<(u64, Option<Exif>)>

Returns image file size and Exif.

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> Display 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> 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
§

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> Instrument for T

source§

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

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

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.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> RpcError for T
where T: Debug + Display + Send + Sync + Unpin + 'static,