From 2c0ec4cdd3aea450de58b0592db43e0a94c360fd Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 6 Jul 2021 19:53:37 +0100 Subject: [PATCH] Use `write_file` util everywhere Removed creating icon cache dir as it's done on startup --- src/api/icons.rs | 13 ++++--------- src/config.rs | 6 ++---- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index 36eb286c..d5ad0b1f 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - fs::{create_dir_all, remove_file, symlink_metadata, File}, + fs::{remove_file, symlink_metadata, File}, io::prelude::*, net::{IpAddr, ToSocketAddrs}, sync::{Arc, RwLock}, @@ -14,7 +14,7 @@ use rocket::{http::ContentType, response::Content, Route}; use crate::{ error::Error, - util::{get_reqwest_client_builder, Cached}, + util::{get_reqwest_client_builder, write_file, Cached}, CONFIG, }; @@ -705,13 +705,8 @@ fn download_icon(domain: &str) -> Result<(Vec, Option<&str>), Error> { } fn save_icon(path: &str, icon: &[u8]) { - match File::create(path) { - Ok(mut f) => { - f.write_all(icon).expect("Error writing icon file"); - } - Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { - create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache"); - } + match write_file(path, icon) { + Ok(_) => (), Err(e) => { warn!("Icon save error: {:?}", e); } diff --git a/src/config.rs b/src/config.rs index 6b4fce59..b1f86449 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,7 @@ use reqwest::Url; use crate::{ db::DbConnType, error::Error, - util::{get_env, get_env_bool}, + util::{get_env, get_env_bool, write_file}, }; static CONFIG_FILE: Lazy = Lazy::new(|| { @@ -691,9 +691,7 @@ impl Config { } //Save to file - use std::{fs::File, io::Write}; - let mut file = File::create(&*CONFIG_FILE)?; - file.write_all(config_str.as_bytes())?; + write_file(&CONFIG_FILE, config_str.as_bytes())?; Ok(()) }