Merge pull request #4215 from madpilot78/Fix_Windows_Renamed_File_2
Reimplement code extracting information from file name.
This commit is contained in:
commit
c7a6828307
@ -36,24 +36,38 @@ pub fn get_license_from_string(s: &str) -> ResultType<License> {
|
|||||||
} else {
|
} else {
|
||||||
s
|
s
|
||||||
};
|
};
|
||||||
|
/*
|
||||||
|
* The following code tokenizes the file name based on commas and
|
||||||
|
* extracts relevant parts sequentially.
|
||||||
|
*
|
||||||
|
* host= is expected to be the first part.
|
||||||
|
*
|
||||||
|
* Since Windows renames files adding (1), (2) etc. before the .exe
|
||||||
|
* in case of duplicates, which causes the host or key values to be
|
||||||
|
* garbled.
|
||||||
|
*
|
||||||
|
* This allows using a ',' (comma) symbol as a final delimiter.
|
||||||
|
*/
|
||||||
if s.contains("host=") {
|
if s.contains("host=") {
|
||||||
let strs: Vec<&str> = s.split("host=").collect();
|
let stripped = &s[s.find("host=").unwrap_or(0)..s.len()];
|
||||||
if strs.len() == 2 {
|
let strs: Vec<&str> = stripped.split(",").collect();
|
||||||
let strs2: Vec<&str> = strs[1].split(",key=").collect();
|
let mut host = "";
|
||||||
let host;
|
|
||||||
let mut key = "";
|
let mut key = "";
|
||||||
if strs2.len() == 2 {
|
let strs_iter = strs.iter();
|
||||||
host = strs2[0];
|
for el in strs_iter {
|
||||||
key = strs2[1];
|
if el.starts_with("host=") {
|
||||||
} else {
|
host = &el[5..el.len()];
|
||||||
host = strs[1];
|
}
|
||||||
|
|
||||||
|
if el.starts_with("key=") {
|
||||||
|
key = &el[4..el.len()];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Ok(License {
|
return Ok(License {
|
||||||
host: host.to_owned(),
|
host: host.to_owned(),
|
||||||
key: key.to_owned(),
|
key: key.to_owned(),
|
||||||
api: "".to_owned(),
|
api: "".to_owned(),
|
||||||
});
|
});
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let strs = if s.contains("-licensed-") {
|
let strs = if s.contains("-licensed-") {
|
||||||
s.split("-licensed-")
|
s.split("-licensed-")
|
||||||
@ -68,3 +82,60 @@ pub fn get_license_from_string(s: &str) -> ResultType<License> {
|
|||||||
}
|
}
|
||||||
bail!("Failed to parse");
|
bail!("Failed to parse");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
mod test {
|
||||||
|
#[test]
|
||||||
|
fn test_filename_license_string() {
|
||||||
|
assert_eq!(
|
||||||
|
get_license_from_string("rustdesk.exe"),
|
||||||
|
Ok(License {
|
||||||
|
host: "".to_owned(),
|
||||||
|
key: "".to_owned(),
|
||||||
|
api: "".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_license_from_string("rustdesk"),
|
||||||
|
Ok(License {
|
||||||
|
host: "".to_owned(),
|
||||||
|
key: "".to_owned(),
|
||||||
|
api: "".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_license_from_string("rustdesk-host=server.example.net.exe"),
|
||||||
|
Ok(License {
|
||||||
|
host: "server.example.net".to_owned(),
|
||||||
|
key: "".to_owned(),
|
||||||
|
api: "".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_license_from_string("rustdesk-host=server.example.net,.exe"),
|
||||||
|
Ok(License {
|
||||||
|
host: "server.example.net".to_owned(),
|
||||||
|
key: "".to_owned(),
|
||||||
|
api: "".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// key in these tests is "foobar.,2" base64 encoded
|
||||||
|
assert_eq!(
|
||||||
|
get_license_from_string("rustdesk-host=server.example.net,key=Zm9vYmFyLiwyCg==.exe"),
|
||||||
|
Ok(License {
|
||||||
|
host: "server.example.net".to_owned(),
|
||||||
|
key: "Zm9vYmFyLiwyCg==".to_owned(),
|
||||||
|
api: "".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_license_from_string("rustdesk-host=server.example.net,key=Zm9vYmFyLiwyCg==,.exe"),
|
||||||
|
Ok(License {
|
||||||
|
host: "server.example.net".to_owned(),
|
||||||
|
key: "Zm9vYmFyLiwyCg==".to_owned(),
|
||||||
|
api: "".to_owned(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user