Browse Source

create media folder in init

merge-requests/95/head
hamidreza kalbasi 5 years ago
parent
commit
93d8205f31
  1. 9
      src/database/globals.rs
  2. 23
      src/database/media.rs

9
src/database/globals.rs

@ -7,6 +7,7 @@ use ruma::{
use rustls::{ServerCertVerifier, WebPKIVerifier}; use rustls::{ServerCertVerifier, WebPKIVerifier};
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
fs,
path::PathBuf, path::PathBuf,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
time::{Duration, Instant}, time::{Duration, Instant},
@ -131,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),
@ -146,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.

23
src/database/media.rs

@ -3,11 +3,7 @@ use image::{imageops::FilterType, GenericImageView};
use crate::{utils, Error, Result}; use crate::{utils, Error, Result};
use std::mem; use std::mem;
use tokio::{ use tokio::{fs::File, io::AsyncReadExt, io::AsyncWriteExt};
fs::{self, File},
io::AsyncReadExt,
io::AsyncWriteExt,
};
pub struct FileMeta { pub struct FileMeta {
pub content_disposition: Option<String>, pub content_disposition: Option<String>,
@ -50,7 +46,6 @@ impl Media {
); );
let path = globals.get_media_file(&key); let path = globals.get_media_file(&key);
fs::create_dir_all(path.parent().unwrap()).await?;
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
f.write_all(file).await?; f.write_all(file).await?;
@ -89,7 +84,6 @@ impl Media {
); );
let path = globals.get_media_file(&key); let path = globals.get_media_file(&key);
fs::create_dir_all(path.parent().unwrap()).await?;
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
f.write_all(file).await?; f.write_all(file).await?;
@ -195,7 +189,12 @@ 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).keys().next() { if let Some(r) = self
.mediaid_file
.scan_prefix(&thumbnail_prefix)
.keys()
.next()
{
// Using saved thumbnail // Using saved thumbnail
let key = r?; let key = r?;
let path = globals.get_media_file(&key); let path = globals.get_media_file(&key);
@ -231,7 +230,12 @@ 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).keys().next() { } else if let Some(r) = self
.mediaid_file
.scan_prefix(&original_prefix)
.keys()
.next()
{
// Generate a thumbnail // Generate a thumbnail
let key = r?; let key = r?;
@ -336,7 +340,6 @@ impl Media {
); );
let path = globals.get_media_file(&thumbnail_key); let path = globals.get_media_file(&thumbnail_key);
fs::create_dir_all(path.parent().unwrap()).await?;
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
f.write_all(&thumbnail_bytes).await?; f.write_all(&thumbnail_bytes).await?;

Loading…
Cancel
Save