|
|
|
@ -1,7 +1,9 @@ |
|
|
|
use image::{imageops::FilterType, GenericImageView}; |
|
|
|
use image::{imageops::FilterType, GenericImageView}; |
|
|
|
|
|
|
|
use crate::database::globals::Globals; |
|
|
|
|
|
|
|
|
|
|
|
use crate::{utils, Error, Result}; |
|
|
|
use crate::{utils, Error, Result}; |
|
|
|
use std::mem; |
|
|
|
use std::{mem}; |
|
|
|
|
|
|
|
use tokio::{fs::{self, File}, io::AsyncWriteExt, io::AsyncReadExt}; |
|
|
|
|
|
|
|
|
|
|
|
pub struct FileMeta { |
|
|
|
pub struct FileMeta { |
|
|
|
pub filename: Option<String>, |
|
|
|
pub filename: Option<String>, |
|
|
|
@ -15,10 +17,11 @@ pub struct Media { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Media { |
|
|
|
impl Media { |
|
|
|
/// Uploads or replaces a file.
|
|
|
|
/// Uploads a file.
|
|
|
|
pub fn create( |
|
|
|
pub async fn create( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
mxc: String, |
|
|
|
mxc: String, |
|
|
|
|
|
|
|
globals: &Globals, |
|
|
|
filename: &Option<&str>, |
|
|
|
filename: &Option<&str>, |
|
|
|
content_type: &Option<&str>, |
|
|
|
content_type: &Option<&str>, |
|
|
|
file: &[u8], |
|
|
|
file: &[u8], |
|
|
|
@ -37,15 +40,20 @@ impl Media { |
|
|
|
.unwrap_or_default(), |
|
|
|
.unwrap_or_default(), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
self.mediaid_file.insert(key, file)?; |
|
|
|
let path = globals.get_media_file(&key); |
|
|
|
|
|
|
|
fs::create_dir_all(path.parent().unwrap()).await?; |
|
|
|
|
|
|
|
let mut f = File::create(path).await?; |
|
|
|
|
|
|
|
f.write_all(file).await?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.mediaid_file.insert(key, vec![])?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Uploads or replaces a file thumbnail.
|
|
|
|
/// Uploads or replaces a file thumbnail.
|
|
|
|
pub fn upload_thumbnail( |
|
|
|
pub async fn upload_thumbnail( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
mxc: String, |
|
|
|
mxc: String, |
|
|
|
|
|
|
|
globals: &Globals, |
|
|
|
filename: &Option<String>, |
|
|
|
filename: &Option<String>, |
|
|
|
content_type: &Option<String>, |
|
|
|
content_type: &Option<String>, |
|
|
|
width: u32, |
|
|
|
width: u32, |
|
|
|
@ -66,13 +74,18 @@ impl Media { |
|
|
|
.unwrap_or_default(), |
|
|
|
.unwrap_or_default(), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
self.mediaid_file.insert(key, file)?; |
|
|
|
let path = globals.get_media_file(&key); |
|
|
|
|
|
|
|
fs::create_dir_all(path.parent().unwrap()).await?; |
|
|
|
|
|
|
|
let mut f = File::create(path).await?; |
|
|
|
|
|
|
|
f.write_all(file).await?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.mediaid_file.insert(key, vec![])?; |
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Downloads a file.
|
|
|
|
/// Downloads a file.
|
|
|
|
pub fn get(&self, mxc: &str) -> Result<Option<FileMeta>> { |
|
|
|
pub async fn get(&self, globals: &Globals, mxc: &str) -> Result<Option<FileMeta>> { |
|
|
|
let mut prefix = mxc.as_bytes().to_vec(); |
|
|
|
let mut prefix = mxc.as_bytes().to_vec(); |
|
|
|
prefix.push(0xff); |
|
|
|
prefix.push(0xff); |
|
|
|
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()); // Width = 0 if it's not a thumbnail
|
|
|
|
@ -80,7 +93,10 @@ impl Media { |
|
|
|
prefix.push(0xff); |
|
|
|
prefix.push(0xff); |
|
|
|
|
|
|
|
|
|
|
|
if let Some(r) = self.mediaid_file.scan_prefix(&prefix).next() { |
|
|
|
if let Some(r) = self.mediaid_file.scan_prefix(&prefix).next() { |
|
|
|
let (key, file) = r?; |
|
|
|
let (key, _file) = r?; |
|
|
|
|
|
|
|
let path = globals.get_media_file(&key.to_vec()); |
|
|
|
|
|
|
|
let mut file = vec![]; |
|
|
|
|
|
|
|
File::open(path).await?.read_to_end(&mut file).await?; |
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff); |
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff); |
|
|
|
|
|
|
|
|
|
|
|
let content_type = parts |
|
|
|
let content_type = parts |
|
|
|
@ -107,7 +123,7 @@ impl Media { |
|
|
|
Ok(Some(FileMeta { |
|
|
|
Ok(Some(FileMeta { |
|
|
|
filename, |
|
|
|
filename, |
|
|
|
content_type, |
|
|
|
content_type, |
|
|
|
file: file.to_vec(), |
|
|
|
file, |
|
|
|
})) |
|
|
|
})) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Ok(None) |
|
|
|
Ok(None) |
|
|
|
@ -137,7 +153,7 @@ impl Media { |
|
|
|
/// - Server creates the thumbnail and sends it to the user
|
|
|
|
/// - Server creates the thumbnail and sends it to the user
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// For width,height <= 96 the server uses another thumbnailing algorithm which crops the image afterwards.
|
|
|
|
/// For width,height <= 96 the server uses another thumbnailing algorithm which crops the image afterwards.
|
|
|
|
pub fn get_thumbnail(&self, mxc: String, width: u32, height: u32) -> Result<Option<FileMeta>> { |
|
|
|
pub async fn get_thumbnail(&self, mxc: String, globals: &Globals, width: u32, height: u32) -> Result<Option<FileMeta>> { |
|
|
|
let (width, height, crop) = self |
|
|
|
let (width, height, crop) = self |
|
|
|
.thumbnail_properties(width, height) |
|
|
|
.thumbnail_properties(width, height) |
|
|
|
.unwrap_or((0, 0, false)); // 0, 0 because that's the original file
|
|
|
|
.unwrap_or((0, 0, false)); // 0, 0 because that's the original file
|
|
|
|
@ -157,7 +173,10 @@ impl Media { |
|
|
|
|
|
|
|
|
|
|
|
if let Some(r) = self.mediaid_file.scan_prefix(&thumbnail_prefix).next() { |
|
|
|
if let Some(r) = self.mediaid_file.scan_prefix(&thumbnail_prefix).next() { |
|
|
|
// Using saved thumbnail
|
|
|
|
// Using saved thumbnail
|
|
|
|
let (key, file) = r?; |
|
|
|
let (key, _file) = r?; |
|
|
|
|
|
|
|
let path = globals.get_media_file(&key.to_vec()); |
|
|
|
|
|
|
|
let mut file = vec![]; |
|
|
|
|
|
|
|
File::open(path).await?.read_to_end(&mut file).await?; |
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff); |
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff); |
|
|
|
|
|
|
|
|
|
|
|
let content_type = parts |
|
|
|
let content_type = parts |
|
|
|
@ -190,7 +209,11 @@ impl Media { |
|
|
|
} else if let Some(r) = self.mediaid_file.scan_prefix(&original_prefix).next() { |
|
|
|
} else if let Some(r) = self.mediaid_file.scan_prefix(&original_prefix).next() { |
|
|
|
// Generate a thumbnail
|
|
|
|
// Generate a thumbnail
|
|
|
|
|
|
|
|
|
|
|
|
let (key, file) = r?; |
|
|
|
let (key, _file) = r?; |
|
|
|
|
|
|
|
let path = globals.get_media_file(&key.to_vec()); |
|
|
|
|
|
|
|
let mut file = vec![]; |
|
|
|
|
|
|
|
File::open(path).await?.read_to_end(&mut file).await?; |
|
|
|
|
|
|
|
|
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff); |
|
|
|
let mut parts = key.rsplit(|&b| b == 0xff); |
|
|
|
|
|
|
|
|
|
|
|
let content_type = parts |
|
|
|
let content_type = parts |
|
|
|
@ -283,19 +306,24 @@ impl Media { |
|
|
|
widthheight, |
|
|
|
widthheight, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
self.mediaid_file.insert(thumbnail_key, &*thumbnail_bytes)?; |
|
|
|
let path = globals.get_media_file(&thumbnail_key); |
|
|
|
|
|
|
|
fs::create_dir_all(path.parent().unwrap()).await?; |
|
|
|
|
|
|
|
let mut f = File::create(path).await?; |
|
|
|
|
|
|
|
f.write_all(&thumbnail_bytes).await?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.mediaid_file.insert(thumbnail_key, vec![])?; |
|
|
|
|
|
|
|
|
|
|
|
Ok(Some(FileMeta { |
|
|
|
Ok(Some(FileMeta { |
|
|
|
filename, |
|
|
|
filename, |
|
|
|
content_type, |
|
|
|
content_type, |
|
|
|
file: thumbnail_bytes.to_vec(), |
|
|
|
file: thumbnail_bytes.to_vec() |
|
|
|
})) |
|
|
|
})) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// Couldn't parse file to generate thumbnail, send original
|
|
|
|
// Couldn't parse file to generate thumbnail, send original
|
|
|
|
Ok(Some(FileMeta { |
|
|
|
Ok(Some(FileMeta { |
|
|
|
filename, |
|
|
|
filename, |
|
|
|
content_type, |
|
|
|
content_type, |
|
|
|
file: file.to_vec(), |
|
|
|
file: file.to_vec() |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|