Minor refactoring

This commit is contained in:
Niels Andriesse 2021-03-23 14:25:52 +11:00
parent 2b662dfcc7
commit ddba1f6396
3 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,7 @@
use std::convert::TryInto;
use std::fs;
use std::io::prelude::*;
use std::path::Path;
use chrono;
use serde::{Deserialize, Serialize};
@ -55,7 +56,9 @@ pub async fn store_file(base64_encoded_bytes: &str, pool: &storage:: DatabaseCon
};
// Write to file
let mut pos = 0;
let mut buffer = match fs::File::create(format!("files/{}", &id)) {
let raw_path = format!("files/{}", &id);
let path = Path::new(&raw_path);
let mut buffer = match fs::File::create(path) {
Ok(buffer) => buffer,
Err(e) => {
println!("Couldn't store file due to error: {}.", e);
@ -87,7 +90,9 @@ pub async fn get_file(id: &str) -> Result<GenericStringResponse, Rejection> { //
}
};
// Try to read the file
let bytes = match fs::read(format!("files/{}", id)) {
let raw_path = format!("files/{}", id);
let path = Path::new(&raw_path);
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(e) => {
println!("Couldn't read file due to error: {}.", e);

View File

@ -87,7 +87,7 @@ pub fn pool_by_room_name(room: &str) -> DatabaseConnectionPool {
if let Some(pool) = pools.get(room) {
return pool.clone();
} else {
let raw_path = format!("./rooms/{}.db", room);
let raw_path = format!("rooms/{}.db", room);
let path = Path::new(&raw_path);
let db_manager = r2d2_sqlite::SqliteConnectionManager::file(path);
let pool = r2d2::Pool::new(db_manager).unwrap();

View File

@ -1,4 +1,5 @@
use std::fs;
use std::path::Path;
use rand::{thread_rng, Rng};
use rusqlite::params;
@ -28,7 +29,9 @@ fn set_up_test_room() {
}
let test_room = "test_room";
storage::create_database_if_needed(test_room);
fs::read("rooms/test_room.db").unwrap(); // Fail if this doesn't exist
let raw_path = format!("rooms/{}.db", test_room);
let path = Path::new(&raw_path);
fs::read(path).unwrap(); // Fail if this doesn't exist
let pool: &storage::DatabaseConnectionPool = &storage::MAIN_POOL;
let conn = pool.get().unwrap();
let stmt = format!("REPLACE INTO {} (id, name) VALUES (?1, ?2)", storage::MAIN_TABLE);