diff --git a/Cargo.lock b/Cargo.lock index 78c5eeb..61464a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -413,6 +413,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -587,6 +597,7 @@ dependencies = [ "js-sys", "log", "mime", + "mime_guess", "native-tls", "once_cell", "percent-encoding", @@ -931,6 +942,15 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.13" @@ -970,6 +990,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "want" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 3186ee2..eb21559 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -reqwest = { version = "0.11.18", features = ["json"] } +reqwest = { version = "0.11.18", features = ["json", "multipart"] } soft_assert = "0.1.1" thiserror = "1.0.43" tokio = { version = "1.29.1", features = ["net"] } diff --git a/src/lib.rs b/src/lib.rs index dea6d44..d133868 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,13 +104,13 @@ impl Forgejo { self.execute(request).await } - async fn post_form( + async fn post_multipart( &self, path: &str, - body: &T, - ) -> Result { + body: reqwest::multipart::Form, + ) -> Result { let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); - let request = self.client.post(url).form(body).build()?; + let request = self.client.post(url).multipart(body).build()?; self.execute(request).await } diff --git a/src/repository.rs b/src/repository.rs index 4d89ec9..8b063a0 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -140,11 +140,12 @@ impl Forgejo { repo: &str, id: u64, name: &str, - file: &[u8], + file: Vec, ) -> Result { - self.post_form( + self.post_multipart( &format!("repos/{owner}/{repo}/releases/{id}/assets?name={name}"), - &file, + reqwest::multipart::Form::new() + .part("attachment", reqwest::multipart::Part::bytes(file).file_name("file").mime_str("*/*").unwrap()), ) .await } diff --git a/tests/ci_test.rs b/tests/ci_test.rs index 993b8ab..d8a4fa5 100644 --- a/tests/ci_test.rs +++ b/tests/ci_test.rs @@ -154,7 +154,7 @@ async fn repo(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let release_latest = api.latest_release("TestingAdmin", "test").await.wrap_err("failed to find latest release")?; ensure!(release_by_tag == release_latest, "releases not equal"); - let attachment = api.create_release_attachment("TestingAdmin", "test", release.id, "test.txt", b"This is a file!").await.wrap_err("failed to create release attachment")?; + let attachment = api.create_release_attachment("TestingAdmin", "test", release.id, "test.txt", b"This is a file!".to_vec()).await.wrap_err("failed to create release attachment")?; api.delete_release_attachment("TestingAdmin", "test", release.id, attachment.id).await.wrap_err("failed to deleted attachment")?; api.delete_release("TestingAdmin", "test", release.id).await.wrap_err("failed to delete release")?;