Browse Source

Merge branch 'master' into 'master'

put media in filesystem

See merge request famedly/conduit!95
merge-requests/95/merge
hamidreza kalbasi 5 years ago
parent
commit
0194aa239e
  1. 32
      src/client_server/media.rs
  2. 24
      src/database.rs
  3. 25
      src/database/globals.rs
  4. 71
      src/database/media.rs
  5. 5
      src/error.rs

32
src/client_server/media.rs

@ -36,8 +36,10 @@ pub async fn create_content_route(
db.globals.server_name(), db.globals.server_name(),
utils::random_string(MXC_LENGTH) utils::random_string(MXC_LENGTH)
); );
db.media.create( db.media
.create(
mxc.clone(), mxc.clone(),
&db.globals,
&body &body
.filename .filename
.as_ref() .as_ref()
@ -45,7 +47,8 @@ pub async fn create_content_route(
.as_deref(), .as_deref(),
&body.content_type.as_deref(), &body.content_type.as_deref(),
&body.file, &body.file,
)?; )
.await?;
db.flush().await?; db.flush().await?;
@ -71,7 +74,7 @@ pub async fn get_content_route(
content_disposition, content_disposition,
content_type, content_type,
file, file,
}) = db.media.get(&mxc)? }) = db.media.get(&db.globals, &mxc).await?
{ {
Ok(get_content::Response { Ok(get_content::Response {
file, file,
@ -93,12 +96,15 @@ pub async fn get_content_route(
) )
.await?; .await?;
db.media.create( db.media
.create(
mxc, mxc,
&db.globals,
&get_content_response.content_disposition.as_deref(), &get_content_response.content_disposition.as_deref(),
&get_content_response.content_type.as_deref(), &get_content_response.content_type.as_deref(),
&get_content_response.file, &get_content_response.file,
)?; )
.await?;
Ok(get_content_response.into()) Ok(get_content_response.into())
} else { } else {
@ -119,15 +125,20 @@ pub async fn get_content_thumbnail_route(
if let Some(FileMeta { if let Some(FileMeta {
content_type, file, .. content_type, file, ..
}) = db.media.get_thumbnail( }) = db
.media
.get_thumbnail(
mxc.clone(), mxc.clone(),
&db.globals,
body.width body.width
.try_into() .try_into()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
body.height body.height
.try_into() .try_into()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
)? { )
.await?
{
Ok(get_content_thumbnail::Response { file, content_type }.into()) Ok(get_content_thumbnail::Response { file, content_type }.into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote { } else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let get_thumbnail_response = db let get_thumbnail_response = db
@ -146,14 +157,17 @@ pub async fn get_content_thumbnail_route(
) )
.await?; .await?;
db.media.upload_thumbnail( db.media
.upload_thumbnail(
mxc, mxc,
&db.globals,
&None, &None,
&get_thumbnail_response.content_type, &get_thumbnail_response.content_type,
body.width.try_into().expect("all UInts are valid u32s"), body.width.try_into().expect("all UInts are valid u32s"),
body.height.try_into().expect("all UInts are valid u32s"), body.height.try_into().expect("all UInts are valid u32s"),
&get_thumbnail_response.file, &get_thumbnail_response.file,
)?; )
.await?;
Ok(get_thumbnail_response.into()) Ok(get_thumbnail_response.into())
} else { } else {

24
src/database.rs

@ -20,7 +20,8 @@ use ruma::{DeviceId, ServerName, UserId};
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::remove_dir_all, fs::{self, remove_dir_all},
io::Write,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use tokio::sync::Semaphore; use tokio::sync::Semaphore;
@ -253,9 +254,11 @@ impl Database {
let password = utils::string_from_bytes(&password); let password = utils::string_from_bytes(&password);
if password.map_or(false, |password| { let password_not_exists = password.map_or(false, |password| {
argon2::verify_encoded(&password, b"").unwrap_or(false) argon2::verify_encoded(&password, b"").unwrap_or(false)
}) { });
if password_not_exists {
db.users.userid_password.insert(userid, b"")?; db.users.userid_password.insert(userid, b"")?;
} }
} }
@ -265,6 +268,21 @@ impl Database {
info!("Migration: 1 -> 2 finished"); info!("Migration: 1 -> 2 finished");
} }
if db.globals.database_version()? < 3 {
// Move media to filesystem
for r in db.media.mediaid_file.iter() {
let (key, content) = r?;
let path = db.globals.get_media_file(&key);
let mut file = fs::File::create(path)?;
file.write_all(&content)?;
db.media.mediaid_file.remove(&key)?;
db.media.mediaid_file.insert(&key, vec![])?;
}
db.globals.bump_database_version(3)?;
info!("Migration: 2 -> 3 finished");
}
// This data is probably outdated // This data is probably outdated
db.rooms.edus.presenceid_presence.clear()?; db.rooms.edus.presenceid_presence.clear()?;

25
src/database/globals.rs

@ -7,6 +7,8 @@ use ruma::{
use rustls::{ServerCertVerifier, WebPKIVerifier}; use rustls::{ServerCertVerifier, WebPKIVerifier};
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
fs,
path::PathBuf,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -130,7 +132,7 @@ impl Globals {
.as_ref() .as_ref()
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static()); .map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
Ok(Self { let s = Self {
globals, globals,
config, config,
keypair: Arc::new(keypair), keypair: Arc::new(keypair),
@ -145,7 +147,11 @@ impl Globals {
bad_event_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())), bad_event_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())),
bad_signature_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())), bad_signature_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())),
servername_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())), servername_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())),
}) };
fs::create_dir_all(s.get_media_folder())?;
Ok(s)
} }
/// Returns this server's keypair. /// Returns this server's keypair.
@ -264,4 +270,19 @@ impl Globals {
self.globals.insert("version", &new_version.to_be_bytes())?; self.globals.insert("version", &new_version.to_be_bytes())?;
Ok(()) Ok(())
} }
pub fn get_media_folder(&self) -> PathBuf {
let mut r = PathBuf::new();
r.push(self.config.database_path.clone());
r.push("media");
r
}
pub fn get_media_file(&self, key: &[u8]) -> PathBuf {
let mut r = PathBuf::new();
r.push(self.config.database_path.clone());
r.push("media");
r.push(base64::encode_config(key, base64::URL_SAFE_NO_PAD));
r
}
} }

