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::{ @@ -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 { @@ -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 { @@ -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.

23
src/database/media.rs

@ -3,11 +3,7 @@ use image::{imageops::FilterType, GenericImageView}; @@ -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<String>,
@ -50,7 +46,6 @@ impl Media { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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?;

Loading…
Cancel
Save