Skip to content

Commit

Permalink
Updated Matrix to also send updater artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
w-henderson committed Jun 28, 2022
1 parent 98e8a35 commit e2c2680
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
66 changes: 60 additions & 6 deletions matrix/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ struct Args {
platform: String,

#[clap(short, long, value_parser)]
file: String,
file: Option<String>,

#[clap(short, long, value_parser)]
signature: String,
updater: Option<String>,

#[clap(short, long, value_parser)]
signature: Option<String>,

#[clap(short = 'n', long = "notes", value_parser)]
release_notes: Option<String>,
Expand All @@ -39,18 +42,65 @@ struct Args {
fn main() {
let args = Args::parse();

let directory_listing = std::fs::read_dir("./")
.unwrap()
.flatten()
.collect::<Vec<_>>();

let file_path = args
.file
.map(PathBuf::from)
.or_else(|| {
directory_listing
.iter()
.find(|d| match d.path().extension() {
Some(ext) => ext == "msi" || ext == "app" || ext == "AppImage",
None => false,
})
.map(|d| d.path())
})
.expect("Could not find file");

let updater_path = args
.updater
.map(PathBuf::from)
.or_else(|| {
directory_listing
.iter()
.find(|d| match d.path().extension() {
Some(ext) => ext == "gz" || ext == "zip",
None => false,
})
.map(|d| d.path())
})
.expect("Could not find updater");

let signature_path = args
.signature
.map(PathBuf::from)
.or_else(|| {
directory_listing
.iter()
.find(|d| d.path().extension().unwrap_or_default() == "sig")
.map(|d| d.path())
})
.expect("Could not find signature");

let payload = {
let file_contents =
base64::encode(std::fs::read(&args.file).expect("Could not read release file"));
base64::encode(std::fs::read(&file_path).expect("Could not read release file"));
let updater_contents =
base64::encode(std::fs::read(&updater_path).expect("Could not read updater file"));
let signature_contents =
base64::encode(std::fs::read(&args.signature).expect("Could not read signature file"));
base64::encode(std::fs::read(&signature_path).expect("Could not read signature file"));

let file_name = PathBuf::from(&args.file)
let file_name = file_path.file_name().unwrap().to_string_lossy().to_string();
let updater_name = updater_path
.file_name()
.unwrap()
.to_string_lossy()
.to_string();
let signature_name = PathBuf::from(&args.signature)
let signature_name = signature_path
.file_name()
.unwrap()
.to_string_lossy()
Expand All @@ -67,6 +117,10 @@ fn main() {
"name": file_name,
"data": file_contents
},
"updater": {
"name": updater_name,
"data": updater_contents
},
"signature": {
"name": signature_name,
"data": signature_contents
Expand Down
19 changes: 17 additions & 2 deletions matrix/server/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct CreateJson {
version: String,
platform: String,
file: FileJson,
updater: FileJson,
signature: FileJson,
release_notes: String,
api_key: String,
Expand All @@ -29,6 +30,7 @@ json_map! {
version => "version",
platform => "platform",
file => "file",
updater => "updater",
signature => "signature",
release_notes => "releaseNotes",
api_key => "apiKey"
Expand Down Expand Up @@ -64,9 +66,11 @@ pub fn handler(request: Request, state: Arc<State>) -> Response {
return Err((StatusCode::BadRequest, "Invalid platform".into()));
}

// Extract and parse the contents of the file and signature.
// Extract and parse the contents of the file, updater and signature.
let file_contents = base64::decode(json.file.data)
.map_err(|_| (StatusCode::BadRequest, "Invalid base64".into()))?;
let updater_contents = base64::decode(json.updater.data)
.map_err(|_| (StatusCode::BadRequest, "Invalid base64".into()))?;
let signature = String::from_utf8(
base64::decode(json.signature.data)
.map_err(|_| (StatusCode::BadRequest, "Invalid base64".into()))?,
Expand All @@ -84,9 +88,20 @@ pub fn handler(request: Request, state: Arc<State>) -> Response {
)
.map_err(|_| (StatusCode::InternalError, "Failed to write file".into()))?;

fs::write(
state
.base_path
.join("releases")
.join(&json.platform)
.join(&json.updater.name),
updater_contents,
)
.map_err(|_| (StatusCode::InternalError, "Failed to write updater".into()))?;

// Write to the database
let database_entry = Release {
url: json.file.name,
filename: json.file.name,
updater_filename: json.updater.name,
version: json.version,
notes: json.release_notes,
platform: json.platform.clone(),
Expand Down
2 changes: 1 addition & 1 deletion matrix/server/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn handler(request: Request, state: Arc<State>) -> Response {
match release {
Ok(release) => Response::redirect(format!(
"/release/download/{}/{}",
release.platform, release.url
release.platform, release.filename
)),
Err(JasonError::InvalidKey) => Response::new(StatusCode::NotFound, "Invalid platform"),
Err(err) => Response::new(StatusCode::InternalError, err.to_string()),
Expand Down
6 changes: 4 additions & 2 deletions matrix/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub struct State {
}

pub struct Release {
url: String,
filename: String,
updater_filename: String,
version: String,
notes: String,
platform: String,
Expand All @@ -35,7 +36,8 @@ pub struct Release {

json_map! {
Release,
url => "url",
filename => "filename",
updater_filename => "updater_filename",
version => "version",
notes => "notes",
platform => "platform",
Expand Down
5 changes: 4 additions & 1 deletion matrix/server/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ pub fn handler(request: Request, state: Arc<State>) -> Response {
.get("Host")
.ok_or_else(|| (StatusCode::BadRequest, "Missing Host header".into()))?;

let release_url = format!("https://{}/release/download/{}/{}", host, release.platform, release.url);
let release_url = format!(
"https://{}/release/download/{}/{}",
host, release.platform, release.updater_filename
);

let payload = json!({
"url": release_url,
Expand Down

0 comments on commit e2c2680

Please sign in to comment.