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>
impl<'a> BlobObject<'a>
sourcepub async fn create(
context: &'a Context,
suggested_name: &str,
data: &[u8],
) -> Result<BlobObject<'a>>
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.
async fn create_new_file( context: &Context, dir: &Path, stem: &str, ext: &str, ) -> Result<(String, File)>
sourcepub async fn create_and_copy(
context: &'a Context,
src: &Path,
) -> Result<BlobObject<'a>>
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.
sourcepub async fn new_from_path(
context: &'a Context,
src: &Path,
) -> Result<BlobObject<'a>>
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
.
sourcepub fn from_path(context: &'a Context, path: &Path) -> Result<BlobObject<'a>>
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.
sourcepub fn from_name(context: &'a Context, name: String) -> Result<BlobObject<'a>>
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.
sourcepub fn to_abs_path(&self) -> PathBuf
pub fn to_abs_path(&self) -> PathBuf
Returns the absolute path to the blob in the filesystem.
sourcepub fn as_name(&self) -> &str
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.
sourcepub fn as_file_name(&self) -> &str
pub fn as_file_name(&self) -> &str
Returns the filename of the blob.
sourcepub fn as_rel_path(&self) -> &Path
pub fn as_rel_path(&self) -> &Path
The path relative in the blob directory.
sourcepub fn suffix(&self) -> Option<&str>
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.
sourcefn sanitise_name(name: &str) -> (String, String)
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.
sourcefn is_acceptible_blob_name(name: impl AsRef<OsStr>) -> bool
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.
sourcepub(crate) async fn store_from_base64(
context: &Context,
data: &str,
suggested_file_stem: &str,
) -> Result<String>
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
.
pub async fn recode_to_avatar_size(&mut self, context: &Context) -> Result<()>
sourcepub async fn recode_to_image_size(
&mut self,
context: &Context,
maybe_sticker: &mut bool,
) -> Result<()>
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.
Trait Implementations§
source§impl<'a> Clone for BlobObject<'a>
impl<'a> Clone for BlobObject<'a>
source§fn clone(&self) -> BlobObject<'a>
fn clone(&self) -> BlobObject<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for BlobObject<'a>
impl<'a> Debug for BlobObject<'a>
source§impl Display for BlobObject<'_>
impl Display for BlobObject<'_>
source§impl<'a> PartialEq for BlobObject<'a>
impl<'a> PartialEq for BlobObject<'a>
impl<'a> Eq for BlobObject<'a>
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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.