Single compute_hash fn + cargo fmt
This commit is contained in:
parent
d3ab3936de
commit
3262f69bb0
|
@ -170,8 +170,8 @@ impl Config {
|
||||||
/// Parses a config file from the given path
|
/// Parses a config file from the given path
|
||||||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let content = read_file(path)
|
let content =
|
||||||
.map_err(|e| errors::Error::chain("Failed to load config", e))?;
|
read_file(path).map_err(|e| errors::Error::chain("Failed to load config", e))?;
|
||||||
Config::parse(&content)
|
Config::parse(&content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use std::{collections::hash_map::DefaultHasher, io::Write};
|
|
||||||
use std::collections::hash_map::Entry as HEntry;
|
use std::collections::hash_map::Entry as HEntry;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::{collections::hash_map::DefaultHasher, io::Write};
|
||||||
|
|
||||||
use image::{EncodableLayout, imageops::FilterType};
|
use image::{imageops::FilterType, EncodableLayout};
|
||||||
use image::{GenericImageView, ImageOutputFormat};
|
use image::{GenericImageView, ImageOutputFormat};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
@ -189,7 +189,7 @@ impl Format {
|
||||||
match *self {
|
match *self {
|
||||||
Png => "png",
|
Png => "png",
|
||||||
Jpeg(_) => "jpg",
|
Jpeg(_) => "jpg",
|
||||||
WebP(_) => "webp"
|
WebP(_) => "webp",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ impl Hash for Format {
|
||||||
Png => 0,
|
Png => 0,
|
||||||
Jpeg(q) => q,
|
Jpeg(q) => q,
|
||||||
WebP(None) => 0,
|
WebP(None) => 0,
|
||||||
WebP(Some(q)) => q
|
WebP(Some(q)) => q,
|
||||||
};
|
};
|
||||||
|
|
||||||
hasher.write_u8(q);
|
hasher.write_u8(q);
|
||||||
|
@ -316,12 +316,8 @@ impl ImageOp {
|
||||||
Format::WebP(q) => {
|
Format::WebP(q) => {
|
||||||
let encoder = webp::Encoder::from_image(&img);
|
let encoder = webp::Encoder::from_image(&img);
|
||||||
let memory = match q {
|
let memory = match q {
|
||||||
Some(q) => {
|
Some(q) => encoder.encode(q as f32 / 100.),
|
||||||
encoder.encode(q as f32 / 100.)
|
None => encoder.encode_lossless(),
|
||||||
}
|
|
||||||
None => {
|
|
||||||
encoder.encode_lossless()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let mut bytes = memory.as_bytes();
|
let mut bytes = memory.as_bytes();
|
||||||
f.write_all(&mut bytes)?;
|
f.write_all(&mut bytes)?;
|
||||||
|
|
|
@ -77,7 +77,8 @@ impl Site {
|
||||||
|
|
||||||
if let Some(theme) = config.theme.clone() {
|
if let Some(theme) = config.theme.clone() {
|
||||||
// Grab data from the extra section of the theme
|
// Grab data from the extra section of the theme
|
||||||
config.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"), &theme)?;
|
config
|
||||||
|
.merge_with_theme(&path.join("themes").join(&theme).join("theme.toml"), &theme)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tera = load_tera(path, &config)?;
|
let tera = load_tera(path, &config)?;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use std::path::PathBuf;
|
||||||
use base64::{decode, encode};
|
use base64::{decode, encode};
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use rendering::{render_content, RenderContext};
|
use rendering::{render_content, RenderContext};
|
||||||
use tera::{Filter as TeraFilter, Result as TeraResult, Tera, Value, to_value, try_get_value};
|
use tera::{to_value, try_get_value, Filter as TeraFilter, Result as TeraResult, Tera, Value};
|
||||||
|
|
||||||
use crate::load_tera;
|
use crate::load_tera;
|
||||||
|
|
||||||
|
@ -18,9 +18,12 @@ pub struct MarkdownFilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MarkdownFilter {
|
impl MarkdownFilter {
|
||||||
pub fn new(path: PathBuf, config: Config, permalinks: HashMap<String, String>) -> TeraResult<Self> {
|
pub fn new(
|
||||||
let tera = load_tera(&path, &config)
|
path: PathBuf,
|
||||||
.map_err(|err| tera::Error::msg(err))?;
|
config: Config,
|
||||||
|
permalinks: HashMap<String, String>,
|
||||||
|
) -> TeraResult<Self> {
|
||||||
|
let tera = load_tera(&path, &config).map_err(|err| tera::Error::msg(err))?;
|
||||||
Ok(Self { config, permalinks, tera })
|
Ok(Self { config, permalinks, tera })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::sync::{Arc, Mutex, RwLock};
|
||||||
use std::{fs, io, result};
|
use std::{fs, io, result};
|
||||||
|
|
||||||
use base64::encode as encode_b64;
|
use base64::encode as encode_b64;
|
||||||
use sha2::{Digest, Sha256, Sha384, Sha512};
|
use sha2::{digest, Sha256, Sha384, Sha512};
|
||||||
use svg_metadata as svg;
|
use svg_metadata as svg;
|
||||||
use tera::{from_value, to_value, Error, Function as TeraFn, Result, Value};
|
use tera::{from_value, to_value, Error, Function as TeraFn, Result, Value};
|
||||||
|
|
||||||
|
@ -90,40 +90,22 @@ fn open_file(search_paths: &[PathBuf], url: &str) -> result::Result<fs::File, io
|
||||||
Err(io::Error::from(io::ErrorKind::NotFound))
|
Err(io::Error::from(io::ErrorKind::NotFound))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_file_sha256(mut file: fs::File) -> result::Result<String, io::Error> {
|
fn compute_file_hash<D: digest::Digest>(
|
||||||
let mut hasher = Sha256::new();
|
mut file: fs::File,
|
||||||
|
base64: bool,
|
||||||
|
) -> result::Result<String, io::Error>
|
||||||
|
where
|
||||||
|
digest::Output<D>: core::fmt::LowerHex,
|
||||||
|
D: std::io::Write,
|
||||||
|
{
|
||||||
|
let mut hasher = D::new();
|
||||||
io::copy(&mut file, &mut hasher)?;
|
io::copy(&mut file, &mut hasher)?;
|
||||||
Ok(format!("{:x}", hasher.finalize()))
|
let val = format!("{:x}", hasher.finalize());
|
||||||
}
|
if base64 {
|
||||||
|
Ok(encode_b64(val))
|
||||||
fn compute_file_sha256_base64(mut file: fs::File) -> result::Result<String, io::Error> {
|
} else {
|
||||||
let mut hasher = Sha256::new();
|
Ok(val)
|
||||||
io::copy(&mut file, &mut hasher)?;
|
}
|
||||||
Ok(format!("{}", encode_b64(hasher.finalize())))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compute_file_sha384(mut file: fs::File) -> result::Result<String, io::Error> {
|
|
||||||
let mut hasher = Sha384::new();
|
|
||||||
io::copy(&mut file, &mut hasher)?;
|
|
||||||
Ok(format!("{:x}", hasher.finalize()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compute_file_sha384_base64(mut file: fs::File) -> result::Result<String, io::Error> {
|
|
||||||
let mut hasher = Sha384::new();
|
|
||||||
io::copy(&mut file, &mut hasher)?;
|
|
||||||
Ok(format!("{}", encode_b64(hasher.finalize())))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compute_file_sha512(mut file: fs::File) -> result::Result<String, io::Error> {
|
|
||||||
let mut hasher = Sha512::new();
|
|
||||||
io::copy(&mut file, &mut hasher)?;
|
|
||||||
Ok(format!("{:x}", hasher.finalize()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compute_file_sha512_base64(mut file: fs::File) -> result::Result<String, io::Error> {
|
|
||||||
let mut hasher = Sha512::new();
|
|
||||||
io::copy(&mut file, &mut hasher)?;
|
|
||||||
Ok(format!("{}", encode_b64(hasher.finalize())))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn file_not_found_err(search_paths: &[PathBuf], url: &str) -> Result<Value> {
|
fn file_not_found_err(search_paths: &[PathBuf], url: &str) -> Result<Value> {
|
||||||
|
@ -174,7 +156,9 @@ impl TeraFn for GetUrl {
|
||||||
}
|
}
|
||||||
|
|
||||||
if cachebust {
|
if cachebust {
|
||||||
match open_file(&self.search_paths, &path).and_then(compute_file_sha256) {
|
match open_file(&self.search_paths, &path)
|
||||||
|
.and_then(|f| compute_file_hash::<Sha256>(f, false))
|
||||||
|
{
|
||||||
Ok(hash) => {
|
Ok(hash) => {
|
||||||
permalink = format!("{}?h={}", permalink, hash);
|
permalink = format!("{}?h={}", permalink, hash);
|
||||||
}
|
}
|
||||||
|
@ -219,17 +203,19 @@ impl TeraFn for GetFileHash {
|
||||||
)
|
)
|
||||||
.unwrap_or(DEFAULT_BASE64);
|
.unwrap_or(DEFAULT_BASE64);
|
||||||
|
|
||||||
let compute_hash_fn = match (sha_type, base64) {
|
let f = match open_file(&self.search_paths, &path) {
|
||||||
(256, true) => compute_file_sha256_base64,
|
Ok(f) => f,
|
||||||
(256, false) => compute_file_sha256,
|
Err(e) => {
|
||||||
(384, true) => compute_file_sha384_base64,
|
return Err(format!("File {} could not be open {}", path, e).into());
|
||||||
(384, false) => compute_file_sha384,
|
}
|
||||||
(512, true) => compute_file_sha512_base64,
|
|
||||||
(512, false) => compute_file_sha512,
|
|
||||||
_ => return Err("`get_file_hash`: bad arguments".into()),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let hash = open_file(&self.search_paths, &path).and_then(compute_hash_fn);
|
let hash = match sha_type {
|
||||||
|
256 => compute_file_hash::<Sha256>(f, base64),
|
||||||
|
384 => compute_file_hash::<Sha384>(f, base64),
|
||||||
|
512 => compute_file_hash::<Sha512>(f, base64),
|
||||||
|
_ => return Err("`get_file_hash`: Invalid sha value".into()),
|
||||||
|
};
|
||||||
|
|
||||||
match hash {
|
match hash {
|
||||||
Ok(digest) => Ok(to_value(digest).unwrap()),
|
Ok(digest) => Ok(to_value(digest).unwrap()),
|
||||||
|
|
|
@ -7,7 +7,7 @@ use config::Config;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
use errors::{Error, Result, bail};
|
use errors::{bail, Error, Result};
|
||||||
use utils::templates::rewrite_theme_paths;
|
use utils::templates::rewrite_theme_paths;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
|
Loading…
Reference in a new issue