Verified Commit 30ef85cd authored by Stavros Korokithakis's avatar Stavros Korokithakis
Browse files

Allow gzipped firmware

parent 61775fa2
Pipeline #316449246 passed with stage
in 4 minutes and 1 second
......@@ -15,9 +15,16 @@ use version_compare::Version;
/**
* Get the latest version firmware for the specific firmware name.
*
* Returns the version as a string along with the filename,
* e.g. ("1.14.2-dev", "ESPURNA_S20-1.14.2-dev.bin.gz").
*/
fn get_latest_fw_version(directory: &str, fw_name: &str) -> std::io::Result<String> {
let re = Regex::new(&format!("{}{}{}", r"^", fw_name, r"-([\d\w\.-]+)\.bin$")).unwrap();
fn get_latest_fw_version(directory: &str, fw_name: &str) -> std::io::Result<(String, String)> {
let re = Regex::new(&format!(
"{}{}{}",
r"^", fw_name, r"-([\d\w\.-]+)\.bin(\.gz)?$"
))
.unwrap();
let paths: Vec<(String, String)> = fs::read_dir(directory)?
.filter_map(|r| {
r.ok()
......@@ -33,19 +40,21 @@ fn get_latest_fw_version(directory: &str, fw_name: &str) -> std::io::Result<Stri
// to create it, so we can't return it from the loop. We can only keep
// the filename itself and parse it again every time.
let mut max_ver_str = "0".to_string();
let mut max_filename = "0".to_string();
let mut found_fw = false;
for (_filename, version) in paths {
for (filename, version) in paths {
let ver = Version::from(&version);
let max_ver = Version::from(&max_ver_str);
if ver > max_ver {
max_ver_str = version;
max_filename = filename;
found_fw = true;
}
}
if found_fw {
Ok(max_ver_str)
Ok((max_ver_str, max_filename))
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
......@@ -114,30 +123,27 @@ fn firmware_request(req: &mut Request) -> IronResult<Response> {
let latest_version = get_latest_fw_version(".", &name);
let response = match latest_version {
Ok(ref lv) => {
Ok((ref lv, ref lf)) => {
if Version::from(lv) > Version::from(&request_version) {
// New version is found.
println!("Got firmware request for \"{}\", request version was {}, newer version is available at {}.", name, request_version, lv);
println!("Got firmware request for \"{}\", request version was {}, newer version is available at {}.", name, request_version, lf);
if nofuss_protocol {
Response::with((
content_type,
iron::status::Ok,
format!(
r#"{{"version": "{}", "firmware": "{}firmware/{}-{}.bin", "spiffs": ""}}"#,
lv, leading_slash, name, lv
r#"{{"version": "{}", "firmware": "{}firmware/{}", "spiffs": ""}}"#,
lv, leading_slash, lf
),
))
} else {
Response::with((
iron::status::Ok,
Path::new(&format!("{}-{}.bin", name, lv).to_string()),
))
Response::with((iron::status::Ok, Path::new(lf)))
}
} else {
// No new version is found.
println!("Got firmware request for \"{}\", request version was {}, latest version available is {} (older).", name, request_version, lv);
println!("Got firmware request for \"{}\", request version was {}, latest version available is {} (older).", name, request_version, lf);
if nofuss_protocol {
Response::with((content_type, iron::status::Ok, "{}"))
} else {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment