libcachebust/src/processor.rs

411 lines
12 KiB
Rust
Raw Normal View History

2021-04-08 20:05:11 +05:30
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* Use of this source code is governed by the Apache 2.0 and/or the MIT
* License.
*/
2021-04-10 17:45:00 +05:30
//! Module describing file processor that changes filenames to setup cache-busting
//!
//! Run the following during build using `build.rs`:
//!
//! ```rust
//! use cache_buster::BusterBuilder;
//!
//! fn main() {
//! // note: add error checking yourself.
//! // println!("cargo:rustc-env=GIT_process={}", git_process);
//! let types = vec![
//! mime::IMAGE_PNG,
//! mime::IMAGE_SVG,
//! mime::IMAGE_JPEG,
//! mime::IMAGE_GIF,
//! ];
//!
//! let config = BusterBuilder::default()
//! .source("./dist")
//! .result("./prod")
//! .mime_types(types)
//! .copy(true)
//! .follow_links(true)
//! .build()
//! .unwrap();
//!
//! config.process().unwrap();
//! }
//! ```
//!
//! There's a runtime component to this library which will let you read modified
//! filenames from within your program. See [Files]
use std::collections::HashMap;
2021-04-08 20:05:11 +05:30
use std::io::Error;
use std::path::Path;
use std::{fs, path::PathBuf};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
2021-04-08 20:05:11 +05:30
use walkdir::WalkDir;
use crate::ENV_VAR_NAME;
2021-04-08 20:45:38 +05:30
2021-04-10 17:45:00 +05:30
/// Configuration for setting up cache-busting
2021-04-08 20:05:11 +05:30
#[derive(Debug, Clone, Builder)]
pub struct Buster {
2021-04-10 17:45:00 +05:30
/// source directory
2021-04-08 20:05:11 +05:30
#[builder(setter(into))]
source: String,
2021-04-10 17:45:00 +05:30
/// mime_types for hashing
2021-04-08 20:05:11 +05:30
mime_types: Vec<mime::Mime>,
2021-04-10 17:45:00 +05:30
/// directory for writing results
2021-04-08 20:05:11 +05:30
#[builder(setter(into))]
result: String,
2021-04-12 18:23:56 +05:30
#[builder(setter(into, strip_option), default)]
/// route prefixes
prefix: Option<String>,
2021-04-10 17:45:00 +05:30
/// copy other non-hashed files from source dire to result dir?
2021-04-08 20:05:11 +05:30
copy: bool,
2021-04-10 17:45:00 +05:30
/// follow symlinks?
2021-04-08 20:05:11 +05:30
follow_links: bool,
}
impl Buster {
2021-04-10 17:45:00 +05:30
// creates base_dir to output files to
fn init(&self) -> Result<(), Error> {
2021-04-08 20:05:11 +05:30
let res = Path::new(&self.result);
if res.exists() {
fs::remove_dir_all(&self.result).unwrap();
}
2021-04-12 18:23:56 +05:30
println!("{}", &self.result);
2021-04-08 20:05:11 +05:30
fs::create_dir(&self.result).unwrap();
self.create_dir_structure(Path::new(&self.source))?;
Ok(())
}
2021-04-09 12:20:27 +05:30
fn hasher(payload: &[u8]) -> String {
2021-04-08 20:05:11 +05:30
use data_encoding::HEXUPPER;
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(payload);
HEXUPPER.encode(&hasher.finalize())
}
2021-04-10 17:45:00 +05:30
/// Processes files.
///
/// If MIME types are uncommon, then use this funtion
/// as it won't panic when a weird MIM is encountered.
///
2021-04-10 18:00:32 +05:30
/// Otherwise, use [process][Self::process]
2021-04-10 17:45:00 +05:30
///
/// Note: it omits processing uncommon MIME types
pub fn try_process(&self) -> Result<(), Error> {
2021-04-10 17:45:00 +05:30
self.init()?;
2021-04-09 14:05:27 +05:30
let mut file_map: Files = Files::new(&self.result);
2021-04-08 20:05:11 +05:30
for entry in WalkDir::new(&self.source)
.follow_links(self.follow_links)
.into_iter()
{
let entry = entry?;
let path = entry.path();
let path = Path::new(&path);
for mime_type in self.mime_types.iter() {
if let Some(file_mime) = mime_guess::from_path(path).first() {
if &file_mime == mime_type {
let contents = Self::read_to_string(&path).unwrap();
let hash = Self::hasher(&contents);
let new_name = format!(
"{}.{}.{}",
path.file_stem().unwrap().to_str().unwrap(),
hash,
path.extension().unwrap().to_str().unwrap()
);
self.copy(path, &new_name);
let (source, destination) = self.gen_map(path, &&new_name);
2021-04-08 22:00:41 +05:30
let _ = file_map.add(
source.to_str().unwrap().into(),
destination.to_str().unwrap().into(),
);
2021-04-08 20:05:11 +05:30
}
}
}
}
file_map.to_env();
Ok(())
2021-04-08 20:05:11 +05:30
}
2021-04-10 17:45:00 +05:30
/// Processes files.
///
/// If MIME types are common, then use this funtion
/// as it will panic when a weird MIM is encountered.
pub fn process(&self) -> Result<(), Error> {
2021-04-10 17:45:00 +05:30
// panics when mimetypes are detected. This way you'll know which files are ignored
// from processing
self.init()?;
2021-04-09 14:05:27 +05:30
let mut file_map: Files = Files::new(&self.result);
2021-04-08 20:05:11 +05:30
for entry in WalkDir::new(&self.source)
.follow_links(self.follow_links)
.into_iter()
{
let entry = entry?;
let path = entry.path();
if !path.is_dir() {
let path = Path::new(&path);
for mime_type in self.mime_types.iter() {
let file_mime = mime_guess::from_path(path)
.first()
.expect(&format!("couldn't resolve MIME for file: {:?}", &path));
if &file_mime == mime_type {
let contents = Self::read_to_string(&path).unwrap();
let hash = Self::hasher(&contents);
let new_name = format!(
"{}.{}.{}",
path.file_stem().unwrap().to_str().unwrap(),
hash,
path.extension().unwrap().to_str().unwrap()
);
self.copy(path, &new_name);
let (source, destination) = self.gen_map(path, &&new_name);
2021-04-08 22:00:41 +05:30
let _ = file_map.add(
source.to_str().unwrap().into(),
destination.to_str().unwrap().into(),
);
2021-04-08 20:05:11 +05:30
}
}
}
}
file_map.to_env();
Ok(())
2021-04-08 20:05:11 +05:30
}
2021-04-10 17:45:00 +05:30
// helper fn to read file to string
2021-04-09 12:20:27 +05:30
fn read_to_string(path: &Path) -> Result<Vec<u8>, Error> {
2021-04-08 20:05:11 +05:30
use std::fs::File;
2021-04-09 12:20:27 +05:30
use std::io::Read;
2021-04-08 20:05:11 +05:30
2021-04-09 12:20:27 +05:30
let mut file_content = Vec::new();
let mut file = File::open(path)?;
file.read_to_end(&mut file_content).expect("Unable to read");
Ok(file_content)
2021-04-08 20:05:11 +05:30
}
2021-04-10 17:45:00 +05:30
// helper fn to generate filemap
2021-04-08 20:05:11 +05:30
fn gen_map<'a>(&self, source: &'a Path, name: &str) -> (&'a Path, PathBuf) {
let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
2021-04-12 18:23:56 +05:30
if let Some(prefix) = &self.prefix {
//panic!("{}", &prefix);
let mut result = self.result.as_str();
if result.chars().nth(0) == Some('/') {
result = &self.result[1..];
}
let destination = Path::new(prefix)
.join(&self.result[1..])
.join(rel_location)
.join(name);
(source, destination.into())
} else {
let destination = Path::new(&self.result).join(rel_location).join(name);
(source, destination.into())
}
2021-04-08 20:05:11 +05:30
}
2021-04-10 17:45:00 +05:30
// helper fn to copy files
2021-04-08 20:05:11 +05:30
fn copy(&self, source: &Path, name: &str) {
let rel_location = source.strip_prefix(&self.source).unwrap().parent().unwrap();
let destination = Path::new(&self.result).join(rel_location).join(name);
fs::copy(source, &destination).unwrap();
}
2021-04-10 17:45:00 +05:30
// helper fn to create directory structure in self.base_dir
2021-04-08 20:05:11 +05:30
fn create_dir_structure(&self, path: &Path) -> Result<(), Error> {
for entry in WalkDir::new(&path)
.follow_links(self.follow_links)
.into_iter()
{
let entry = entry?;
let entry_path = entry.path();
let entry_path = Path::new(&entry_path);
if entry_path.is_dir() && path != entry_path {
Self::create_dir_structure(&self, entry_path)?;
} else {
if entry_path.is_dir() {
let rel_location = entry_path.strip_prefix(&self.source).unwrap();
let destination = Path::new(&self.result).join(rel_location);
if !destination.exists() {
fs::create_dir(destination)?
}
}
}
}
Ok(())
}
}
/// Filemap struct
///
/// maps original names to generated names
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
struct Files {
/// filemap<original-path, modified-path>
pub map: HashMap<String, String>,
base_dir: String,
}
impl Files {
/// Initialize map
fn new(base_dir: &str) -> Self {
Files {
map: HashMap::default(),
base_dir: base_dir.into(),
}
}
/// Create file map: map original path to modified paths
fn add(&mut self, k: String, v: String) -> Result<(), &'static str> {
if self.map.contains_key(&k) {
Err("key exists")
} else {
self.map.insert(k, v);
Ok(())
}
}
/// This crate uses compile-time environment variables to transfer
/// data to the main program. This funtction sets that variable
fn to_env(&self) {
println!(
"cargo:rustc-env={}={}",
ENV_VAR_NAME,
serde_json::to_string(&self).unwrap()
);
// needed for testing load()
// if the above statement fails(println), then something's broken
// with the rust compiler. So not really worried about that.
#[cfg(test)]
std::env::set_var(ENV_VAR_NAME, serde_json::to_string(&self).unwrap());
}
#[cfg(test)]
/// Load filemap in main program. Should be called from main program
fn load() -> Self {
let env = std::env::var(ENV_VAR_NAME)
.expect("unable to read env var, might be a bug in lib. Please report on GitHub");
let res: Files = serde_json::from_str(&env).unwrap();
res
}
}
2021-04-08 20:05:11 +05:30
#[cfg(test)]
2021-04-08 22:00:41 +05:30
pub mod tests {
2021-04-08 20:05:11 +05:30
use super::*;
#[test]
fn hasher_works() {
let types = vec![
mime::IMAGE_PNG,
mime::IMAGE_SVG,
mime::IMAGE_JPEG,
mime::IMAGE_GIF,
];
let config = BusterBuilder::default()
.source("./dist")
2021-04-10 17:45:00 +05:30
.result("./prod56")
2021-04-08 20:05:11 +05:30
.mime_types(types)
.copy(true)
.follow_links(true)
.build()
.unwrap();
config.process().unwrap();
let mut files = Files::load();
2021-04-08 20:05:11 +05:30
2021-04-08 20:45:38 +05:30
for (k, v) in files.map.drain() {
2021-04-08 20:05:11 +05:30
let src = Path::new(&k);
let dest = Path::new(&v);
assert_eq!(src.exists(), dest.exists());
}
2021-04-12 18:23:56 +05:30
2021-04-08 22:00:41 +05:30
cleanup(&config);
2021-04-08 20:05:11 +05:30
}
2021-04-08 20:14:53 +05:30
#[test]
2021-04-10 17:45:00 +05:30
fn try_process_works() {
2021-04-08 20:14:53 +05:30
let types = vec![
mime::IMAGE_PNG,
mime::IMAGE_SVG,
mime::IMAGE_JPEG,
mime::IMAGE_GIF,
];
let config = BusterBuilder::default()
.source("./dist")
.result("/tmp/prod")
.mime_types(types)
.copy(true)
.follow_links(true)
.build()
.unwrap();
config.process().unwrap();
let mut files = Files::load();
2021-04-08 20:14:53 +05:30
2021-04-08 20:45:38 +05:30
for (k, v) in files.map.drain() {
2021-04-08 20:14:53 +05:30
let src = Path::new(&k);
let dest = Path::new(&v);
assert_eq!(src.exists(), dest.exists());
}
2021-04-08 22:00:41 +05:30
cleanup(&config);
}
pub fn cleanup(config: &Buster) {
let _ = fs::remove_dir_all(&config.result);
2021-04-08 20:14:53 +05:30
}
2021-04-12 18:23:56 +05:30
#[test]
fn prefix_works() {
let types = vec![
mime::IMAGE_PNG,
mime::IMAGE_SVG,
mime::IMAGE_JPEG,
mime::IMAGE_GIF,
];
let config = BusterBuilder::default()
.source("./dist")
.result("/tmp/prod2i")
.mime_types(types)
.copy(true)
.follow_links(true)
.prefix("/test")
.build()
.unwrap();
config.process().unwrap();
let mut files = Files::load();
if let Some(prefix) = &config.prefix {
for (k, v) in files.map.drain() {
let src = Path::new(&k);
println!("{}", &v);
let dest = Path::new(&v[prefix.len()..]);
assert_eq!(src.exists(), dest.exists());
}
}
cleanup(&config);
}
2021-04-08 20:05:11 +05:30
}