Skip to content

Commit 19c1507

Browse files
committed
feat(download): 增强仓库下载功能
- 修改下载命令,支持指定分支 - 更新 README.md 中的使用说明 - 调整配置文件中的最小文件大小限制 - 优化下载函数,增加 zip 文件完整性校验
1 parent 54b50d3 commit 19c1507

5 files changed

Lines changed: 23 additions & 9 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
- **镜像加速**
3232
自动测试多个Git镜像源延迟,选择最快的源进行克隆/拉取(支持`clone`/`pull`/`push`/`fetch`)。
33-
- **下载文件**
33+
- **下载文件**
3434
支持直接下载仓库压缩包(TODO: 支持下载release文件)。
3535
- **代理支持**
3636
可通过命令行参数或配置文件设置HTTP/HTTPS代理。
@@ -85,8 +85,8 @@ fgit clone <user>/<repo> # fgit的特色方式,会自动转换为https://githu
8585
fgit pull
8686

8787
# 下载仓库压缩包(自动选择镜像源)
88-
fgit download-zip <仓库URL>
89-
fgit download-zip <user>/<repo>
88+
fgit download <仓库URL>
89+
fgit --branch <分支名> download <user>/<repo>
9090

9191
# 启用代理
9292
fgit --use-proxy http://127.0.0.1:7890 clone <仓库URL>

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_downloader_config(self):
4242
return dict(self.config.items('downloader'))
4343
self.config.add_section('downloader')
4444
self.config.set('downloader', 'chunk_size', '1024')
45-
self.config.set('downloader', 'MIN_FILE_SIZE', '1')
45+
self.config.set('downloader', 'MIN_FILE_SIZE', '100')
4646
self._save()
4747
return dict(self.config.items('downloader'))
4848

downloader.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import os
2+
import zipfile
13
from tqdm import tqdm
24
import requests
35
from loguru import logger
46

5-
def download_file(url: str, file_path: str, chunk_size: int = 1024, MIN_FILE_SIZE: int = 1) -> bool:
7+
def download_file(url: str, file_path: str, chunk_size: int = 1024, MIN_FILE_SIZE: int = 100) -> bool:
68
try:
79
response = requests.get(url, stream=True)
810
total_size = int(response.headers.get('content-length', 0))
@@ -16,6 +18,15 @@ def download_file(url: str, file_path: str, chunk_size: int = 1024, MIN_FILE_SIZ
1618
for data in response.iter_content(chunk_size=int(chunk_size)):
1719
f.write(data)
1820
pbar.update(len(data))
21+
22+
with zipfile.ZipFile(file_path, 'r') as zip_ref:
23+
try:
24+
zip_ref.testzip() # 校验文件完整性
25+
except zipfile.BadZipFile:
26+
logger.error(f"下载失败: {file_path} 不是一个有效的zip文件")
27+
os.remove(file_path)
28+
return False
29+
1930
return True
2031
except Exception as e:
2132
logger.error(f"下载失败: {str(e)}")

fgit.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
parser = argparse.ArgumentParser(description='Git加速工具,支持镜像源和代理')
2020
parser.add_argument('command', type=str, help='git命令, 或是fgit命令')
2121
parser.add_argument('--use-proxy', type=str, help='设置HTTP代理(格式: http://[user:pass@]host:port)')
22+
parser.add_argument('--branch', type=str, help='分支名(仅在download命令时有效)', default='main')
23+
2224
parser.add_argument('--verbose', action='store_true', help='显示详细输出')
2325
args, unknown_args = parser.parse_known_args()
2426

@@ -45,7 +47,7 @@ def main():
4547
logger.debug(Fore.CYAN + f"命令参数: {' '.join(sys.argv)}" + Style.RESET_ALL)
4648

4749

48-
if args.command == 'download-zip':
50+
if args.command == 'download':
4951
handle_download_zip(args, unknown_args, config, env, args.verbose)
5052
return
5153

@@ -66,7 +68,7 @@ def handle_download_zip(args, unknown_args, config, env, verbose):
6668
if not downloader_config:
6769
logger.warning(Fore.YELLOW + "🧐 下载配置不存在, 使用默认配置" + Style.RESET_ALL)
6870
chunk_size = downloader_config.get('chunk_size', 1024)
69-
MIN_FILE_SIZE = downloader_config.get('min_file_size', 1)
71+
MIN_FILE_SIZE = downloader_config.get('min_file_size', 100)
7072

7173
original_url = unknown_args[0]
7274
if '://' not in original_url and '/' in original_url:
@@ -89,9 +91,9 @@ def handle_download_zip(args, unknown_args, config, env, verbose):
8991

9092
mirror_list = select_mirror(config, args.verbose)
9193
for mirror in mirror_list:
92-
new_url = convert_url(original_url, mirror) + '/archive/refs/heads/main.zip'
94+
new_url = convert_url(original_url, mirror) + f'/archive/refs/heads/{args.branch}.zip'
9395
logger.info(Fore.GREEN + f"🔄 尝试镜像源 {mirror} [{mirror_list.index(mirror) + 1}/{len(mirror_list)}]: {new_url}" + Style.RESET_ALL)
94-
if download_file(new_url, os.path.join(os.getcwd(), repo_name + '.zip'), chunk_size=chunk_size, MIN_FILE_SIZE=MIN_FILE_SIZE):
96+
if download_file(new_url, os.path.join(os.getcwd(), f'{repo_name}-{args.branch}.zip'), chunk_size=chunk_size, MIN_FILE_SIZE=MIN_FILE_SIZE):
9597
return
9698
logger.error(Fore.RED + "❌ 所有镜像源尝试失败" + Style.RESET_ALL)
9799
return

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ ping3
22
colorama
33
prettytable
44
loguru
5+
requests
56
tqdm

0 commit comments

Comments
 (0)