Skip to content

Commit

Permalink
fix: bug on create db file
Browse files Browse the repository at this point in the history
  • Loading branch information
danloh committed Nov 30, 2022
1 parent 25c3bc4 commit befdbc7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
21 changes: 12 additions & 9 deletions src-tauri/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use serde::{Deserialize, Serialize};
use std::path;
use tauri::api::path::local_data_dir;
use chrono::offset::Local;
use crate::storage::do_log;
use crate::storage::{do_log, create_mdsilo_dir};
use crate::models::{Channel, NewChannel, Article, NewArticle};
use crate::schema;

pub fn establish_connection() -> SqliteConnection {
let db_path = path::Path::new(&local_data_dir().unwrap())
.join("mdsilo")
.join("mdsilo.db");
let data_path = create_mdsilo_dir()
.expect("Error on creating data dir");

let db_path = data_path.join("mdsilo.db");

let database_url = db_path
.to_str()
.clone()
.expect(&format!("Error convert path {:?} to url", db_path));
.expect("Error on converting db path to url");

SqliteConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))

.map_err(|e| do_log(
"Error".to_string(),
format!("Error on connecting to db: {:?}", e),
format!("{}", Local::now().format("%m/%d/%Y %H:%M:%S"))
))
.expect("Error on connecting to database")
}

pub fn get_channels() -> Vec<Channel> {
Expand Down
39 changes: 29 additions & 10 deletions src-tauri/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str;
use chrono::offset::Local;
use tauri::api::path::local_data_dir;
Expand All @@ -12,22 +12,41 @@ pub struct StorageData {
pub status: bool,
}

#[tauri::command]
pub fn set_data(key: String, value: Value) -> bool {
pub fn create_mdsilo_dir() -> Option<PathBuf> {
// println!("local data dir: {:?}", local_data_dir());

// Linux: $HOME/.local/share/mdsilo
// macOS: $HOME/Library/Application
// Windows: $HOME/AppData/Local/mdsilo
let storage_dir = match local_data_dir() {
Some(dir) => Path::new(&dir).join("mdsilo"),
None => {
return false;
if let Some(local_dir) = local_data_dir() {
let data_path = Path::new(&local_dir).join("mdsilo");
// make sure the mdsilo dir is created
match fs::create_dir_all(data_path.clone()) {
Ok(_) => Some(data_path),
Err(e) => {
do_log(
"Error".to_string(),
format!("Error on creating local_data_dir: {:?}", e),
format!("{}", Local::now().format("%m/%d/%Y %H:%M:%S"))
);
None
},
}
};
} else {
do_log(
"Error".to_string(),
format!("Error on getting local_data_dir"),
format!("{}", Local::now().format("%m/%d/%Y %H:%M:%S"))
);
None
}
}

if fs::create_dir_all(storage_dir.clone()).is_err() {
return false;
#[tauri::command]
pub fn set_data(key: String, value: Value) -> bool {
let storage_dir = match create_mdsilo_dir() {
Some(dir) => dir,
None => { return false; }
};

let vec_value = match serde_json::to_vec(&value) {
Expand Down

0 comments on commit befdbc7

Please sign in to comment.