Skip to main content

htmlpacker/
encoder.rs

1/*
2* encoder.rs
3*
4* The point of this file:
5* encode the wasm binaries using brotli and base64
6* return a Base, which means what goes into a <pre> tag inside the html
7* 
8* base64 is highly optimized for encode/decode
9* we will eventually implement base94 if there is a need
10* 
11* so what do we need to do to encode
12* we read the file from a filepath
13* 
14* we compress with brotli (or not)
15* 
16* we encode as base64
17* 
18* we return the id and the utf-8 compatible string
19*/
20
21use std::fs::File;
22use std::io::Read;
23use std::error::Error;
24use base64::prelude::*;
25
26use sha2::{Sha256, Digest};
27
28#[derive(Debug)]
29pub struct Base {
30    pub id: String, // identifier
31    pub hash: String, //sha-256 hash of the text as bytes
32    pub text: String, // text content
33}
34
35impl Base {
36    pub fn new(id: String, hash: String, text: String) -> Self {
37        Base {
38            id: id,
39            hash: hash,
40            text: text,
41        }
42    }
43}
44
45// get file and read file content
46fn get_file_bytes(png_path: &str) -> Result<Vec<u8>, Box<dyn Error>> {
47    let mut file = File::open(png_path)?;
48    let mut buffer = Vec::new();
49    file.read_to_end(&mut buffer)?;
50    Ok(buffer)
51}
52
53// encode any local file to base64
54pub fn encode_base64(
55    file_path: &str, 
56    id: &str,
57) -> Result<Base, Box<dyn Error>> {
58    let buffer = get_file_bytes(file_path)?;
59    let hash = Sha256::digest(&buffer);
60    let hash_string = format!("{:x}", hash);
61    //println!("hash {:?}\nstring: {}", hash, hash_string);
62    let encoded = BASE64_STANDARD.encode(&buffer);
63    Ok(Base::new(
64        String::from(id),
65        hash_string,
66        encoded
67    ))
68}
69
70// so we have a buffer of bytes
71// lets compress with brotli
72// create a buffer for compressed data
73pub fn encode_brotli(
74    buffer: &Vec<u8>
75) -> Result<Vec<u8>, Box<dyn Error>> {
76    let mut compressed_buffer = Vec::new();
77    println!("brotli compression"); 
78    brotli::BrotliCompress(
79        &mut &buffer[..],           // input buffer as a Read impl
80        &mut compressed_buffer,     // output buffer as a Write impl
81        &brotli::enc::BrotliEncoderParams {
82            //quality: 11,          // quality (0-11)
83            quality: 9,             // try 9 as sweet spot
84            lgwin: 22,              // Window size (recommended 20-22)
85            ..Default::default() 
86        }
87    )?;
88    println!("brotli done"); 
89    Ok(compressed_buffer)
90}
91
92// encode both with brotli and then base64
93pub fn encode_brotli_base64(
94    file_path: &str, 
95    id: &str,
96) -> Result<Base, Box<dyn Error>> {
97    let buffer = get_file_bytes(file_path)?;
98    let hash = Sha256::digest(&buffer);
99    let hash_string = format!("{:x}", hash);
100    let compressed_buffer = encode_brotli(&buffer)?;
101    let encoded = BASE64_STANDARD.encode(&compressed_buffer);
102    Ok(Base::new(
103        String::from(id),
104        hash_string,
105        encoded
106    ))
107}
108