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>
impl<'a> BlobObject<'a>
Sourcepub fn create_and_deduplicate(
context: &'a Context,
src: &Path,
original_name: &Path,
) -> Result<BlobObject<'a>>
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.
Sourcepub fn create_and_deduplicate_from_bytes(
context: &'a Context,
data: &[u8],
original_name: &str,
) -> Result<BlobObject<'a>>
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.
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: &str) -> Result<BlobObject<'a>>
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.
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.
Note that this is NOT the user-visible filename, which is only stored in Param::Filename on the message.
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 is_acceptible_blob_name(name: &str) -> bool
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.
Sourcepub(crate) fn store_from_base64(context: &Context, data: &str) -> Result<String>
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.
pub async fn recode_to_avatar_size(&mut self, context: &Context) -> Result<()>
Sourcepub async fn recode_to_image_size(
&mut self,
context: &Context,
name: Option<String>,
maybe_sticker: &mut bool,
) -> Result<String>
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.
Sourcefn 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>
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>
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<'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,
§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.