From 93d8205f315e8b24826edfd17c60e0a37486465d Mon Sep 17 00:00:00 2001 From: hamidreza kalbasi Date: Tue, 8 Jun 2021 17:50:06 +0430 Subject: [PATCH] create media folder in init --- src/database/globals.rs | 9 +++++++-- src/database/media.rs | 23 +++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/database/globals.rs b/src/database/globals.rs index e382f0c..e17e563 100644 --- a/src/database/globals.rs +++ b/src/database/globals.rs @@ -7,6 +7,7 @@ use ruma::{ use rustls::{ServerCertVerifier, WebPKIVerifier}; use std::{ collections::{BTreeMap, HashMap}, + fs, path::PathBuf, sync::{Arc, RwLock}, time::{Duration, Instant}, @@ -131,7 +132,7 @@ impl Globals { .as_ref() .map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static()); - Ok(Self { + let s = Self { globals, config, keypair: Arc::new(keypair), @@ -146,7 +147,11 @@ impl Globals { bad_event_ratelimiter: Arc::new(RwLock::new(BTreeMap::new())), bad_signature_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. diff --git a/src/database/media.rs b/src/database/media.rs index f2caa11..34b1fab 100644 --- a/src/database/media.rs +++ b/src/database/media.rs @@ -3,11 +3,7 @@ use image::{imageops::FilterType, GenericImageView}; use crate::{utils, Error, Result}; use std::mem; -use tokio::{ - fs::{self, File}, - io::AsyncReadExt, - io::AsyncWriteExt, -}; +use tokio::{fs::File, io::AsyncReadExt, io::AsyncWriteExt}; pub struct FileMeta { pub content_disposition: Option, @@ -50,7 +46,6 @@ impl Media { ); 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?; @@ -89,7 +84,6 @@ impl Media { ); 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?; @@ -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.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 let key = r?; let path = globals.get_media_file(&key); @@ -231,7 +230,12 @@ impl Media { content_type, 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 let key = r?; @@ -336,7 +340,6 @@ impl Media { ); 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?;