From c731cd963c9c35ca8f9aeea4625192d1170b2584 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 27 May 2025 19:19:09 -0500 Subject: [PATCH] Some but not all of the upload routine --- Cargo.toml | 2 +- src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4ecc1db..45c55db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2024" [dependencies] clap = { version = "4.0.7", features = ["derive", "env"] } -reqwest = { version = "0.11.13", features = ["json"] } +reqwest = { version = "0.11.13", features = ["json", "stream", "multipart"] } serde = { version = "1.0.152", features = ["derive"] } tokio = { version = "1.24.2", features = ["macros", "rt-multi-thread"] } diff --git a/src/main.rs b/src/main.rs index a93e65d..2e304fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,12 @@ use gt_tools::CreateReleaseOption; use gt_tools::{ReleaseInfo, cli::Args}; +use reqwest::multipart::Part; -use std::env; +use std::collections::HashMap; +use std::{ + env, + fs +}; use clap::Parser; @@ -49,9 +54,18 @@ async fn main() -> Result<(), Error> { println!("Release Tag: {tag_name}"); println!("Creating?: {create}"); println!("Files..."); - for file in files { + for file in &files { println!("--- {file}"); } + // TODO: Pre-create the release, if it doesn't exist. + // TODO: Find an existing release and use it's ID, if it does + do_upload_release( + &client, + &args.gitea_url, + &args.repo, + 52usize, + files + ).await?; } } @@ -110,3 +124,59 @@ async fn do_create_release( println!("{:?}", result); Ok(()) } + +async fn do_upload_release( + client: &reqwest::Client, + gitea_url: &str, + repo: &str, + release_id: usize, + files: Vec, +) -> Result<(), Error> { + let token = env::var("RELEASE_KEY_GITEA").expect( + "You must set the RELEASE_KEY_GITEA environment variable so the Gitea API can be used.", + ); + let request_url = format!( + "{gitea_url}{front}{repo}{back}/{id}/assets", + front = API_RELEASE_FRONT, + back = API_RELEASE_BACK, + id = release_id + ); + // Setup a "partial request". I'll need to loop over the files, so I'll + // clone this a bunch of times for each one + let partial_request = client + .post(request_url) + .header(USER_AGENT, "gt-tools-test-agent") + .header(ACCEPT, "application/json") + // .header("Content-Type", "multipart/form-data") + .header("Authorization", format!("token {}", token)); + + // Ensure all files exists before starting the uploads + for file in &files { + assert!(fs::exists(file) + .expect(&format!("Can't check existence of {file}")) + ); + } + + for file in files { + if let Some(request) = partial_request.try_clone() { + println!("Uploading file {}", &file); + let data = reqwest::multipart::Part::stream(fs::read(&file).unwrap()) + .file_name("attachment") + .mime_str("text/plain")?; + + let form= reqwest::multipart::Form::new() + .part("attachment", data); + + let request = request + .multipart(form) + .query(&[("name", &file)]); + dbg!(&request); + let response = request.send().await?; + dbg!(&response); + dbg!(&response.text().await?); + } else { + panic!("Failed to clone the RequestBuilder during file upload loop."); + } + } + Ok(()) +}