71
src/database/media.rs

@ -1,7 +1,9 @@
use crate::database::globals::Globals;
use image::{imageops::FilterType, GenericImageView}; use image::{imageops::FilterType, GenericImageView};
use crate::{utils, Error, Result}; use crate::{utils, Error, Result};
use std::mem; use std::mem;
use tokio::{fs::File, io::AsyncReadExt, io::AsyncWriteExt};
pub struct FileMeta { pub struct FileMeta {
pub content_disposition: Option<String>, pub content_disposition: 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,
content_disposition: &Option<&str>, content_disposition: &Option<&str>,
content_type: &Option<&str>, content_type: &Option<&str>,
file: &[u8], file: &[u8],
@ -42,15 +45,19 @@ impl Media {
.unwrap_or_default(), .unwrap_or_default(),
); );
self.mediaid_file.insert(key, file)?; let path = globals.get_media_file(&key);
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,
content_disposition: &Option<String>, content_disposition: &Option<String>,
content_type: &Option<String>, content_type: &Option<String>,
width: u32, width: u32,
@ -76,21 +83,28 @@ impl Media {
.unwrap_or_default(), .unwrap_or_default(),
); );
self.mediaid_file.insert(key, file)?; let path = globals.get_media_file(&key);
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
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 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); prefix.push(0xff);
if let Some(r) = self.mediaid_file.scan_prefix(&prefix).next() { if let Some(r) = self.mediaid_file.scan_prefix(&prefix).keys().next() {
let (key, file) = r?; let key = r?;
let path = globals.get_media_file(&key);
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
@ -121,7 +135,7 @@ impl Media {
Ok(Some(FileMeta { Ok(Some(FileMeta {
content_disposition, content_disposition,
content_type, content_type,
file: file.to_vec(), file,
})) }))
} else { } else {
Ok(None) Ok(None)
@ -151,7 +165,13 @@ 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
@ -169,9 +189,17 @@ impl Media {
original_prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 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); original_prefix.push(0xff);
if let Some(r) = self.mediaid_file.scan_prefix(&thumbnail_prefix).next() { if let Some(r) = self
.mediaid_file
.scan_prefix(&thumbnail_prefix)
.keys()
.next()
{
// Using saved thumbnail // Using saved thumbnail
let (key, file) = r?; let key = r?;
let path = globals.get_media_file(&key);
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
@ -202,10 +230,19 @@ impl Media {
content_type, content_type,
file: file.to_vec(), file: file.to_vec(),
})) }))
} 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)
.keys()
.next()
{
// Generate a thumbnail // Generate a thumbnail
let (key, file) = r?; let key = r?;
let path = globals.get_media_file(&key);
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
@ -302,7 +339,11 @@ impl Media {
widthheight, widthheight,
); );
self.mediaid_file.insert(thumbnail_key, &*thumbnail_bytes)?; let path = globals.get_media_file(&thumbnail_key);
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 {
content_disposition, content_disposition,

5
src/error.rs

@ -40,6 +40,11 @@ pub enum Error {
}, },
#[error("{0}")] #[error("{0}")]
FederationError(Box<ServerName>, RumaError), FederationError(Box<ServerName>, RumaError),
#[error("Could not do this io: {source}")]
IoError {
#[from]
source: std::io::Error,
},
#[error("{0}")] #[error("{0}")]
BadServerResponse(&'static str), BadServerResponse(&'static str),
#[error("{0}")] #[error("{0}")]

Loading…
Cancel
Save