2020-05-18 16:53:34 +01:00
|
|
|
use crate::{utils, Error, Result};
|
2020-05-19 17:31:34 +01:00
|
|
|
use std::mem;
|
2020-05-18 16:53:34 +01:00
|
|
|
|
2020-07-28 13:59:30 +01:00
|
|
|
pub struct FileMeta {
|
|
|
|
pub filename: Option<String>,
|
|
|
|
pub content_type: String,
|
|
|
|
pub file: Vec<u8>,
|
|
|
|
}
|
2020-07-26 04:56:50 +01:00
|
|
|
|
2020-05-18 16:53:34 +01:00
|
|
|
pub struct Media {
|
2020-05-19 17:31:34 +01:00
|
|
|
pub(super) mediaid_file: sled::Tree, // MediaId = MXC + WidthHeight + Filename + ContentType
|
2020-05-18 16:53:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Media {
|
|
|
|
/// Uploads or replaces a file.
|
|
|
|
pub fn create(
|
|
|
|
&self,
|
|
|
|
mxc: String,
|
|
|
|
filename: Option<&String>,
|
|
|
|
content_type: &str,
|
|
|
|
file: &[u8],
|
|
|
|
) -> Result<()> {
|
|
|
|
let mut key = mxc.as_bytes().to_vec();
|
|
|
|
key.push(0xff);
|
2020-05-19 17:31:34 +01:00
|
|
|
key.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
|
|
|
|
key.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
|
|
|
key.push(0xff);
|
2020-05-18 16:53:34 +01:00
|
|
|
key.extend_from_slice(filename.map(|f| f.as_bytes()).unwrap_or_default());
|
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(content_type.as_bytes());
|
|
|
|
|
|
|
|
self.mediaid_file.insert(key, file)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Downloads a file.
|
2020-07-26 04:56:50 +01:00
|
|
|
pub fn get(&self, mxc: String) -> Result<Option<FileMeta>> {
|
2020-05-18 16:53:34 +01:00
|
|
|
let mut prefix = mxc.as_bytes().to_vec();
|
|
|
|
prefix.push(0xff);
|
2020-05-19 17:31:34 +01:00
|
|
|
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
|
|
|
|
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
|
|
|
prefix.push(0xff);
|
2020-05-18 16:53:34 +01:00
|
|
|
|
|
|
|
if let Some(r) = self.mediaid_file.scan_prefix(&prefix).next() {
|
|
|
|
let (key, file) = r?;
|
2020-05-19 17:31:34 +01:00
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
|
|
|
|
|
|
let content_type = utils::string_from_bytes(
|
|
|
|
parts
|
|
|
|
.next()
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?,
|
2020-06-09 14:13:17 +01:00
|
|
|
)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Content type in mediaid_file is invalid unicode."))?;
|
2020-05-18 16:53:34 +01:00
|
|
|
|
|
|
|
let filename_bytes = parts
|
|
|
|
.next()
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
2020-06-09 14:13:17 +01:00
|
|
|
|
2020-05-18 16:53:34 +01:00
|
|
|
let filename = if filename_bytes.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-06-11 09:03:08 +01:00
|
|
|
Some(utils::string_from_bytes(filename_bytes).map_err(|_| {
|
|
|
|
Error::bad_database("Filename in mediaid_file is invalid unicode.")
|
|
|
|
})?)
|
2020-05-18 16:53:34 +01:00
|
|
|
};
|
|
|
|
|
2020-07-28 13:59:30 +01:00
|
|
|
Ok(Some(FileMeta {
|
|
|
|
filename,
|
|
|
|
content_type,
|
|
|
|
file: file.to_vec(),
|
|
|
|
}))
|
2020-05-19 17:31:34 +01:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Downloads a file's thumbnail.
|
2020-07-26 04:56:50 +01:00
|
|
|
pub fn get_thumbnail(&self, mxc: String, width: u32, height: u32) -> Result<Option<FileMeta>> {
|
2020-05-19 17:31:34 +01:00
|
|
|
let mut main_prefix = mxc.as_bytes().to_vec();
|
|
|
|
main_prefix.push(0xff);
|
|
|
|
|
|
|
|
let mut thumbnail_prefix = main_prefix.clone();
|
|
|
|
thumbnail_prefix.extend_from_slice(&width.to_be_bytes());
|
|
|
|
thumbnail_prefix.extend_from_slice(&height.to_be_bytes());
|
|
|
|
thumbnail_prefix.push(0xff);
|
|
|
|
|
|
|
|
let mut original_prefix = main_prefix;
|
|
|
|
original_prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
|
|
|
|
original_prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
|
|
|
|
original_prefix.push(0xff);
|
|
|
|
|
|
|
|
if let Some(r) = self.mediaid_file.scan_prefix(&thumbnail_prefix).next() {
|
|
|
|
// Using saved thumbnail
|
|
|
|
let (key, file) = r?;
|
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
|
|
|
2020-05-18 16:53:34 +01:00
|
|
|
let content_type = utils::string_from_bytes(
|
|
|
|
parts
|
|
|
|
.next()
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Invalid Media ID in db"))?,
|
2020-06-09 14:13:17 +01:00
|
|
|
)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Content type in mediaid_file is invalid unicode."))?;
|
2020-05-18 16:53:34 +01:00
|
|
|
|
2020-05-19 17:31:34 +01:00
|
|
|
let filename_bytes = parts
|
|
|
|
.next()
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
2020-06-09 14:13:17 +01:00
|
|
|
|
2020-05-19 17:31:34 +01:00
|
|
|
let filename = if filename_bytes.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-06-09 14:13:17 +01:00
|
|
|
Some(
|
|
|
|
utils::string_from_bytes(filename_bytes)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Filename in db is invalid."))?,
|
2020-06-09 14:13:17 +01:00
|
|
|
)
|
2020-05-19 17:31:34 +01:00
|
|
|
};
|
|
|
|
|
2020-07-28 13:59:30 +01:00
|
|
|
Ok(Some(FileMeta {
|
|
|
|
filename,
|
|
|
|
content_type,
|
|
|
|
file: file.to_vec(),
|
|
|
|
}))
|
2020-05-19 17:31:34 +01:00
|
|
|
} else if let Some(r) = self.mediaid_file.scan_prefix(&original_prefix).next() {
|
|
|
|
// Generate a thumbnail
|
|
|
|
let (key, file) = r?;
|
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff);
|
|
|
|
|
|
|
|
let content_type = utils::string_from_bytes(
|
|
|
|
parts
|
|
|
|
.next()
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?,
|
2020-06-09 14:13:17 +01:00
|
|
|
)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Content type in mediaid_file is invalid unicode."))?;
|
2020-05-19 17:31:34 +01:00
|
|
|
|
|
|
|
let filename_bytes = parts
|
|
|
|
.next()
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
|
2020-06-09 14:13:17 +01:00
|
|
|
|
2020-05-19 17:31:34 +01:00
|
|
|
let filename = if filename_bytes.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-06-11 09:03:08 +01:00
|
|
|
Some(utils::string_from_bytes(filename_bytes).map_err(|_| {
|
|
|
|
Error::bad_database("Filename in mediaid_file is invalid unicode.")
|
|
|
|
})?)
|
2020-05-19 17:31:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(image) = image::load_from_memory(&file) {
|
|
|
|
let thumbnail = image.thumbnail(width, height);
|
|
|
|
let mut thumbnail_bytes = Vec::new();
|
2020-05-19 22:50:20 +01:00
|
|
|
thumbnail.write_to(&mut thumbnail_bytes, image::ImageOutputFormat::Png)?;
|
2020-05-19 17:31:34 +01:00
|
|
|
|
|
|
|
// Save thumbnail in database so we don't have to generate it again next time
|
|
|
|
let mut thumbnail_key = key.to_vec();
|
|
|
|
let width_index = thumbnail_key
|
|
|
|
.iter()
|
|
|
|
.position(|&b| b == 0xff)
|
2020-06-11 09:03:08 +01:00
|
|
|
.ok_or_else(|| Error::bad_database("Media in db is invalid."))?
|
2020-05-19 17:31:34 +01:00
|
|
|
+ 1;
|
|
|
|
let mut widthheight = width.to_be_bytes().to_vec();
|
|
|
|
widthheight.extend_from_slice(&height.to_be_bytes());
|
|
|
|
|
|
|
|
thumbnail_key.splice(
|
|
|
|
width_index..width_index + 2 * mem::size_of::<u32>(),
|
|
|
|
widthheight,
|
|
|
|
);
|
|
|
|
|
|
|
|
self.mediaid_file.insert(thumbnail_key, &*thumbnail_bytes)?;
|
|
|
|
|
2020-07-28 13:59:30 +01:00
|
|
|
Ok(Some(FileMeta {
|
|
|
|
filename,
|
|
|
|
content_type,
|
|
|
|
file: thumbnail_bytes.to_vec(),
|
|
|
|
}))
|
2020-05-19 17:31:34 +01:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
2020-05-18 16:53:34 +01:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|