From 9a11e5aeeb443679180873c0cb425d361e4f852f Mon Sep 17 00:00:00 2001
From: hamflx
Date: Sat, 11 Feb 2023 02:28:28 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20zip=20=E8=A7=A3?=
=?UTF-8?q?=E5=8C=85=E5=85=BC=E5=AE=B9=E6=80=A7=EF=BC=8C=E5=A2=9E=E5=8A=A0?=
=?UTF-8?q?=20README=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Cargo.lock | 2 +-
Cargo.toml | 4 +++-
LICENSE | 9 +++++++++
README.md | 27 +++++++++++++++++++++++++++
install.ps1 | 17 ++++++++++-------
src/main.rs | 20 ++++++++++----------
6 files changed, 60 insertions(+), 19 deletions(-)
create mode 100644 LICENSE
create mode 100644 README.md
diff --git a/Cargo.lock b/Cargo.lock
index fcf3fcb..5105567 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -308,7 +308,7 @@ dependencies = [
[[package]]
name = "fetchbrowser"
-version = "0.1.0"
+version = "0.1.1"
dependencies = [
"anyhow",
"bytes",
diff --git a/Cargo.toml b/Cargo.toml
index 30a6cb2..6bad5d9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,8 +1,10 @@
[package]
name = "fetchbrowser"
-version = "0.1.0"
+version = "0.1.1"
edition = "2021"
authors = ["hamflx "]
+license = "MIT"
+license-file = "LICENSE"
[[bin]]
name = "fb"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ee4f10d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+MIT License
+
+Copyright 2023 hamflx
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ef3411b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,27 @@
+# Fetch Browser
+
+浏览器下载器。支持下载指定版本的 `Chromium` 和 `Firefox`。
+
+## 安装(Windows)
+
+在 `Powershell` 中输入如下命令:
+
+```powershell
+irm https://raw.githubusercontent.com/hamflx/fetchbrowser/master/install.ps1 | iex
+```
+
+## 使用
+
+下载 `Chromium 98`
+
+```powershell
+fb 98
+```
+
+**注意:在特定平台第一次下载 `Chromium` 会比较慢,因为会联机查找版本信息,后续会使用缓存的数据。**
+
+下载 `Firefox 98`
+
+```powershell
+fb --firefox 98
+```
diff --git a/install.ps1 b/install.ps1
index bdfd858..189222f 100644
--- a/install.ps1
+++ b/install.ps1
@@ -1,23 +1,26 @@
+$ErrorActionPreference = 'Stop'
+
$repo = "hamflx/fetchbrowser"
$file = "fb.exe"
-$releases = "https://api.github.com/repos/$repo/releases"
+$releases = "https://api.github.com/repos/${repo}/releases"
Write-Host Determining latest release
$tag = (Invoke-WebRequest $releases | ConvertFrom-Json)[0].tag_name
-$download = "https://github.com/$repo/releases/download/$tag/$file"
-$fb_dir = "$HOME/.fb"
-$fb_bin_dir = "$fb_dir/bin"
-$fb_bin_path = "$fb_bin_dir/$file"
+$download = "https://github.com/${repo}/releases/download/${tag}/${file}"
+$fb_dir = "${HOME}\.fb"
+$fb_bin_dir = "${fb_dir}\bin"
+$fb_bin_path = "${fb_bin_dir}\$file"
-New-Item "$fb_bin_dir" -ItemType Directory -Force
+New-Item "${fb_bin_dir}" -ItemType Directory -Force
Write-Host Dowloading latest release
Invoke-WebRequest $download -Out $fb_bin_path
$old_path = [System.Environment]::GetEnvironmentVariable("PATH", "User")
-if ($old_path -notcontains $fb_bin_path) {
+if (!(";${old_path};".ToLower() -like "*;${fb_bin_path};*".ToLower())) {
$new_path = $old_path + [IO.Path]::PathSeparator + $fb_bin_path
[Environment]::SetEnvironmentVariable("PATH", $new_path, "User")
+ $Env:Path += ";${fb_bin_path}"
}
diff --git a/src/main.rs b/src/main.rs
index 21645aa..1615863 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -133,8 +133,6 @@ fn download_zip_file(
println!("==> downloading {}", zip_file.media_link);
let mut win_zip_response = reqwest::blocking::get(&zip_file.media_link)?;
- // 执行 zip 解压过程,并去除压缩包的根目录。
- let mut prefix = String::new();
loop {
let mut zip = match read_zipfile_from_stream(&mut win_zip_response) {
Ok(Some(zip)) => zip,
@@ -145,14 +143,13 @@ fn download_zip_file(
let zip_name = zip.name();
println!("==> unzip: {zip_name}");
- if prefix.is_empty() {
- if zip.is_dir() {
- prefix = zip.name().to_owned();
- } else {
- return Err(anyhow!("压缩包内目录结构不正确。"));
- }
- } else if zip_name.starts_with(&prefix) {
- let file_path = base_path.join(&zip_name[prefix.len()..]);
+ if zip_name.starts_with("chrome-win/")
+ || zip_name.starts_with("chrome-win32/")
+ || zip_name.starts_with("chrome-mac/")
+ || zip_name.starts_with("chrome-linux/")
+ {
+ let prefix_len = zip_name.find('/').unwrap() + 1;
+ let file_path = base_path.join(&zip_name[prefix_len..]);
if zip.is_dir() {
std::fs::create_dir_all(&file_path).map_err(|err| {
anyhow!(
@@ -162,6 +159,9 @@ fn download_zip_file(
)
})?;
} else {
+ if let Some(parent_dir) = file_path.parent() {
+ let _ = std::fs::create_dir_all(parent_dir);
+ }
copy(
&mut zip,
&mut OpenOptions::new()