From 66daf8ec6d73ca3bb502701647cff0654885f13d Mon Sep 17 00:00:00 2001 From: luhc228 Date: Sat, 1 Jun 2024 21:17:42 +0800 Subject: [PATCH] feat:m update ci --- .github/workflows/ci.yml | 19 ++++++++++++------- src/utils/download_file.rs | 38 +++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee7fc35..e25d846 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,16 +21,19 @@ jobs: os: windows-latest target: x86_64-pc-windows-msvc bin: toolkit.exe + name: toolkit-Windows-x86_64.zip - os_name: macOS-x86_64 # https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories os: macos-13 target: x86_64-apple-darwin bin: toolkit + name: toolkit-macOS-x86_64.zip - os_name: macOS-aarch64 os: macos-latest target: aarch64-apple-darwin - skip_tests: true bin: toolkit + name: toolkit-macOS-aarch64.zip + skip_tests: true toolchain: [stable] steps: - uses: actions/checkout@v4 @@ -55,21 +58,23 @@ jobs: args: "--locked --release" strip: true if: (startsWith(github.ref, 'refs/tags/cli-v') || github.ref == 'refs/heads/test-release') + # Windows - name: Move binary to bin directory run: | - mkdir -p bin - move target/${{ matrix.platform.target }}/release/${{ matrix.platform.bin }} bin/${{ matrix.platform.os_name }}-${{ matrix.platform.bin }} + cd target/${{ matrix.platform.target }}/release + 7z a ../../../${{ matrix.platform.name }} ${{ matrix.platform.bin }} if: matrix.platform.os == 'windows-latest' && (startsWith(github.ref, 'refs/tags/cli-v') || github.ref == 'refs/heads/test-release') + # Macos & Linux - name: Move binary to bin directory run: | - mkdir -p bin - mv target/${{ matrix.platform.target }}/release/${{ matrix.platform.bin }} bin/${{ matrix.platform.os_name }}-${{ matrix.platform.bin }} + cd target/${{ matrix.platform.target }}/release + zip ../../../${{ matrix.platform.name }} ${{ matrix.platform.bin }} if: matrix.platform.os != 'windows-latest' && (startsWith(github.ref, 'refs/tags/cli-v') || github.ref == 'refs/heads/test-release') - name: Publish release binary uses: actions/upload-artifact@v4 with: name: toolkit-${{ matrix.platform.os_name }} - path: bin/${{ matrix.platform.os_name }}-${{ matrix.platform.bin }} + path: toolkit-* if: (startsWith(github.ref, 'refs/tags/cli-v') || github.ref == 'refs/heads/test-release') - name: Generate SHA-256 run: shasum -a 256 bin/${{ matrix.platform.os_name }}-${{ matrix.platform.bin }} > bin/${{ matrix.platform.os_name }}-${{ matrix.platform.bin }}.sha256 @@ -78,6 +83,6 @@ jobs: uses: softprops/action-gh-release@v2 with: draft: true - files: "bin/*" + files: "toolkit-*" body_path: CHANGELOG.md if: (startsWith(github.ref, 'refs/tags/cli-v') || github.ref == 'refs/heads/test-release') diff --git a/src/utils/download_file.rs b/src/utils/download_file.rs index 94c39b9..b0ae028 100644 --- a/src/utils/download_file.rs +++ b/src/utils/download_file.rs @@ -1,7 +1,7 @@ use anyhow::Result; use futures_util::StreamExt; use regex::Regex; -use reqwest::{Client, Response}; +use reqwest::{Client, Response, Url}; use std::{cmp::min, env, fs::File, io::Write, path::PathBuf}; pub async fn download_file(url: &str, set_process_message: impl Fn(&str)) -> Result { @@ -50,19 +50,27 @@ fn get_file_name_from_response(response: &Response) -> Result { .ok_or(anyhow::anyhow!("Failed to get content-disposition header"))? .to_str() .map_err(|err| anyhow::anyhow!("Failed to convert content-disposition header to string. Error: {}", err))?; - let re = - Regex::new(r"filename=([^;]+)").map_err(|err| anyhow::anyhow!("Failed to create regex. Error: {}", err))?; - let file_name = re - .captures(content_disposition) - .ok_or(anyhow::anyhow!( - "Failed to get file name from content-disposition header" - ))? - .get(1) - .ok_or(anyhow::anyhow!( - "Failed to get file name from content-disposition header" - ))? - .as_str() - .replace('"', ""); + if content_disposition == "attachment" { + let url = response.url(); + let filename = Url::parse(response.url().as_str()) + .map_err(|err| anyhow::anyhow!("Failed to parse url '{}'. Error: {}", url, err)) + .map(|url| url.path_segments().unwrap().last().unwrap().to_string())?; + Ok(filename) + } else { + let re = + Regex::new(r"filename=([^;]+)").map_err(|err| anyhow::anyhow!("Failed to create regex. Error: {}", err))?; + let file_name = re + .captures(content_disposition) + .ok_or(anyhow::anyhow!( + "Failed to get file name from content-disposition header" + ))? + .get(1) + .ok_or(anyhow::anyhow!( + "Failed to get file name from content-disposition header" + ))? + .as_str() + .replace('"', ""); - Ok(file_name) + Ok(file_name) + } }