add migrations

This commit is contained in:
hamidreza kalbasi 2021-06-08 20:53:24 +04:30 committed by Timo Kösters
parent affa124864
commit 2385bd1cfd
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
2 changed files with 25 additions and 10 deletions

View File

@ -20,11 +20,7 @@ use log::error;
use rocket::futures::{channel::mpsc, stream::FuturesUnordered, StreamExt};
use ruma::{DeviceId, ServerName, UserId};
use serde::Deserialize;
use std::{
collections::HashMap,
fs::remove_dir_all,
sync::{Arc, RwLock},
};
use std::{collections::HashMap, fs::{self, remove_dir_all}, io::Write, sync::{Arc, RwLock}};
use tokio::sync::Semaphore;
#[derive(Clone, Debug, Deserialize)]
@ -253,9 +249,11 @@ impl Database {
for (userid, password) in db.users.userid_password.iter() {
let password = utils::string_from_bytes(&password);
if password.map_or(false, |password| {
let empty_hashed_password = password.map_or(false, |password| {
argon2::verify_encoded(&password, b"").unwrap_or(false)
}) {
});
if empty_hashed_password {
db.users.userid_password.insert(&userid, b"")?;
}
}
@ -265,6 +263,23 @@ impl Database {
println!("Migration: 1 -> 2 finished");
}
if db.globals.database_version()? < 3 {
// Move media to filesystem
for (key, content) in db.media.mediaid_file.iter() {
if content.len() == 0 {
continue;
}
let path = db.globals.get_media_file(&key);
let mut file = fs::File::create(path)?;
file.write_all(&content)?;
db.media.mediaid_file.insert(&key, &[])?;
}
db.globals.bump_database_version(3)?;
println!("Migration: 2 -> 3 finished");
}
// This data is probably outdated
db.rooms.edus.presenceid_presence.clear()?;

View File

@ -103,7 +103,7 @@ impl Media {
let mut iter = self.mediaid_file.scan_prefix(prefix);
if let Some((key, _)) = iter.next() {
let path = globals.get_media_file(&key);
let mut file = vec![];
let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?;
let mut parts = key.rsplit(|&b| b == 0xff);
@ -192,7 +192,7 @@ impl Media {
if let Some((key, _)) = self.mediaid_file.scan_prefix(thumbnail_prefix).next() {
// Using saved thumbnail
let path = globals.get_media_file(&key);
let mut file = vec![];
let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?;
let mut parts = key.rsplit(|&b| b == 0xff);
@ -227,7 +227,7 @@ impl Media {
} else if let Some((key, _)) = self.mediaid_file.scan_prefix(original_prefix).next() {
// Generate a thumbnail
let path = globals.get_media_file(&key);
let mut file = vec![];
let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?;
let mut parts = key.rsplit(|&b| b == 0xff);