Skip to content

Commit

Permalink
feat: 优化 zip 解包兼容性,增加 README。
Browse files Browse the repository at this point in the history
  • Loading branch information
hamflx committed Feb 10, 2023
1 parent 8a99c72 commit 9a11e5a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
name = "fetchbrowser"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["hamflx <[email protected]>"]
license = "MIT"
license-file = "LICENSE"

[[bin]]
name = "fb"
Expand Down
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
17 changes: 10 additions & 7 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -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}"
}
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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!(
Expand All @@ -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()
Expand Down

0 comments on commit 9a11e5a

Please sign in to comment.