mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-14 04:13:43 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09bdb5584a | ||
|
|
36f5c9569e | ||
|
|
3d9edc5133 | ||
|
|
ed08c05532 | ||
|
|
9bd8befcb8 | ||
|
|
7ba87e48fc | ||
|
|
af5f739630 | ||
|
|
5558cac263 | ||
|
|
4128e718e9 | ||
|
|
6d017b18f5 | ||
|
|
35eb089178 | ||
|
|
b5f78f0fbe | ||
|
|
628d73f90d | ||
|
|
7d2d84d844 | ||
|
|
1cbe58d760 | ||
|
|
c77a4094a3 | ||
|
|
33f6325bf7 | ||
|
|
701ea59035 | ||
|
|
e77441f0de | ||
|
|
dd46e7a3eb | ||
|
|
f42838810a | ||
|
|
7fdba12376 | ||
|
|
20130ae6d0 | ||
|
|
4598e9b351 | ||
|
|
2e639e0fef | ||
|
|
dbbbc204fb | ||
|
|
caab121cc2 | ||
|
|
c637b36094 | ||
|
|
38ba901956 | ||
|
|
8bae1a5850 | ||
|
|
56375c0edb | ||
|
|
5b69472ac8 | ||
|
|
dbc9c4c121 | ||
|
|
c0e24ce869 | ||
|
|
74c24ad860 | ||
|
|
9b1ed15fea | ||
|
|
a875a9258e | ||
|
|
0d2ac10af8 | ||
|
|
978b8bc7cb | ||
|
|
50c3713c90 | ||
|
|
2639b6bc65 | ||
|
|
8b29ceb151 | ||
|
|
e1c14ad9f8 | ||
|
|
e4f86097e8 | ||
|
|
0515ce6081 | ||
|
|
048554df4d | ||
|
|
2d02486ca6 | ||
|
|
3523f5855a | ||
|
|
aa9577a2e0 | ||
|
|
efc9dcca0b | ||
|
|
b062336c09 | ||
|
|
baf88a5cb7 | ||
|
|
e208014d34 | ||
|
|
3ecedd60fd | ||
|
|
6fd68e4752 | ||
|
|
c2910b0e8c | ||
|
|
8ed51787ad | ||
|
|
2e5fd3017c | ||
|
|
027d29e834 | ||
|
|
1a3c8c026b | ||
|
|
17cb59c606 | ||
|
|
e3828c2982 | ||
|
|
8cef0bb727 | ||
|
|
21ec1e4539 | ||
|
|
dda44cc770 | ||
|
|
78911b2aa9 | ||
|
|
4935dcd0e6 | ||
|
|
e8fe6ad550 | ||
|
|
db4003ad4f | ||
|
|
0a4be165a5 | ||
|
|
c62867e3ef | ||
|
|
6df4959845 | ||
|
|
e0b18013be | ||
|
|
d205e3b372 | ||
|
|
719daf054b | ||
|
|
a5f72a22c3 | ||
|
|
bf5c7655f2 | ||
|
|
691c2f496e | ||
|
|
9ef3405e43 | ||
|
|
215faa0bf0 | ||
|
|
695da7ea87 |
No files matched your search
@@ -0,0 +1,27 @@
|
|||||||
|
## Donate
|
||||||
|
|
||||||
|
Thank you for your support! 🙏
|
||||||
|
|
||||||
|
<div style="text-align: center;">
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>微信</th>
|
||||||
|
<th>支付宝</th>
|
||||||
|
<th>PayPal</th>
|
||||||
|
<th>Ko-fi</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><img src="/.donate/qr/wechatpay.jpg" alt="wechatpay" width="300" /></td>
|
||||||
|
<td><img src="/.donate/qr/alipay.jpg" alt="alipay" width="300" /></td>
|
||||||
|
<td><a href="https://paypal.me/monkeyWie" target="_blank">Click Me</a></td>
|
||||||
|
<td><a href="https://ko-fi.com/gopeed" target="_blank">Click Me</a></td>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
### Crypto
|
||||||
|
|
||||||
|
- **BTC**: 35jYkPN8bgkh7AWjt3B1VgMQHqtGYcvF6p
|
||||||
|
- **ETH:** 0x928a7fd2398c941Ac6713704e606Fa414595e3A0
|
||||||
|
- **Matic:** 0x928a7fd2398c941Ac6713704e606Fa414595e3A0
|
||||||
|
- **BNB:** 0x928a7fd2398c941Ac6713704e606Fa414595e3A0
|
||||||
|
- **USDT(ERC20)**: 0x928a7fd2398c941Ac6713704e606Fa414595e3A0
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
@@ -0,0 +1,15 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
#github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||||
|
#patreon: # Replace with a single Patreon username
|
||||||
|
#open_collective: # Replace with a single Open Collective username
|
||||||
|
#ko_fi: # Replace with a single Ko-fi username
|
||||||
|
#tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||||
|
#community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||||
|
#liberapay: # Replace with a single Liberapay username
|
||||||
|
#issuehunt: # Replace with a single IssueHunt username
|
||||||
|
#otechie: # Replace with a single Otechie username
|
||||||
|
#custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||||
|
# github: monkeyWie
|
||||||
|
ko_fi: gopeed
|
||||||
|
custom: https://github.com/GopeedLab/gopeed/blob/main/.donate/index.md#donate
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<!--
|
<!--
|
||||||
请根据以下模版提issue
|
Please use the following template to create an issue.
|
||||||
-->
|
-->
|
||||||
### 问题描述(必要)
|
### Description(required)
|
||||||
### 版本号(必要)
|
### App Version(required)
|
||||||
### 操作系统(必要)
|
### OS Version(required)
|
||||||
### 相关截图
|
### Snapshots
|
||||||
### 相关日志
|
### Log
|
||||||
+68
-42
@@ -2,8 +2,10 @@ name: build
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
release:
|
|
||||||
types: [published]
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
get-release:
|
get-release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -12,9 +14,11 @@ jobs:
|
|||||||
upload_url: ${{ steps.get-release.outputs.upload_url }}
|
upload_url: ${{ steps.get-release.outputs.upload_url }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: monkeyWie/get-latest-release@v1
|
||||||
- uses: thebritican/[email protected]
|
|
||||||
id: get-release
|
id: get-release
|
||||||
|
with:
|
||||||
|
myToken: ${{ github.token }}
|
||||||
|
|
||||||
build-windows:
|
build-windows:
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
needs: [get-release]
|
needs: [get-release]
|
||||||
@@ -29,7 +33,10 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
||||||
run: |
|
run: |
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/monkeyWie/gopeed/bind/desktop
|
Invoke-WebRequest -Uri "https://github.com/jrsoftware/issrc/raw/main/Files/Languages/Unofficial/ChineseSimplified.isl" -OutFile "ChineseSimplified.isl"
|
||||||
|
mv ChineseSimplified.isl "C:\Program Files (x86)\Inno Setup 6\Languages\"
|
||||||
|
|
||||||
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build windows
|
flutter build windows
|
||||||
$mingw = "C:\ProgramData\Chocolatey\lib\mingw\tools\install\mingw64\bin"
|
$mingw = "C:\ProgramData\Chocolatey\lib\mingw\tools\install\mingw64\bin"
|
||||||
@@ -43,7 +50,7 @@ jobs:
|
|||||||
cp $system\vcruntime140_1.dll $release
|
cp $system\vcruntime140_1.dll $release
|
||||||
|
|
||||||
New-Item -Path build\windows\Output -ItemType Directory
|
New-Item -Path build\windows\Output -ItemType Directory
|
||||||
Compress-Archive -Path "$release*" -DestinationPath build\windows\Output\Gopeed-$env:VERSION-windows-amd64.zip
|
Compress-Archive -Path "$release*" -DestinationPath "build\windows\Output\Gopeed-$env:VERSION-windows-amd64-portable.zip"
|
||||||
|
|
||||||
cd build/windows
|
cd build/windows
|
||||||
echo @"
|
echo @"
|
||||||
@@ -77,9 +84,12 @@ jobs:
|
|||||||
Compression=lzma
|
Compression=lzma
|
||||||
SolidCompression=yes
|
SolidCompression=yes
|
||||||
WizardStyle=modern
|
WizardStyle=modern
|
||||||
|
LanguageDetectionMethod=uilanguage
|
||||||
|
ShowLanguageDialog=yes
|
||||||
|
|
||||||
[Languages]
|
[Languages]
|
||||||
Name: "english"; MessagesFile: "compiler:Default.isl"
|
Name: "english"; MessagesFile: "compiler:Default.isl"
|
||||||
|
Name: "chinesesimplified"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"
|
||||||
|
|
||||||
[Tasks]
|
[Tasks]
|
||||||
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
|
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
|
||||||
@@ -88,6 +98,9 @@ jobs:
|
|||||||
Source: ".\runner\Release\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs
|
Source: ".\runner\Release\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs
|
||||||
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
|
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
|
||||||
|
|
||||||
|
[UninstallDelete]
|
||||||
|
Type: filesandordirs; Name: "{app}"
|
||||||
|
|
||||||
[Icons]
|
[Icons]
|
||||||
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
|
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
|
||||||
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
|
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
|
||||||
@@ -96,7 +109,9 @@ jobs:
|
|||||||
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
|
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
|
||||||
"@ > setup.iss
|
"@ > setup.iss
|
||||||
iscc.exe setup.iss
|
iscc.exe setup.iss
|
||||||
mv "Output\gopeed.exe" "Output\Gopeed-$env:VERSION-windows-amd64-install.exe"
|
mv "Output\gopeed.exe" "Output\Gopeed-$env:VERSION-windows-amd64.exe"
|
||||||
|
Compress-Archive -Path "Output\Gopeed-$env:VERSION-windows-amd64.exe" -DestinationPath "Output\Gopeed-$env:VERSION-windows-amd64.zip"
|
||||||
|
Remove-Item -Path "Output\Gopeed-$env:VERSION-windows-amd64.exe"
|
||||||
- name: Upload
|
- name: Upload
|
||||||
uses: shogo82148/actions-upload-release-asset@v1
|
uses: shogo82148/actions-upload-release-asset@v1
|
||||||
with:
|
with:
|
||||||
@@ -112,20 +127,9 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
go-version: "^1.19"
|
go-version: "^1.19"
|
||||||
cache: true
|
cache: true
|
||||||
- run: mkdir -p ~/image-cache
|
|
||||||
- id: image-cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/image-cache
|
|
||||||
key: image-cache-xgo-v1.19
|
|
||||||
- if: steps.image-cache.outputs.cache-hit != 'true'
|
|
||||||
run: |
|
|
||||||
docker pull techknowlogick/xgo:go-1.19.x
|
|
||||||
docker save -o ~/image-cache/xgo.tar techknowlogick/xgo:go-1.19.x
|
|
||||||
- if: steps.image-cache.outputs.cache-hit == 'true'
|
|
||||||
run: docker load -i ~/image-cache/xgo.tar
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
|
docker pull techknowlogick/xgo:go-1.19.x
|
||||||
go install src.techknowlogick.com/xgo@latest
|
go install src.techknowlogick.com/xgo@latest
|
||||||
xgo --targets=darwin/arm64 -tags="nosqlite" -ldflags="-w -s" -buildmode=c-shared -pkg=bind/desktop -out=libgopeed .
|
xgo --targets=darwin/arm64 -tags="nosqlite" -ldflags="-w -s" -buildmode=c-shared -pkg=bind/desktop -out=libgopeed .
|
||||||
mv libgopeed-*.dylib libgopeed.dylib
|
mv libgopeed-*.dylib libgopeed.dylib
|
||||||
@@ -151,14 +155,23 @@ jobs:
|
|||||||
- uses: actions/download-artifact@v3
|
- uses: actions/download-artifact@v3
|
||||||
with:
|
with:
|
||||||
name: macos-arm64-lib
|
name: macos-arm64-lib
|
||||||
path: ui/flutter/macos/Frameworks
|
path: ui/flutter/lib-arm64
|
||||||
- name: Build
|
- name: Build
|
||||||
env:
|
env:
|
||||||
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
||||||
run: |
|
run: |
|
||||||
PROJECT_DIR=$(pwd)
|
PROJECT_DIR=$(pwd)
|
||||||
|
|
||||||
cd ui/flutter
|
# amd64 lib
|
||||||
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/lib-amd64/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
|
||||||
|
# universal binary
|
||||||
|
mkdir -p ui/flutter/macos/Frameworks
|
||||||
|
cp ui/flutter/lib-amd64/libgopeed.dylib ui/flutter/macos/Frameworks/amd64-lib
|
||||||
|
cp ui/flutter/lib-arm64/libgopeed.dylib ui/flutter/macos/Frameworks/arm64-lib
|
||||||
|
cd ui/flutter/macos/Frameworks
|
||||||
|
lipo -create -output libgopeed.dylib amd64-lib arm64-lib
|
||||||
|
|
||||||
|
cd $PROJECT_DIR/ui/flutter
|
||||||
flutter build macos
|
flutter build macos
|
||||||
cd build/macos/Build/Products/Release
|
cd build/macos/Build/Products/Release
|
||||||
cat>appdmg.json<<EOF
|
cat>appdmg.json<<EOF
|
||||||
@@ -172,14 +185,7 @@ jobs:
|
|||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
mkdir dist
|
mkdir dist
|
||||||
appdmg appdmg.json dist/Gopeed-$VERSION-macos-arm64.dmg
|
appdmg appdmg.json dist/Gopeed-$VERSION-macos.dmg
|
||||||
|
|
||||||
# amd64
|
|
||||||
cd $PROJECT_DIR
|
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o bin/libgopeed.dylib github.com/monkeyWie/gopeed/bind/desktop
|
|
||||||
cp bin/libgopeed.dylib ui/flutter/build/macos/Build/Products/Release/Gopeed.app/Contents/Frameworks/
|
|
||||||
cd ui/flutter/build/macos/Build/Products/Release
|
|
||||||
appdmg appdmg.json dist/Gopeed-$VERSION-macos-amd64.dmg
|
|
||||||
- name: Upload
|
- name: Upload
|
||||||
uses: shogo82148/actions-upload-release-asset@v1
|
uses: shogo82148/actions-upload-release-asset@v1
|
||||||
with:
|
with:
|
||||||
@@ -203,7 +209,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
||||||
run: |
|
run: |
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build linux
|
flutter build linux
|
||||||
mkdir -p debian/gui
|
mkdir -p debian/gui
|
||||||
@@ -262,12 +268,16 @@ jobs:
|
|||||||
- name: Build
|
- name: Build
|
||||||
env:
|
env:
|
||||||
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
||||||
|
APK_KEYSTORE : ${{ secrets.APK_KEYSTORE }}
|
||||||
|
APK_KEY_PASSWORD : ${{ secrets.APK_KEY_PASSWORD }}
|
||||||
|
APK_STORE_PASSWORD : ${{ secrets.APK_STORE_PASSWORD }}
|
||||||
run: |
|
run: |
|
||||||
mkdir ui/flutter/android/app/libs
|
|
||||||
go install golang.org/x/mobile/cmd/gomobile@latest
|
go install golang.org/x/mobile/cmd/gomobile@latest
|
||||||
|
go get golang.org/x/mobile/bind
|
||||||
gomobile init
|
gomobile init
|
||||||
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/monkeyWie/gopeed/bind/mobile
|
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/GopeedLab/gopeed/bind/mobile
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
|
echo $APK_KEYSTORE | base64 -di > android/app/upload-keystore.jks
|
||||||
flutter build apk
|
flutter build apk
|
||||||
mkdir dist
|
mkdir dist
|
||||||
cp build/app/outputs/flutter-apk/app-release.apk dist/Gopeed-$VERSION-android.apk
|
cp build/app/outputs/flutter-apk/app-release.apk dist/Gopeed-$VERSION-android.apk
|
||||||
@@ -301,9 +311,13 @@ jobs:
|
|||||||
export CGO_ENABLED=0
|
export CGO_ENABLED=0
|
||||||
for goos in "${goos_arr[@]}"; do
|
for goos in "${goos_arr[@]}"; do
|
||||||
for goarch in "${goarch_arr[@]}"; do
|
for goarch in "${goarch_arr[@]}"; do
|
||||||
name=gopeed-web-$VERSION-$goos-$goarch
|
goos_name=$goos
|
||||||
|
if [ $goos = "darwin" ]; then
|
||||||
|
goos_name="macos"
|
||||||
|
fi
|
||||||
|
name=gopeed-web-$VERSION-$goos_name-$goarch
|
||||||
dir="dist/$name/"
|
dir="dist/$name/"
|
||||||
(GOOS=$goos GOARCH=$goarch go build -tags nosqlite,web -ldflags="-s -w" -o $dir github.com/monkeyWie/gopeed/cmd/web \
|
(GOOS=$goos GOARCH=$goarch go build -tags nosqlite,web -ldflags="-s -w" -o $dir github.com/GopeedLab/gopeed/cmd/web \
|
||||||
&& cd $dir \
|
&& cd $dir \
|
||||||
&& file=$(ls -AU | head -1) \
|
&& file=$(ls -AU | head -1) \
|
||||||
&& mkdir $name \
|
&& mkdir $name \
|
||||||
@@ -324,13 +338,25 @@ jobs:
|
|||||||
needs: [get-release]
|
needs: [get-release]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- name: Build
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
- name: Login to DockerHub
|
||||||
|
uses: docker/login-action@v1
|
||||||
|
with:
|
||||||
|
username: liwei2633
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v2
|
||||||
env:
|
env:
|
||||||
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
VERSION: ${{ needs.get-release.outputs.tag_name }}
|
||||||
run: |
|
with:
|
||||||
tag=liwei2633/gopeed:${VERSION:1}
|
push: true
|
||||||
docker build -t $tag .
|
platforms: |
|
||||||
docker login -u liwei2633 -p ${{ secrets.DOCKER_PASSWORD }}
|
linux/386
|
||||||
docker push $tag
|
linux/amd64
|
||||||
docker tag $tag liwei2633/gopeed:latest
|
linux/arm64
|
||||||
docker push liwei2633/gopeed:latest
|
tags: |
|
||||||
|
liwei2633/gopeed:latest
|
||||||
|
liwei2633/gopeed:${{ env.VERSION }}
|
||||||
@@ -11,10 +11,14 @@ on:
|
|||||||
- "pkg/**"
|
- "pkg/**"
|
||||||
- "ui/**"
|
- "ui/**"
|
||||||
- ".github/workflows/release.yml"
|
- ".github/workflows/release.yml"
|
||||||
|
- ".github/release-drafter.yml"
|
||||||
- "go.mod"
|
- "go.mod"
|
||||||
- "go.sum"
|
- "go.sum"
|
||||||
- "Dockerfile"
|
- "Dockerfile"
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ jobs:
|
|||||||
# go get -u golang.org/x/lint/golint
|
# go get -u golang.org/x/lint/golint
|
||||||
# golint -set_exit_status ./...
|
# golint -set_exit_status ./...
|
||||||
test:
|
test:
|
||||||
name: Test
|
name: Test check
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
@@ -54,9 +54,8 @@ jobs:
|
|||||||
- uses: codecov/codecov-action@v3
|
- uses: codecov/codecov-action@v3
|
||||||
with:
|
with:
|
||||||
files: ./coverage.txt
|
files: ./coverage.txt
|
||||||
fail_ci_if_error: true
|
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build check
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
# needs: [lint, test]
|
# needs: [lint, test]
|
||||||
needs: [test]
|
needs: [test]
|
||||||
@@ -66,5 +65,12 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
go-version: "^1.19"
|
go-version: "^1.19"
|
||||||
cache: true
|
cache: true
|
||||||
|
- uses: subosito/flutter-action@v2
|
||||||
|
- run: |
|
||||||
|
sudo apt-get update -y
|
||||||
|
sudo apt-get install -y ninja-build libgtk-3-dev
|
||||||
- name: Build
|
- name: Build
|
||||||
run: go build github.com/monkeyWie/gopeed/cmd/gopeed
|
run: |
|
||||||
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
|
||||||
|
cd ui/flutter
|
||||||
|
flutter build linux
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
# Gopeed contributors guide
|
||||||
|
|
||||||
|
Firstly, thank you for your interest in contributing to Gopeed. This guide will help you better
|
||||||
|
participate in the development of Gopeed.
|
||||||
|
|
||||||
|
## Branch description
|
||||||
|
|
||||||
|
This project only has one main branch, namely the `main` branch. If you want to participate in the
|
||||||
|
development of Gopeed, please fork this project first, and then develop in your fork project. After
|
||||||
|
development is completed, submit a PR to this project and merge it into the `main` branch.
|
||||||
|
|
||||||
|
## Local development
|
||||||
|
|
||||||
|
It is recommended to develop and debug through the web. First, start the backend service, and start
|
||||||
|
it by the command line `go run cmd/api/main.go`, the default port of the service is `9999`, and then
|
||||||
|
start the front-end flutter project in `debug` mode to run.
|
||||||
|
|
||||||
|
## Translation
|
||||||
|
|
||||||
|
The internationalization files of Gopeed are located in the `ui/flutter/assets/locales` directory.
|
||||||
|
You only need to add the corresponding language file in this directory.
|
||||||
|
|
||||||
|
|
||||||
|
Generate locales after you edit locales:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
get generate locales
|
||||||
|
```
|
||||||
|
|
||||||
|
Please refer to `en_us.dart` for translation.
|
||||||
|
|
||||||
|
## flutter development
|
||||||
|
|
||||||
|
Don't forget to run`dart format ./ui/flutter`before you commit to keep your code in standard dart format
|
||||||
|
|
||||||
|
Turn on build_runner watcher if you want to edit api/models:
|
||||||
|
|
||||||
|
```
|
||||||
|
flutter pub run build_runner watch
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
get-cli commands usages:
|
||||||
|
|
||||||
|
```
|
||||||
|
create:
|
||||||
|
controller: Generate controller
|
||||||
|
page: Use to generate pages
|
||||||
|
view: Generate view
|
||||||
|
generate:
|
||||||
|
locales: Generate translation file from json files
|
||||||
|
model: generate Class model from json
|
||||||
|
```
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# Gopeed 贡献指南
|
||||||
|
|
||||||
|
首先感谢您对贡献代码感兴趣,这份指南将帮助您更好的参与到 Gopeed 的开发中来。
|
||||||
|
|
||||||
|
## 分支说明
|
||||||
|
|
||||||
|
本项目只有一个主分支,即 `main` 分支,如果您想要参与到 Gopeed 的开发中来,请先 fork 本项目,然后在您的 fork 项目中进行开发,开发完成后再向本项目提交
|
||||||
|
PR,合并到 `main` 分支。
|
||||||
|
|
||||||
|
## 本地开发
|
||||||
|
|
||||||
|
建议通过 web 端进行开发调试,首先启动后端服务,通过命令行 `go run cmd/api/main.go` 启动 ,服务启动默认端口为 `9999`,然后以 `debug` 模式启动前端
|
||||||
|
flutter 项目即可运行。
|
||||||
|
|
||||||
|
## 翻译
|
||||||
|
|
||||||
|
Gopeed 的国际化文件位于 `ui/flutter/assets/locales` 目录下,只需要在该目录下添加对应的语言文件即可。
|
||||||
|
|
||||||
|
编辑locales后请运行以下命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
get generate locales
|
||||||
|
```
|
||||||
|
|
||||||
|
请注意以 `en_US.json` 为参照进行翻译。
|
||||||
|
|
||||||
|
## flutter开发
|
||||||
|
|
||||||
|
每次提交前请务必`dart format ./ui/flutter`
|
||||||
|
|
||||||
|
如果要编辑api/models,请打开build_runner watcher:
|
||||||
|
|
||||||
|
```
|
||||||
|
flutter pub run build_runner watch
|
||||||
|
```
|
||||||
|
|
||||||
|
适用get-cli 命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
create:
|
||||||
|
controller: Generate controller
|
||||||
|
page: Use to generate pages
|
||||||
|
view: Generate view
|
||||||
|
generate:
|
||||||
|
locales: Generate translation file from json files
|
||||||
|
model: generate Class model from json
|
||||||
|
```
|
||||||
|
|
||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
FROM cirrusci/flutter:3.3.7 AS flutter
|
FROM cirrusci/flutter:3.7.7 AS flutter
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY ./ui/flutter/pubspec.yaml ./ui/flutter/pubspec.lock ./
|
COPY ./ui/flutter/pubspec.yaml ./ui/flutter/pubspec.lock ./
|
||||||
RUN flutter pub get
|
RUN flutter pub get
|
||||||
@@ -11,7 +11,7 @@ COPY ./go.mod ./go.sum ./
|
|||||||
RUN go mod download
|
RUN go mod download
|
||||||
COPY . .
|
COPY . .
|
||||||
COPY --from=flutter /app/build/web ./cmd/web/dist
|
COPY --from=flutter /app/build/web ./cmd/web/dist
|
||||||
RUN CGO_ENABLED=0 go build -tags nosqlite,web -ldflags="-s -w" -o dist/gopeed github.com/monkeyWie/gopeed/cmd/web
|
RUN CGO_ENABLED=0 go build -tags nosqlite,web -ldflags="-s -w" -o dist/gopeed github.com/GopeedLab/gopeed/cmd/web
|
||||||
|
|
||||||
FROM alpine:3.14.2
|
FROM alpine:3.14.2
|
||||||
LABEL maintainer="monkeyWie"
|
LABEL maintainer="monkeyWie"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
[](https://github.com/monkeyWie/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/monkeyWie/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/monkeyWie/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
|
[](https://github.com/GopeedLab/gopeed/blob/main/.donate/index.md#donate)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||

|

|
||||||
@@ -9,9 +10,11 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
Gopeed is a high-speed downloader developed by `Golang`+`Flutter`, which supports (HTTP, BitTorrent, Magnet) protocol downloads and supports all platforms.
|
Gopeed (full name Go Speed), a high-speed downloader developed by `Golang` + `Flutter`, supports (HTTP, BitTorrent, Magnet) protocol, and supports all platforms.
|
||||||
|
|
||||||
## Installation
|
Visit ✈ [Official Website](https://gopeed.com) | 📖 [Develop Docs](https://docs.gopeed.com)
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
**Supported platforms**
|
**Supported platforms**
|
||||||
|
|
||||||
@@ -23,7 +26,7 @@ Gopeed is a high-speed downloader developed by `Golang`+`Flutter`, which support
|
|||||||
- [x] web
|
- [x] web
|
||||||
- [x] docker
|
- [x] docker
|
||||||
|
|
||||||
[To Release](https://github.com/monkeyWie/gopeed/releases/latest)
|
[Download](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||||
|
|
||||||
> Tips: If the macos open failed, please execute the `xattr -d com.apple.quarantine /Applications/Gopeed.app` command in the terminal
|
> Tips: If the macos open failed, please execute the `xattr -d com.apple.quarantine /Applications/Gopeed.app` command in the terminal
|
||||||
|
|
||||||
@@ -32,18 +35,33 @@ Gopeed is a high-speed downloader developed by `Golang`+`Flutter`, which support
|
|||||||
use `go install`:
|
use `go install`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go install github.com/monkeyWie/gopeed/cmd/gopeed
|
go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
|
|
||||||
|
#### Start directly
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
|
docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Using Docker Compose
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Access Gopeed
|
||||||
|
|
||||||
When the docker container is running, you can access the web page through `http://localhost:9999`.
|
When the docker container is running, you can access the web page through `http://localhost:9999`.
|
||||||
|
|
||||||
> Tip: Modify the download path to `/download` on the setting page to access the downloaded files on the host.
|
> Tip: Modify the download path to `/download` on the setting page to access the downloaded files on the host.
|
||||||
|
|
||||||
|
## Donate
|
||||||
|
|
||||||
|
If you like this project, please consider [donating](/.donate/index.md#donate) to support the development of this project, thank you!
|
||||||
|
|
||||||
## Showcase
|
## Showcase
|
||||||
|
|
||||||

|

|
||||||
@@ -57,7 +75,7 @@ This project is divided into two parts, the front end uses `flutter`, the back e
|
|||||||
### Environment
|
### Environment
|
||||||
|
|
||||||
1. Golang 1.19+
|
1. Golang 1.19+
|
||||||
2. Flutter 3.0+
|
2. Flutter 3.7+
|
||||||
|
|
||||||
### Clone
|
### Clone
|
||||||
|
|
||||||
@@ -65,6 +83,10 @@ This project is divided into two parts, the front end uses `flutter`, the back e
|
|||||||
git clone [email protected]:monkeyWie/gopeed.git
|
git clone [email protected]:monkeyWie/gopeed.git
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Contributing
|
||||||
|
|
||||||
|
Please refer to [CONTRIBUTING.md](/CONTRIBUTING.md)
|
||||||
|
|
||||||
### Build
|
### Build
|
||||||
|
|
||||||
#### Desktop
|
#### Desktop
|
||||||
@@ -76,7 +98,7 @@ command:
|
|||||||
- windows
|
- windows
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build windows
|
flutter build windows
|
||||||
```
|
```
|
||||||
@@ -84,7 +106,7 @@ flutter build windows
|
|||||||
- macos
|
- macos
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build macos
|
flutter build macos
|
||||||
```
|
```
|
||||||
@@ -92,7 +114,7 @@ flutter build macos
|
|||||||
- linux
|
- linux
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build linux
|
flutter build linux
|
||||||
```
|
```
|
||||||
@@ -103,6 +125,7 @@ Same as before, you also need to prepare the `cgo` environment, and then install
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
go install golang.org/x/mobile/cmd/gomobile@latest
|
go install golang.org/x/mobile/cmd/gomobile@latest
|
||||||
|
go get golang.org/x/mobile/bind
|
||||||
gomobile init
|
gomobile init
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -111,24 +134,24 @@ command:
|
|||||||
- android
|
- android
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/monkeyWie/gopeed/bind/mobile
|
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/GopeedLab/gopeed/bind/mobile
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build apk
|
flutter build apk
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Web
|
#### Web
|
||||||
|
|
||||||
Web platform communicates directly with the backend http server, no additional environment is required.
|
|
||||||
|
|
||||||
command:
|
command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build web
|
flutter build web
|
||||||
cd ../../
|
cd ../../
|
||||||
|
rm -rf cmd/web/dist
|
||||||
cp -r ui/flutter/build/web cmd/web/dist
|
cp -r ui/flutter/build/web cmd/web/dist
|
||||||
go build -tags nosqlite,web -ldflags="-s -w" -o bin/ github.com/monkeyWie/gopeed/cmd/web
|
go build -tags nosqlite,web -ldflags="-s -w" -o bin/ github.com/GopeedLab/gopeed/cmd/web
|
||||||
````
|
go run cmd/web/main.go
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
+38
-16
@@ -1,6 +1,7 @@
|
|||||||
[](https://github.com/monkeyWie/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/monkeyWie/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/monkeyWie/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
|
[](https://github.com/GopeedLab/gopeed/blob/main/.donate/index.md#donate)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||

|

|
||||||
@@ -9,9 +10,11 @@
|
|||||||
|
|
||||||
## 介绍
|
## 介绍
|
||||||
|
|
||||||
Gopeed 是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、BitTorrent、Magnet)协议下载,并且支持全平台使用。
|
Gopeed(全称 Go Speed),是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、BitTorrent、Magnet)协议下载,并且支持全平台使用。
|
||||||
|
|
||||||
## 下载
|
访问 ✈ [官方网站](https://gopeed.com/zh-CN) | 📖 [开发文档](https://docs.gopeed.com/zh/)
|
||||||
|
|
||||||
|
## 安装
|
||||||
|
|
||||||
**已支持平台**
|
**已支持平台**
|
||||||
|
|
||||||
@@ -23,7 +26,7 @@ Gopeed 是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、
|
|||||||
- [x] web
|
- [x] web
|
||||||
- [x] docker
|
- [x] docker
|
||||||
|
|
||||||
[点击前往](https://github.com/monkeyWie/gopeed/releases/latest)
|
[前往下载](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||||
|
|
||||||
> 注:macos 版本运行如果提示损坏,请在终端执行 `xattr -d com.apple.quarantine /Applications/Gopeed.app` 命令
|
> 注:macos 版本运行如果提示损坏,请在终端执行 `xattr -d com.apple.quarantine /Applications/Gopeed.app` 命令
|
||||||
|
|
||||||
@@ -32,18 +35,32 @@ Gopeed 是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、
|
|||||||
使用`go install`安装:
|
使用`go install`安装:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go install github.com/monkeyWie/gopeed/cmd/gopeed
|
go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
|
|
||||||
|
#### 直接运行
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
|
docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 使用 Docker Compose
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 访问服务
|
||||||
|
|
||||||
当 docker 容器运行时,可以通过 `http://localhost:9999` 访问 web 页面。
|
当 docker 容器运行时,可以通过 `http://localhost:9999` 访问 web 页面。
|
||||||
> 提示:在设置页面把下载路径修改为 `/download` 以便在宿主机访问下载完的文件。
|
> 提示:在设置页面把下载路径修改为 `/download` 以便在宿主机访问下载完的文件。
|
||||||
|
|
||||||
|
## 打赏
|
||||||
|
|
||||||
|
如果觉得项目对你有帮助,请考虑[打赏](/.donate/index.md#donate)以支持这个项目的发展,非常感谢!
|
||||||
|
|
||||||
## 界面展示
|
## 界面展示
|
||||||
|
|
||||||

|

|
||||||
@@ -57,7 +74,7 @@ docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
|
|||||||
### 环境要求
|
### 环境要求
|
||||||
|
|
||||||
1. Golang 1.19+
|
1. Golang 1.19+
|
||||||
2. Flutter 3.0+
|
2. Flutter 3.7+
|
||||||
|
|
||||||
### 克隆项目
|
### 克隆项目
|
||||||
|
|
||||||
@@ -65,6 +82,10 @@ docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
|
|||||||
git clone [email protected]:monkeyWie/gopeed.git
|
git clone [email protected]:monkeyWie/gopeed.git
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 贡献代码
|
||||||
|
|
||||||
|
请参考[贡献指南](CONTRIBUTING_zh-CN.md)
|
||||||
|
|
||||||
### 编译
|
### 编译
|
||||||
|
|
||||||
#### 桌面端
|
#### 桌面端
|
||||||
@@ -76,7 +97,7 @@ git clone [email protected]:monkeyWie/gopeed.git
|
|||||||
- windows
|
- windows
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build windows
|
flutter build windows
|
||||||
```
|
```
|
||||||
@@ -84,7 +105,7 @@ flutter build windows
|
|||||||
- macos
|
- macos
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build macos
|
flutter build macos
|
||||||
```
|
```
|
||||||
@@ -92,7 +113,7 @@ flutter build macos
|
|||||||
- linux
|
- linux
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/monkeyWie/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build linux
|
flutter build linux
|
||||||
```
|
```
|
||||||
@@ -103,6 +124,7 @@ flutter build linux
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
go install golang.org/x/mobile/cmd/gomobile@latest
|
go install golang.org/x/mobile/cmd/gomobile@latest
|
||||||
|
go get golang.org/x/mobile/bind
|
||||||
gomobile init
|
gomobile init
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -111,14 +133,12 @@ gomobile init
|
|||||||
- android
|
- android
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/monkeyWie/gopeed/bind/mobile
|
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/GopeedLab/gopeed/bind/mobile
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build apk
|
flutter build apk
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Web 端(推荐本地调试使用)
|
#### Web 端
|
||||||
|
|
||||||
Web 端直接与后端 http 服务通讯,不需要额外准备环境。
|
|
||||||
|
|
||||||
构建命令:
|
构建命令:
|
||||||
|
|
||||||
@@ -126,8 +146,10 @@ Web 端直接与后端 http 服务通讯,不需要额外准备环境。
|
|||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
flutter build web
|
flutter build web
|
||||||
cd ../../
|
cd ../../
|
||||||
|
rm -rf cmd/web/dist
|
||||||
cp -r ui/flutter/build/web cmd/web/dist
|
cp -r ui/flutter/build/web cmd/web/dist
|
||||||
go build -tags nosqlite,web -ldflags="-s -w" -o bin/ github.com/monkeyWie/gopeed/cmd/web
|
go build -tags nosqlite,web -ldflags="-s -w" -o bin/ github.com/GopeedLab/gopeed/cmd/web
|
||||||
|
go run cmd/web/main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
## 开源许可
|
## 开源许可
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/monkeyWie/gopeed/pkg/download"
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package main
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest"
|
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
|||||||
+2
-2
@@ -4,8 +4,8 @@ package libgopeed
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest"
|
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(cfg string) (int, error) {
|
func Start(cfg string) (int, error) {
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"github.com/GopeedLab/gopeed/cmd"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cfg := &model.StartConfig{
|
||||||
|
Network: "tcp",
|
||||||
|
Address: "127.0.0.1:9999",
|
||||||
|
Storage: model.StorageBolt,
|
||||||
|
}
|
||||||
|
cmd.Start(cfg)
|
||||||
|
}
|
||||||
File renamed without changes.
+1
-1
@@ -25,7 +25,7 @@ func parse() *args {
|
|||||||
if len(t) > 0 {
|
if len(t) > 0 {
|
||||||
args.url = t[0]
|
args.url = t[0]
|
||||||
} else {
|
} else {
|
||||||
gPrintln("missing url parameter")
|
gPrintln("missing url parameter, for example: gopeed https://www.google.com or gopeed bt.torrent or gopeed magnet:?xt=urn:btih:...")
|
||||||
gPrintln("try 'gopeed -h' for more information")
|
gPrintln("try 'gopeed -h' for more information")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -2,9 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/monkeyWie/gopeed/pkg/download"
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
"github.com/monkeyWie/gopeed/pkg/util"
|
"github.com/GopeedLab/gopeed/pkg/protocol/http"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@@ -40,7 +41,7 @@ func main() {
|
|||||||
}).
|
}).
|
||||||
Create(&base.Options{
|
Create(&base.Options{
|
||||||
Path: *args.dir,
|
Path: *args.dir,
|
||||||
Connections: *args.connections,
|
Extra: http.OptsExtra{Connections: *args.connections},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -55,10 +56,10 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func printProgress(task *download.Task, title string) {
|
func printProgress(task *download.Task, title string) {
|
||||||
rate := float64(task.Progress.Downloaded) / float64(task.Res.Size)
|
rate := float64(task.Progress.Downloaded) / float64(task.Meta.Res.Size)
|
||||||
completeWidth := int(progressWidth * rate)
|
completeWidth := int(progressWidth * rate)
|
||||||
speed := util.ByteFmt(task.Progress.Speed)
|
speed := util.ByteFmt(task.Progress.Speed)
|
||||||
totalSize := util.ByteFmt(task.Res.Size)
|
totalSize := util.ByteFmt(task.Meta.Res.Size)
|
||||||
sb.WriteString(fmt.Sprintf("\r%s [", title))
|
sb.WriteString(fmt.Sprintf("\r%s [", title))
|
||||||
for i := 0; i < progressWidth; i++ {
|
for i := 0; i < progressWidth; i++ {
|
||||||
if i < completeWidth {
|
if i < completeWidth {
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed banner.txt
|
||||||
|
var banner string
|
||||||
|
|
||||||
|
func Start(cfg *model.StartConfig) {
|
||||||
|
fmt.Println(banner)
|
||||||
|
srv, listener, err := rest.BuildServer(cfg)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
exist, _, err := rest.Downloader.GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
// Set default download dir to user download dir
|
||||||
|
userDir, err := os.UserHomeDir()
|
||||||
|
if err == nil {
|
||||||
|
downloadDir := filepath.Join(userDir, "Downloads")
|
||||||
|
rest.Downloader.PutConfig(&download.DownloaderStoreConfig{
|
||||||
|
DownloadDir: downloadDir,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
fmt.Printf("Server start success on http://%s\n", listener.Addr().String())
|
||||||
|
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-16
@@ -5,16 +5,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"github.com/GopeedLab/gopeed/cmd"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest"
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed banner.txt
|
|
||||||
var banner string
|
|
||||||
|
|
||||||
//go:embed dist/*
|
//go:embed dist/*
|
||||||
var dist embed.FS
|
var dist embed.FS
|
||||||
|
|
||||||
@@ -30,13 +25,5 @@ func main() {
|
|||||||
WebEnable: true,
|
WebEnable: true,
|
||||||
WebFS: sub,
|
WebFS: sub,
|
||||||
}
|
}
|
||||||
fmt.Println(banner)
|
cmd.Start(cfg)
|
||||||
srv, listener, err := rest.BuildServer(cfg)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
fmt.Printf("Server start success on http://%s\n", listener.Addr().String())
|
|
||||||
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
gopeed:
|
||||||
|
container_name: gopeed
|
||||||
|
ports:
|
||||||
|
- 9999:9999 # HTTP port (host:container)
|
||||||
|
image: liwei2633/gopeed
|
||||||
|
volumes:
|
||||||
|
- ~/Downloads:/download # replace with your prefer path
|
||||||
|
restart: unless-stopped
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
module github.com/monkeyWie/gopeed
|
module github.com/GopeedLab/gopeed
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/anacrolix/torrent v1.46.0
|
github.com/anacrolix/torrent v1.46.0
|
||||||
|
github.com/gorilla/handlers v1.5.1
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/matoous/go-nanoid/v2 v2.0.0
|
github.com/matoous/go-nanoid/v2 v2.0.0
|
||||||
go.etcd.io/bbolt v1.3.6
|
go.etcd.io/bbolt v1.3.6
|
||||||
@@ -39,7 +40,6 @@ require (
|
|||||||
github.com/felixge/httpsnoop v1.0.1 // indirect
|
github.com/felixge/httpsnoop v1.0.1 // indirect
|
||||||
github.com/google/btree v1.0.1 // indirect
|
github.com/google/btree v1.0.1 // indirect
|
||||||
github.com/google/uuid v1.3.0 // indirect
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
github.com/gorilla/handlers v1.5.1 // indirect
|
|
||||||
github.com/gorilla/websocket v1.4.2 // indirect
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
github.com/huandu/xstrings v1.3.2 // indirect
|
github.com/huandu/xstrings v1.3.2 // indirect
|
||||||
github.com/lispad/go-generics-tools v1.0.0 // indirect
|
github.com/lispad/go-generics-tools v1.0.0 // indirect
|
||||||
@@ -66,11 +66,8 @@ require (
|
|||||||
github.com/tidwall/btree v1.3.1 // indirect
|
github.com/tidwall/btree v1.3.1 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
|
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
|
||||||
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
|
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
|
||||||
golang.org/x/mobile v0.0.0-20220928052126-fa6bcb076835 // indirect
|
|
||||||
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 // indirect
|
|
||||||
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
|
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||||
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
|
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
|
||||||
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023 // indirect
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
)
|
)
|
||||||
@@ -6,19 +6,29 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller interface {
|
type Controller struct {
|
||||||
Touch(name string, size int64) (file *os.File, err error)
|
GetConfig func(v any) (bool, error)
|
||||||
|
FileController
|
||||||
//ContextDialer() (proxy.Dialer, error)
|
//ContextDialer() (proxy.Dialer, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultController struct {
|
type FileController interface {
|
||||||
|
Touch(name string, size int64) (file *os.File, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewController() *DefaultController {
|
type DefaultFileController struct {
|
||||||
return &DefaultController{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DefaultController) Touch(name string, size int64) (file *os.File, err error) {
|
func NewController() *Controller {
|
||||||
|
return &Controller{
|
||||||
|
FileController: &DefaultFileController{},
|
||||||
|
GetConfig: func(v any) (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DefaultFileController) Touch(name string, size int64) (file *os.File, err error) {
|
||||||
dir := filepath.Dir(name)
|
dir := filepath.Dir(name)
|
||||||
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
+48
-22
@@ -1,44 +1,70 @@
|
|||||||
package fetcher
|
package fetcher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/monkeyWie/gopeed/internal/controller"
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 对应协议的下载支持
|
// Fetcher defines the interface for a download protocol.
|
||||||
|
// One fetcher for each download task
|
||||||
type Fetcher interface {
|
type Fetcher interface {
|
||||||
Setup(ctl controller.Controller) error
|
// Name return the name of the protocol.
|
||||||
// 解析请求
|
Name() string
|
||||||
Resolve(req *base.Request) (res *base.Resource, err error)
|
|
||||||
// 创建任务
|
|
||||||
Create(res *base.Resource, opts *base.Options) (err error)
|
|
||||||
Start() (err error)
|
|
||||||
Pause() (err error)
|
|
||||||
Continue() (err error)
|
|
||||||
Close() (err error)
|
|
||||||
|
|
||||||
// 获取任务各个文件下载进度
|
Setup(ctl *controller.Controller) error
|
||||||
|
// Resolve resource info from request
|
||||||
|
Resolve(req *base.Request) error
|
||||||
|
// Create ready to download, but not started
|
||||||
|
Create(opts *base.Options) error
|
||||||
|
Start() error
|
||||||
|
Pause() error
|
||||||
|
Continue() error
|
||||||
|
Close() error
|
||||||
|
|
||||||
|
// Meta returns the meta information of the download.
|
||||||
|
Meta() *FetcherMeta
|
||||||
|
// Progress returns the progress of the download.
|
||||||
Progress() Progress
|
Progress() Progress
|
||||||
// 该方法会一直阻塞,直到任务下载结束
|
// Wait for the download to complete, this method will block until the download is done.
|
||||||
Wait() (err error)
|
Wait() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FetcherMeta defines the meta information of a fetcher.
|
||||||
|
type FetcherMeta struct {
|
||||||
|
Req *base.Request `json:"req"`
|
||||||
|
Res *base.Resource `json:"res"`
|
||||||
|
Opts *base.Options `json:"opts"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FetcherMeta) Filepath(file *base.FileInfo) string {
|
||||||
|
finalName := m.Opts.Name
|
||||||
|
if finalName == "" {
|
||||||
|
finalName = file.Name
|
||||||
|
}
|
||||||
|
return path.Join(m.Opts.Path, m.Res.RootDir, finalName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FetcherBuilder defines the interface for a fetcher builder.
|
||||||
type FetcherBuilder interface {
|
type FetcherBuilder interface {
|
||||||
|
// Schemes returns the schemes supported by the fetcher.
|
||||||
Schemes() []string
|
Schemes() []string
|
||||||
|
// Build returns a new fetcher.
|
||||||
Build() Fetcher
|
Build() Fetcher
|
||||||
|
|
||||||
// Store 存储任务
|
// Store fetcher
|
||||||
Store(fetcher Fetcher) (any, error)
|
Store(fetcher Fetcher) (any, error)
|
||||||
// Restore 恢复任务
|
// Restore fetcher
|
||||||
Restore() (v any, f func(res *base.Resource, opts *base.Options, v any) Fetcher)
|
Restore() (v any, f func(meta *FetcherMeta, v any) Fetcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultFetcher struct {
|
type DefaultFetcher struct {
|
||||||
Ctl controller.Controller
|
Ctl *controller.Controller
|
||||||
|
Meta *FetcherMeta
|
||||||
DoneCh chan error
|
DoneCh chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *DefaultFetcher) Setup(ctl controller.Controller) (err error) {
|
func (f *DefaultFetcher) Setup(ctl *controller.Controller) (err error) {
|
||||||
f.Ctl = ctl
|
f.Ctl = ctl
|
||||||
f.DoneCh = make(chan error, 1)
|
f.DoneCh = make(chan error, 1)
|
||||||
return
|
return
|
||||||
@@ -48,10 +74,10 @@ func (f *DefaultFetcher) Wait() (err error) {
|
|||||||
return <-f.DoneCh
|
return <-f.DoneCh
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取任务中各个文件的已下载字节数
|
// Progress is a map of the progress of each file in the torrent.
|
||||||
type Progress []int64
|
type Progress []int64
|
||||||
|
|
||||||
// TotalDownloaded 获取任务总下载字节数
|
// TotalDownloaded returns the total downloaded bytes.
|
||||||
func (p Progress) TotalDownloaded() int64 {
|
func (p Progress) TotalDownloaded() int64 {
|
||||||
total := int64(0)
|
total := int64(0)
|
||||||
for _, d := range p {
|
for _, d := range p {
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
Trackers []string `json:"trackers"`
|
||||||
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
package bt
|
package bt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
"github.com/anacrolix/torrent"
|
"github.com/anacrolix/torrent"
|
||||||
"github.com/anacrolix/torrent/metainfo"
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
"github.com/anacrolix/torrent/storage"
|
"github.com/anacrolix/torrent/storage"
|
||||||
"github.com/monkeyWie/gopeed/internal/controller"
|
"path"
|
||||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/util"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -17,11 +18,10 @@ import (
|
|||||||
var client *torrent.Client
|
var client *torrent.Client
|
||||||
|
|
||||||
type Fetcher struct {
|
type Fetcher struct {
|
||||||
Ctl controller.Controller
|
ctl *controller.Controller
|
||||||
|
|
||||||
torrent *torrent.Torrent
|
torrent *torrent.Torrent
|
||||||
res *base.Resource
|
meta *fetcher.FetcherMeta
|
||||||
opts *base.Options
|
|
||||||
|
|
||||||
ready atomic.Bool
|
ready atomic.Bool
|
||||||
progress fetcher.Progress
|
progress fetcher.Progress
|
||||||
@@ -29,8 +29,15 @@ type Fetcher struct {
|
|||||||
torrentPaths map[string]string
|
torrentPaths map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Setup(ctl controller.Controller) (err error) {
|
func (f *Fetcher) Name() string {
|
||||||
f.Ctl = ctl
|
return "bt"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Setup(ctl *controller.Controller) (err error) {
|
||||||
|
f.ctl = ctl
|
||||||
|
if f.meta == nil {
|
||||||
|
f.meta = &fetcher.FetcherMeta{}
|
||||||
|
}
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
cfg := torrent.NewDefaultClientConfig()
|
cfg := torrent.NewDefaultClientConfig()
|
||||||
@@ -62,52 +69,17 @@ func (f *Fetcher) Setup(ctl controller.Controller) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Wait() (err error) {
|
func (f *Fetcher) Resolve(req *base.Request) error {
|
||||||
for {
|
if err := f.addTorrent(req.URL); err != nil {
|
||||||
if f.ready.Load() {
|
return err
|
||||||
done := true
|
|
||||||
for _, selectIndex := range f.opts.SelectFiles {
|
|
||||||
file := f.torrent.Files()[selectIndex]
|
|
||||||
if file.BytesCompleted() < file.Length() {
|
|
||||||
done = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if done {
|
|
||||||
// remove unselected files
|
|
||||||
for i, file := range f.torrent.Files() {
|
|
||||||
selected := false
|
|
||||||
for _, selectIndex := range f.opts.SelectFiles {
|
|
||||||
if i == selectIndex {
|
|
||||||
selected = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !selected {
|
|
||||||
util.SafeRemove(filepath.Join(f.opts.Path, file.Path()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond * 500)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Resolve(req *base.Request) (res *base.Resource, err error) {
|
|
||||||
if err = f.addTorrent(req.URL, true); err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer f.torrent.Drop()
|
defer f.torrent.Drop()
|
||||||
res = &base.Resource{
|
res := &base.Resource{
|
||||||
Req: req,
|
|
||||||
Name: f.torrent.Name(),
|
Name: f.torrent.Name(),
|
||||||
Range: true,
|
Range: true,
|
||||||
|
RootDir: f.torrent.Name(),
|
||||||
Files: make([]*base.FileInfo, len(f.torrent.Files())),
|
Files: make([]*base.FileInfo, len(f.torrent.Files())),
|
||||||
Extra: map[string]any{
|
Hash: f.torrent.InfoHash().String(),
|
||||||
"infoHash": f.torrent.InfoHash().String(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
for i, file := range f.torrent.Files() {
|
for i, file := range f.torrent.Files() {
|
||||||
res.Files[i] = &base.FileInfo{
|
res.Files[i] = &base.FileInfo{
|
||||||
@@ -117,37 +89,39 @@ func (f *Fetcher) Resolve(req *base.Request) (res *base.Resource, err error) {
|
|||||||
}
|
}
|
||||||
res.Size += file.Length()
|
res.Size += file.Length()
|
||||||
}
|
}
|
||||||
return
|
f.meta.Req = req
|
||||||
|
f.meta.Res = res
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Create(res *base.Resource, opts *base.Options) (err error) {
|
func (f *Fetcher) Create(opts *base.Options) (err error) {
|
||||||
f.res = res
|
f.meta.Opts = opts
|
||||||
f.opts = opts
|
if len(opts.SelectFiles) == 0 {
|
||||||
if len(f.opts.SelectFiles) == 0 {
|
opts.SelectFiles = make([]int, 0)
|
||||||
f.opts.SelectFiles = make([]int, 0)
|
|
||||||
for i := range f.torrent.Files() {
|
for i := range f.torrent.Files() {
|
||||||
f.opts.SelectFiles = append(f.opts.SelectFiles, i)
|
opts.SelectFiles = append(opts.SelectFiles, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.opts.Path != "" {
|
if opts.Path != "" {
|
||||||
if extra, ok := res.Extra.(map[string]any); ok {
|
f.torrentPaths[f.meta.Res.Hash] = path.Join(f.meta.Opts.Path, f.meta.Res.RootDir)
|
||||||
f.torrentPaths[extra["infoHash"].(string)] = f.opts.Path
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
f.progress = make(fetcher.Progress, len(f.opts.SelectFiles))
|
f.progress = make(fetcher.Progress, len(f.meta.Opts.SelectFiles))
|
||||||
f.ready.Store(false)
|
f.ready.Store(false)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Start() (err error) {
|
func (f *Fetcher) Start() (err error) {
|
||||||
if err = f.addTorrent(f.res.Req.URL, false); err != nil {
|
if !f.ready.Load() {
|
||||||
|
if err = f.addTorrent(f.meta.Req.URL); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
files := f.torrent.Files()
|
files := f.torrent.Files()
|
||||||
if len(f.opts.SelectFiles) == len(files) {
|
if len(f.meta.Opts.SelectFiles) == len(files) {
|
||||||
f.torrent.DownloadAll()
|
f.torrent.DownloadAll()
|
||||||
} else {
|
} else {
|
||||||
for _, selectIndex := range f.opts.SelectFiles {
|
for _, selectIndex := range f.meta.Opts.SelectFiles {
|
||||||
file := files[selectIndex]
|
file := files[selectIndex]
|
||||||
file.Download()
|
file.Download()
|
||||||
}
|
}
|
||||||
@@ -170,19 +144,56 @@ func (f *Fetcher) Close() (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Wait() (err error) {
|
||||||
|
for {
|
||||||
|
if f.ready.Load() {
|
||||||
|
done := true
|
||||||
|
for _, selectIndex := range f.meta.Opts.SelectFiles {
|
||||||
|
file := f.torrent.Files()[selectIndex]
|
||||||
|
if file.BytesCompleted() < file.Length() {
|
||||||
|
done = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if done {
|
||||||
|
// remove unselected files
|
||||||
|
for i, file := range f.torrent.Files() {
|
||||||
|
selected := false
|
||||||
|
for _, selectIndex := range f.meta.Opts.SelectFiles {
|
||||||
|
if i == selectIndex {
|
||||||
|
selected = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !selected {
|
||||||
|
util.SafeRemove(filepath.Join(f.meta.Opts.Path, file.Path()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * 500)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||||
|
return f.meta
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Progress() fetcher.Progress {
|
func (f *Fetcher) Progress() fetcher.Progress {
|
||||||
if !f.ready.Load() {
|
if !f.ready.Load() {
|
||||||
return f.progress
|
return f.progress
|
||||||
}
|
}
|
||||||
for i := range f.progress {
|
for i := range f.progress {
|
||||||
selectIndex := f.opts.SelectFiles[i]
|
selectIndex := f.meta.Opts.SelectFiles[i]
|
||||||
file := f.torrent.Files()[selectIndex]
|
file := f.torrent.Files()[selectIndex]
|
||||||
f.progress[i] = file.BytesCompleted()
|
f.progress[i] = file.BytesCompleted()
|
||||||
}
|
}
|
||||||
return f.progress
|
return f.progress
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) addTorrent(url string, resolve bool) (err error) {
|
func (f *Fetcher) addTorrent(url string) (err error) {
|
||||||
schema := util.ParseSchema(url)
|
schema := util.ParseSchema(url)
|
||||||
if schema == "MAGNET" {
|
if schema == "MAGNET" {
|
||||||
f.torrent, err = client.AddMagnet(url)
|
f.torrent, err = client.AddMagnet(url)
|
||||||
@@ -192,10 +203,19 @@ func (f *Fetcher) addTorrent(url string, resolve bool) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
<-f.torrent.GotInfo()
|
var cfg config
|
||||||
if resolve {
|
exist, err := f.ctl.GetConfig(&cfg)
|
||||||
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if exist && len(cfg.Trackers) > 0 {
|
||||||
|
announceList := make([][]string, 0)
|
||||||
|
for _, tracker := range cfg.Trackers {
|
||||||
|
announceList = append(announceList, []string{tracker})
|
||||||
|
}
|
||||||
|
f.torrent.AddTrackers(announceList)
|
||||||
|
}
|
||||||
|
<-f.torrent.GotInfo()
|
||||||
f.ready.Store(true)
|
f.ready.Store(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -217,6 +237,10 @@ func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fb *FetcherBuilder) Restore() (v any, f func(res *base.Resource, opts *base.Options, v any) fetcher.Fetcher) {
|
func (fb *FetcherBuilder) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) {
|
||||||
return nil, nil
|
return nil, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher {
|
||||||
|
return &Fetcher{
|
||||||
|
meta: meta,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,25 @@
|
|||||||
package bt
|
package bt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/monkeyWie/gopeed/internal/controller"
|
"encoding/json"
|
||||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/test"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFetcher_Resolve_Torrent(t *testing.T) {
|
func TestFetcher_Resolve_Torrent(t *testing.T) {
|
||||||
fetcher := buildFetcher()
|
doResolve(t, buildFetcher())
|
||||||
res, err := fetcher.Resolve(&base.Request{
|
}
|
||||||
|
|
||||||
|
func TestFetcher_Config(t *testing.T) {
|
||||||
|
doResolve(t, buildConfigFetcher())
|
||||||
|
}
|
||||||
|
|
||||||
|
func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
|
||||||
|
err := fetcher.Resolve(&base.Request{
|
||||||
URL: "./testdata/ubuntu-22.04-live-server-amd64.iso.torrent",
|
URL: "./testdata/ubuntu-22.04-live-server-amd64.iso.torrent",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -18,22 +27,20 @@ func TestFetcher_Resolve_Torrent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
want := &base.Resource{
|
want := &base.Resource{
|
||||||
Req: res.Req,
|
|
||||||
Name: "ubuntu-22.04-live-server-amd64.iso",
|
Name: "ubuntu-22.04-live-server-amd64.iso",
|
||||||
Size: 1466714112,
|
Size: 1466714112,
|
||||||
Range: true,
|
Range: true,
|
||||||
|
RootDir: "ubuntu-22.04-live-server-amd64.iso",
|
||||||
Files: []*base.FileInfo{
|
Files: []*base.FileInfo{
|
||||||
{
|
{
|
||||||
Name: "ubuntu-22.04-live-server-amd64.iso",
|
Name: "ubuntu-22.04-live-server-amd64.iso",
|
||||||
Size: 1466714112,
|
Size: 1466714112,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Extra: map[string]any{
|
Hash: "8a55cfbd5ca5d11507364765936c4f9e55b253ed",
|
||||||
"infoHash": "8a55cfbd5ca5d11507364765936c4f9e55b253ed",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(want, res) {
|
if !reflect.DeepEqual(want, fetcher.Meta().Res) {
|
||||||
t.Errorf("Resolve() got = %v, want %v", res, want)
|
t.Errorf("Resolve() got = %v, want %v", fetcher.Meta().Res, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,3 +49,21 @@ func buildFetcher() fetcher.Fetcher {
|
|||||||
fetcher.Setup(controller.NewController())
|
fetcher.Setup(controller.NewController())
|
||||||
return fetcher
|
return fetcher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildConfigFetcher() fetcher.Fetcher {
|
||||||
|
fetcher := new(FetcherBuilder).Build()
|
||||||
|
newController := controller.NewController()
|
||||||
|
mockCfg := config{
|
||||||
|
Trackers: []string{
|
||||||
|
"udp://tracker.birkenwald.de:6969/announce",
|
||||||
|
"udp://tracker.bitsearch.to:1337/announce",
|
||||||
|
}}
|
||||||
|
newController.GetConfig = func(v any) (bool, error) {
|
||||||
|
if err := json.Unmarshal([]byte(test.ToJson(mockCfg)), v); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
fetcher.Setup(newController)
|
||||||
|
return fetcher
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
Connections int `json:"connections"`
|
||||||
|
}
|
||||||
@@ -4,9 +4,10 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
"github.com/monkeyWie/gopeed/pkg/util"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
fhttp "github.com/GopeedLab/gopeed/pkg/protocol/http"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
"mime"
|
||||||
@@ -15,9 +16,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,11 +33,26 @@ func (re *RequestError) Error() string {
|
|||||||
return fmt.Sprintf("http request fail,code:%d", re.Code)
|
return fmt.Sprintf("http request fail,code:%d", re.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Fetcher struct {
|
type chunk struct {
|
||||||
*fetcher.DefaultFetcher
|
Status base.Status
|
||||||
|
Begin int64
|
||||||
|
End int64
|
||||||
|
Downloaded int64
|
||||||
|
}
|
||||||
|
|
||||||
res *base.Resource
|
func newChunk(begin int64, end int64) *chunk {
|
||||||
opts *base.Options
|
return &chunk{
|
||||||
|
Status: base.DownloadStatusReady,
|
||||||
|
Begin: begin,
|
||||||
|
End: end,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Fetcher struct {
|
||||||
|
ctl *controller.Controller
|
||||||
|
doneCh chan error
|
||||||
|
|
||||||
|
meta *fetcher.FetcherMeta
|
||||||
status base.Status
|
status base.Status
|
||||||
chunks []*chunk
|
chunks []*chunk
|
||||||
|
|
||||||
@@ -48,22 +62,38 @@ type Fetcher struct {
|
|||||||
pauseCh chan interface{}
|
pauseCh chan interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
func (f *Fetcher) Name() string {
|
||||||
|
return "http"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Setup(ctl *controller.Controller) (err error) {
|
||||||
|
f.ctl = ctl
|
||||||
|
f.doneCh = make(chan error, 1)
|
||||||
|
f.pauseCh = make(chan interface{}, 1)
|
||||||
|
if f.meta == nil {
|
||||||
|
f.meta = &fetcher.FetcherMeta{}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Resolve(req *base.Request) error {
|
||||||
|
if err := base.ParseReqExtra[fhttp.ReqExtra](req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
httpReq, err := buildRequest(nil, req)
|
httpReq, err := buildRequest(nil, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
client := buildClient()
|
client := buildClient()
|
||||||
// 只访问一个字节,测试资源是否支持Range请求
|
// 只访问一个字节,测试资源是否支持Range请求
|
||||||
httpReq.Header.Set(base.HttpHeaderRange, fmt.Sprintf(base.HttpHeaderRangeFormat, 0, 0))
|
httpReq.Header.Set(base.HttpHeaderRange, fmt.Sprintf(base.HttpHeaderRangeFormat, 0, 0))
|
||||||
httpResp, err := client.Do(httpReq)
|
httpResp, err := client.Do(httpReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
// 拿到响应头就关闭,不用加defer
|
// 拿到响应头就关闭,不用加defer
|
||||||
httpResp.Body.Close()
|
httpResp.Body.Close()
|
||||||
res := &base.Resource{
|
res := &base.Resource{
|
||||||
Req: req,
|
|
||||||
Range: false,
|
Range: false,
|
||||||
Files: []*base.FileInfo{},
|
Files: []*base.FileInfo{},
|
||||||
}
|
}
|
||||||
@@ -75,7 +105,7 @@ func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
|||||||
if contentTotal != "" {
|
if contentTotal != "" {
|
||||||
parse, err := strconv.ParseInt(contentTotal, 10, 64)
|
parse, err := strconv.ParseInt(contentTotal, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
res.Size = parse
|
res.Size = parse
|
||||||
}
|
}
|
||||||
@@ -85,12 +115,12 @@ func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
|||||||
if contentLength != "" {
|
if contentLength != "" {
|
||||||
parse, err := strconv.ParseInt(contentLength, 10, 64)
|
parse, err := strconv.ParseInt(contentLength, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
res.Size = parse
|
res.Size = parse
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, NewRequestError(httpResp.StatusCode, httpResp.Status)
|
return NewRequestError(httpResp.StatusCode, httpResp.Status)
|
||||||
}
|
}
|
||||||
file := &base.FileInfo{
|
file := &base.FileInfo{
|
||||||
Size: res.Size,
|
Size: res.Size,
|
||||||
@@ -104,34 +134,58 @@ func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Get file filePath by URL
|
// Get file filePath by URL
|
||||||
if file.Name == "" && strings.Count(req.URL, "/") > 2 {
|
if file.Name == "" {
|
||||||
file.Name = filepath.Base(req.URL)
|
file.Name = path.Base(httpReq.URL.Path)
|
||||||
}
|
}
|
||||||
// unknown file filePath
|
// unknown file filePath
|
||||||
if file.Name == "" {
|
if file.Name == "" || file.Name == "/" || file.Name == "." {
|
||||||
file.Name = "unknown"
|
file.Name = "unknown"
|
||||||
}
|
}
|
||||||
res.Files = append(res.Files, file)
|
res.Files = append(res.Files, file)
|
||||||
res.Name = file.Name
|
res.Name = file.Name
|
||||||
return res, nil
|
f.meta.Req = req
|
||||||
|
f.meta.Res = res
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Create(res *base.Resource, opts *base.Options) error {
|
func (f *Fetcher) Create(opts *base.Options) error {
|
||||||
f.res = res
|
f.meta.Opts = opts
|
||||||
f.opts = opts
|
|
||||||
f.status = base.DownloadStatusReady
|
f.status = base.DownloadStatusReady
|
||||||
|
|
||||||
|
if err := base.ParseReqExtra[fhttp.ReqExtra](f.meta.Req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := base.ParseOptsExtra[fhttp.OptsExtra](f.meta.Opts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if opts.Extra == nil {
|
||||||
|
opts.Extra = &fhttp.OptsExtra{}
|
||||||
|
}
|
||||||
|
extra := opts.Extra.(*fhttp.OptsExtra)
|
||||||
|
if extra.Connections == 0 {
|
||||||
|
var cfg config
|
||||||
|
exist, err := f.ctl.GetConfig(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if exist {
|
||||||
|
extra.Connections = cfg.Connections
|
||||||
|
} else {
|
||||||
|
extra.Connections = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Start() (err error) {
|
func (f *Fetcher) Start() (err error) {
|
||||||
// 创建文件
|
// 创建文件
|
||||||
name := f.filepath()
|
name := f.filepath()
|
||||||
f.file, err = f.Ctl.Touch(name, f.res.Size)
|
f.file, err = f.ctl.Touch(name, f.meta.Res.Size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.status = base.DownloadStatusRunning
|
f.status = base.DownloadStatusRunning
|
||||||
f.chunks = splitChunk(f.res, f.opts)
|
f.chunks = f.splitChunk()
|
||||||
f.fetch()
|
f.fetch()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -169,6 +223,10 @@ func (f *Fetcher) Close() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||||
|
return f.meta
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Progress() fetcher.Progress {
|
func (f *Fetcher) Progress() fetcher.Progress {
|
||||||
p := make(fetcher.Progress, 0)
|
p := make(fetcher.Progress, 0)
|
||||||
if len(f.chunks) > 0 {
|
if len(f.chunks) > 0 {
|
||||||
@@ -181,14 +239,18 @@ func (f *Fetcher) Progress() fetcher.Progress {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) Wait() (err error) {
|
||||||
|
return <-f.doneCh
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher) filepath() string {
|
func (f *Fetcher) filepath() string {
|
||||||
return util.Filepath(f.opts.Path, f.res.Files[0].Name, f.opts.Name)
|
return f.meta.Filepath(f.meta.Res.Files[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) fetch() {
|
func (f *Fetcher) fetch() {
|
||||||
f.ctx, f.cancel = context.WithCancel(context.Background())
|
f.ctx, f.cancel = context.WithCancel(context.Background())
|
||||||
eg, _ := errgroup.WithContext(f.ctx)
|
eg, _ := errgroup.WithContext(f.ctx)
|
||||||
for i := 0; i < f.opts.Connections; i++ {
|
for i := 0; i < len(f.chunks); i++ {
|
||||||
i := i
|
i := i
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return f.fetchChunk(i)
|
return f.fetchChunk(i)
|
||||||
@@ -207,7 +269,7 @@ func (f *Fetcher) fetch() {
|
|||||||
} else {
|
} else {
|
||||||
f.status = base.DownloadStatusDone
|
f.status = base.DownloadStatusDone
|
||||||
}
|
}
|
||||||
f.DoneCh <- err
|
f.doneCh <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -215,7 +277,7 @@ func (f *Fetcher) fetch() {
|
|||||||
func (f *Fetcher) fetchChunk(index int) (err error) {
|
func (f *Fetcher) fetchChunk(index int) (err error) {
|
||||||
chunk := f.chunks[index]
|
chunk := f.chunks[index]
|
||||||
|
|
||||||
httpReq, err := buildRequest(f.ctx, f.res.Req)
|
httpReq, err := buildRequest(f.ctx, f.meta.Req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -237,7 +299,7 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
|
|||||||
resp *http.Response
|
resp *http.Response
|
||||||
retry bool
|
retry bool
|
||||||
)
|
)
|
||||||
if f.res.Range {
|
if f.meta.Res.Range {
|
||||||
httpReq.Header.Set(base.HttpHeaderRange,
|
httpReq.Header.Set(base.HttpHeaderRange,
|
||||||
fmt.Sprintf(base.HttpHeaderRangeFormat, chunk.Begin+chunk.Downloaded, chunk.End))
|
fmt.Sprintf(base.HttpHeaderRangeFormat, chunk.Begin+chunk.Downloaded, chunk.End))
|
||||||
} else {
|
} else {
|
||||||
@@ -291,7 +353,7 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isComplete := f.res.Range && chunk.Downloaded >= chunk.End-chunk.Begin+1
|
isComplete := f.meta.Res.Range && chunk.Downloaded >= chunk.End-chunk.Begin+1
|
||||||
if f.status == base.DownloadStatusPause && !isComplete {
|
if f.status == base.DownloadStatusPause && !isComplete {
|
||||||
chunk.Status = base.DownloadStatusPause
|
chunk.Status = base.DownloadStatusPause
|
||||||
return
|
return
|
||||||
@@ -301,19 +363,20 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitChunk(res *base.Resource, opts *base.Options) (chunks []*chunk) {
|
func (f *Fetcher) splitChunk() (chunks []*chunk) {
|
||||||
if res.Range {
|
if f.meta.Res.Range {
|
||||||
|
connections := f.meta.Opts.Extra.(*fhttp.OptsExtra).Connections
|
||||||
// 每个连接平均需要下载的分块大小
|
// 每个连接平均需要下载的分块大小
|
||||||
chunkSize := res.Size / int64(opts.Connections)
|
chunkSize := f.meta.Res.Size / int64(connections)
|
||||||
chunks = make([]*chunk, opts.Connections)
|
chunks = make([]*chunk, connections)
|
||||||
for i := 0; i < opts.Connections; i++ {
|
for i := 0; i < connections; i++ {
|
||||||
var (
|
var (
|
||||||
begin = chunkSize * int64(i)
|
begin = chunkSize * int64(i)
|
||||||
end int64
|
end int64
|
||||||
)
|
)
|
||||||
if i == opts.Connections-1 {
|
if i == connections-1 {
|
||||||
// 最后一个分块需要保证把文件下载完
|
// 最后一个分块需要保证把文件下载完
|
||||||
end = res.Size - 1
|
end = f.meta.Res.Size - 1
|
||||||
} else {
|
} else {
|
||||||
end = begin + chunkSize - 1
|
end = begin + chunkSize - 1
|
||||||
}
|
}
|
||||||
@@ -351,7 +414,7 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
|
|||||||
if req.Extra == nil {
|
if req.Extra == nil {
|
||||||
method = http.MethodGet
|
method = http.MethodGet
|
||||||
} else {
|
} else {
|
||||||
extra := req.Extra.(extra)
|
extra := req.Extra.(*fhttp.ReqExtra)
|
||||||
if extra.Method != "" {
|
if extra.Method != "" {
|
||||||
method = extra.Method
|
method = extra.Method
|
||||||
} else {
|
} else {
|
||||||
@@ -393,10 +456,7 @@ func (fb *FetcherBuilder) Schemes() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fb *FetcherBuilder) Build() fetcher.Fetcher {
|
func (fb *FetcherBuilder) Build() fetcher.Fetcher {
|
||||||
return &Fetcher{
|
return &Fetcher{}
|
||||||
DefaultFetcher: new(fetcher.DefaultFetcher),
|
|
||||||
pauseCh: make(chan interface{}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) {
|
func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) {
|
||||||
@@ -406,16 +466,17 @@ func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fb *FetcherBuilder) Restore() (v any, f func(res *base.Resource, opts *base.Options, v any) fetcher.Fetcher) {
|
func (fb *FetcherBuilder) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) {
|
||||||
return &fetcherData{}, func(res *base.Resource, opts *base.Options, v any) fetcher.Fetcher {
|
return &fetcherData{}, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher {
|
||||||
fd := v.(*fetcherData)
|
fd := v.(*fetcherData)
|
||||||
fb := &FetcherBuilder{}
|
fb := &FetcherBuilder{}
|
||||||
fetcher := fb.Build().(*Fetcher)
|
fetcher := fb.Build().(*Fetcher)
|
||||||
fetcher.res = res
|
fetcher.meta = meta
|
||||||
fetcher.opts = opts
|
|
||||||
fetcher.status = base.DownloadStatusPause
|
fetcher.status = base.DownloadStatusPause
|
||||||
|
base.ParseReqExtra[fhttp.ReqExtra](fetcher.meta.Req)
|
||||||
|
base.ParseOptsExtra[fhttp.OptsExtra](fetcher.meta.Opts)
|
||||||
if len(fd.Chunks) == 0 {
|
if len(fd.Chunks) == 0 {
|
||||||
fetcher.chunks = splitChunk(res, opts)
|
fetcher.chunks = fetcher.splitChunk()
|
||||||
} else {
|
} else {
|
||||||
fetcher.chunks = fd.Chunks
|
fetcher.chunks = fd.Chunks
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/monkeyWie/gopeed/internal/controller"
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
"github.com/monkeyWie/gopeed/internal/test"
|
"github.com/GopeedLab/gopeed/internal/test"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/protocol/http"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -41,15 +43,14 @@ func testResolve(startTestServer func() net.Listener, want *base.Resource, t *te
|
|||||||
listener := startTestServer()
|
listener := startTestServer()
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
fetcher := buildFetcher()
|
fetcher := buildFetcher()
|
||||||
res, err := fetcher.Resolve(&base.Request{
|
err := fetcher.Resolve(&base.Request{
|
||||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
res.Req = nil
|
if !reflect.DeepEqual(want, fetcher.meta.Res) {
|
||||||
if !reflect.DeepEqual(want, res) {
|
t.Errorf("Resolve() got = %v, want %v", fetcher.meta.Res, want)
|
||||||
t.Errorf("Resolve() got = %v, want %v", res, want)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +79,7 @@ func TestFetcher_DownloadChunked(t *testing.T) {
|
|||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
// chunked编码下载
|
// chunked编码下载
|
||||||
downloadNormal(listener, 1, t)
|
downloadNormal(listener, 1, t)
|
||||||
|
downloadNormal(listener, 2, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFetcher_DownloadPost(t *testing.T) {
|
func TestFetcher_DownloadPost(t *testing.T) {
|
||||||
@@ -111,28 +113,54 @@ func TestFetcher_DownloadResume(t *testing.T) {
|
|||||||
downloadResume(listener, 16, t)
|
downloadResume(listener, 16, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadReady(listener net.Listener, connections int, t *testing.T) (fetcher fetcher.Fetcher, res *base.Resource, opts *base.Options) {
|
func TestFetcher_Config(t *testing.T) {
|
||||||
fetcher = buildFetcher()
|
fetcher := doDownloadReady(buildConfigFetcher(), test.StartTestFileServer(), 0, t)
|
||||||
res, err := fetcher.Resolve(&base.Request{
|
err := fetcher.Start()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = fetcher.Wait()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := test.FileMd5(test.BuildFile)
|
||||||
|
got := test.FileMd5(test.DownloadFile)
|
||||||
|
if want != got {
|
||||||
|
t.Errorf("Download() got = %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadReady(listener net.Listener, connections int, t *testing.T) fetcher.Fetcher {
|
||||||
|
return doDownloadReady(buildFetcher(), listener, connections, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doDownloadReady(f fetcher.Fetcher, listener net.Listener, connections int, t *testing.T) fetcher.Fetcher {
|
||||||
|
err := f.Resolve(&base.Request{
|
||||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
opts = &base.Options{
|
var extra any = nil
|
||||||
Name: test.DownloadName,
|
if connections > 0 {
|
||||||
Path: test.Dir,
|
extra = http.OptsExtra{
|
||||||
Connections: connections,
|
Connections: connections,
|
||||||
}
|
}
|
||||||
err = fetcher.Create(res, opts)
|
}
|
||||||
|
opts := &base.Options{
|
||||||
|
Name: test.DownloadName,
|
||||||
|
Path: test.Dir,
|
||||||
|
Extra: extra,
|
||||||
|
}
|
||||||
|
err = f.Create(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadNormal(listener net.Listener, connections int, t *testing.T) {
|
func downloadNormal(listener net.Listener, connections int, t *testing.T) {
|
||||||
fetcher, _, _ := downloadReady(listener, connections, t)
|
fetcher := downloadReady(listener, connections, t)
|
||||||
err := fetcher.Start()
|
err := fetcher.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -149,8 +177,8 @@ func downloadNormal(listener net.Listener, connections int, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func downloadPost(listener net.Listener, connections int, t *testing.T) {
|
func downloadPost(listener net.Listener, connections int, t *testing.T) {
|
||||||
fetcher, _, _ := downloadReady(listener, connections, t)
|
fetcher := downloadReady(listener, connections, t)
|
||||||
fetcher.(*Fetcher).res.Req.Extra = extra{
|
fetcher.Meta().Req.Extra = &http.ReqExtra{
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Header: map[string]string{
|
Header: map[string]string{
|
||||||
"Authorization": "Bearer 123456",
|
"Authorization": "Bearer 123456",
|
||||||
@@ -173,7 +201,7 @@ func downloadPost(listener net.Listener, connections int, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func downloadContinue(listener net.Listener, connections int, t *testing.T) {
|
func downloadContinue(listener net.Listener, connections int, t *testing.T) {
|
||||||
fetcher, _, _ := downloadReady(listener, connections, t)
|
fetcher := downloadReady(listener, connections, t)
|
||||||
err := fetcher.Start()
|
err := fetcher.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -198,7 +226,7 @@ func downloadContinue(listener net.Listener, connections int, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func downloadError(listener net.Listener, connections int, t *testing.T) {
|
func downloadError(listener net.Listener, connections int, t *testing.T) {
|
||||||
fetcher, _, _ := downloadReady(listener, connections, t)
|
fetcher := downloadReady(listener, connections, t)
|
||||||
err := fetcher.Start()
|
err := fetcher.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -210,7 +238,7 @@ func downloadError(listener net.Listener, connections int, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func downloadResume(listener net.Listener, connections int, t *testing.T) {
|
func downloadResume(listener net.Listener, connections int, t *testing.T) {
|
||||||
fetcher, res, opts := downloadReady(listener, connections, t)
|
fetcher := downloadReady(listener, connections, t)
|
||||||
err := fetcher.Start()
|
err := fetcher.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -226,7 +254,7 @@ func downloadResume(listener net.Listener, connections int, t *testing.T) {
|
|||||||
fetcher.Pause()
|
fetcher.Pause()
|
||||||
|
|
||||||
_, f := fb.Restore()
|
_, f := fb.Restore()
|
||||||
f(res, opts, data)
|
f(fetcher.Meta(), data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -249,3 +277,19 @@ func buildFetcher() *Fetcher {
|
|||||||
fetcher.Setup(controller.NewController())
|
fetcher.Setup(controller.NewController())
|
||||||
return fetcher.(*Fetcher)
|
return fetcher.(*Fetcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildConfigFetcher() fetcher.Fetcher {
|
||||||
|
fetcher := new(FetcherBuilder).Build()
|
||||||
|
newController := controller.NewController()
|
||||||
|
mockCfg := config{
|
||||||
|
Connections: 16,
|
||||||
|
}
|
||||||
|
newController.GetConfig = func(v any) (bool, error) {
|
||||||
|
if err := json.Unmarshal([]byte(test.ToJson(mockCfg)), v); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
fetcher.Setup(newController)
|
||||||
|
return fetcher
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import "github.com/monkeyWie/gopeed/pkg/base"
|
|
||||||
|
|
||||||
type chunk struct {
|
|
||||||
Status base.Status
|
|
||||||
Begin int64
|
|
||||||
End int64
|
|
||||||
Downloaded int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func newChunk(begin int64, end int64) *chunk {
|
|
||||||
return &chunk{
|
|
||||||
Status: base.DownloadStatusReady,
|
|
||||||
Begin: begin,
|
|
||||||
End: end,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type extra struct {
|
|
||||||
Method string
|
|
||||||
Header map[string]string
|
|
||||||
Body string
|
|
||||||
}
|
|
||||||
@@ -49,3 +49,7 @@ func ToJson(v interface{}) string {
|
|||||||
buf, _ := json.Marshal(v)
|
buf, _ := json.Marshal(v)
|
||||||
return string(buf)
|
return string(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func JsonEqual(v1 any, v2 any) bool {
|
||||||
|
return ToJson(v1) == ToJson(v2)
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
NotFound = errors.New("not found")
|
||||||
|
BadParams = errors.New("bad params")
|
||||||
|
)
|
||||||
+47
-18
@@ -1,26 +1,25 @@
|
|||||||
package base
|
package base
|
||||||
|
|
||||||
// 下载请求
|
import (
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Request download request
|
||||||
type Request struct {
|
type Request struct {
|
||||||
// 下载链接
|
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
// 附加信息
|
Extra any `json:"extra"`
|
||||||
Extra interface{} `json:"extra"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 资源信息
|
// Resource download resource
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
Req *Request `json:"req"`
|
|
||||||
// 资源名称
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
// 资源总大小
|
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
// 是否支持断点下载
|
// is support range download
|
||||||
Range bool `json:"range"`
|
Range bool `json:"range"`
|
||||||
// 资源所包含的文件列表
|
RootDir string `json:"rootDir"`
|
||||||
|
// file list
|
||||||
Files []*FileInfo `json:"files"`
|
Files []*FileInfo `json:"files"`
|
||||||
// 附加信息
|
Hash string `json:"hash"`
|
||||||
Extra interface{} `json:"extra"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
@@ -29,14 +28,44 @@ type FileInfo struct {
|
|||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 下载选项
|
// Options for download
|
||||||
type Options struct {
|
type Options struct {
|
||||||
// 保存文件名
|
// Download file name
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
// 保存目录
|
// Download file path
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
// 并发连接数
|
// Select file indexes to download
|
||||||
Connections int `json:"connections"`
|
|
||||||
// 选择下载的文件下标列表
|
|
||||||
SelectFiles []int `json:"selectFiles"`
|
SelectFiles []int `json:"selectFiles"`
|
||||||
|
// Extra info for specific fetcher
|
||||||
|
Extra any `json:"extra"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseReqExtra[E any](req *Request) error {
|
||||||
|
if req.Extra == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, ok := req.Extra.(*E); ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var t E
|
||||||
|
if err := util.MapToStruct(req.Extra, &t); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Extra = &t
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseOptsExtra[E any](opts *Options) error {
|
||||||
|
if opts.Extra == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, ok := opts.Extra.(*E); ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var t E
|
||||||
|
if err := util.MapToStruct(opts.Extra, &t); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
opts.Extra = &t
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
+147
-77
@@ -2,12 +2,13 @@ package download
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/monkeyWie/gopeed/internal/controller"
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
"github.com/monkeyWie/gopeed/internal/protocol/bt"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/monkeyWie/gopeed/internal/protocol/http"
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
||||||
"github.com/monkeyWie/gopeed/pkg/util"
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -15,8 +16,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// task info bucket
|
||||||
bucketTask = "task"
|
bucketTask = "task"
|
||||||
|
// task download data bucket
|
||||||
bucketSave = "save"
|
bucketSave = "save"
|
||||||
|
// downloader config bucket
|
||||||
bucketConfig = "config"
|
bucketConfig = "config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,8 +36,8 @@ type Progress struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Downloader struct {
|
type Downloader struct {
|
||||||
controller *controller.DefaultController
|
|
||||||
fetchBuilders map[string]fetcher.FetcherBuilder
|
fetchBuilders map[string]fetcher.FetcherBuilder
|
||||||
|
fetcherMap map[string]fetcher.Fetcher
|
||||||
storage Storage
|
storage Storage
|
||||||
tasks []*Task
|
tasks []*Task
|
||||||
listener Listener
|
listener Listener
|
||||||
@@ -43,34 +47,6 @@ type Downloader struct {
|
|||||||
closed atomic.Bool
|
closed atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type DownloaderConfig struct {
|
|
||||||
Controller *controller.DefaultController
|
|
||||||
FetchBuilders []fetcher.FetcherBuilder
|
|
||||||
Storage Storage
|
|
||||||
|
|
||||||
// RefreshInterval time duration to refresh task progress(ms)
|
|
||||||
RefreshInterval int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *DownloaderConfig) Init() *DownloaderConfig {
|
|
||||||
if cfg.Controller == nil {
|
|
||||||
cfg.Controller = controller.NewController()
|
|
||||||
}
|
|
||||||
if len(cfg.FetchBuilders) == 0 {
|
|
||||||
cfg.FetchBuilders = []fetcher.FetcherBuilder{
|
|
||||||
new(http.FetcherBuilder),
|
|
||||||
new(bt.FetcherBuilder),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if cfg.Storage == nil {
|
|
||||||
cfg.Storage = NewMemStorage()
|
|
||||||
}
|
|
||||||
if cfg.RefreshInterval == 0 {
|
|
||||||
cfg.RefreshInterval = 1000
|
|
||||||
}
|
|
||||||
return cfg
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDownloader(cfg *DownloaderConfig) *Downloader {
|
func NewDownloader(cfg *DownloaderConfig) *Downloader {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
cfg = &DownloaderConfig{}
|
cfg = &DownloaderConfig{}
|
||||||
@@ -78,8 +54,9 @@ func NewDownloader(cfg *DownloaderConfig) *Downloader {
|
|||||||
cfg.Init()
|
cfg.Init()
|
||||||
|
|
||||||
d := &Downloader{
|
d := &Downloader{
|
||||||
controller: cfg.Controller,
|
|
||||||
fetchBuilders: make(map[string]fetcher.FetcherBuilder),
|
fetchBuilders: make(map[string]fetcher.FetcherBuilder),
|
||||||
|
fetcherMap: make(map[string]fetcher.Fetcher),
|
||||||
|
|
||||||
refreshInterval: cfg.RefreshInterval,
|
refreshInterval: cfg.RefreshInterval,
|
||||||
storage: cfg.Storage,
|
storage: cfg.Storage,
|
||||||
lock: &sync.Mutex{},
|
lock: &sync.Mutex{},
|
||||||
@@ -105,7 +82,13 @@ func (d *Downloader) Setup() error {
|
|||||||
if tasks == nil {
|
if tasks == nil {
|
||||||
tasks = make([]*Task, 0)
|
tasks = make([]*Task, 0)
|
||||||
} else {
|
} else {
|
||||||
for _, task := range tasks {
|
for i := len(tasks) - 1; i >= 0; i-- {
|
||||||
|
task := tasks[i]
|
||||||
|
// Remove broken tasks
|
||||||
|
if task.Meta == nil {
|
||||||
|
tasks = append(tasks[:i], tasks[i+1:]...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
initTask(task)
|
initTask(task)
|
||||||
if task.Status != base.DownloadStatusDone && task.Status != base.DownloadStatusError {
|
if task.Status != base.DownloadStatusDone && task.Status != base.DownloadStatusError {
|
||||||
task.Status = base.DownloadStatusPause
|
task.Status = base.DownloadStatusPause
|
||||||
@@ -114,8 +97,9 @@ func (d *Downloader) Setup() error {
|
|||||||
}
|
}
|
||||||
d.tasks = tasks
|
d.tasks = tasks
|
||||||
|
|
||||||
// 每秒统计一次下载速度
|
// 每个tick统计一次下载速度
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
for !d.closed.Load() {
|
for !d.closed.Load() {
|
||||||
if len(d.tasks) > 0 {
|
if len(d.tasks) > 0 {
|
||||||
for _, task := range d.tasks {
|
for _, task := range d.tasks {
|
||||||
@@ -134,7 +118,7 @@ func (d *Downloader) Setup() error {
|
|||||||
|
|
||||||
current := task.fetcher.Progress().TotalDownloaded()
|
current := task.fetcher.Progress().TotalDownloaded()
|
||||||
task.Progress.Used = task.timer.Used()
|
task.Progress.Used = task.timer.Used()
|
||||||
task.Progress.Speed = current - task.Progress.Downloaded
|
task.Progress.Speed = task.calcSpeed(current-task.Progress.Downloaded, float64(d.refreshInterval)/1000)
|
||||||
task.Progress.Downloaded = current
|
task.Progress.Downloaded = current
|
||||||
d.emit(EventKeyProgress, task)
|
d.emit(EventKeyProgress, task)
|
||||||
|
|
||||||
@@ -158,61 +142,116 @@ func (d *Downloader) Setup() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) buildFetcher(url string) (fetcher.FetcherBuilder, fetcher.Fetcher, error) {
|
func (d *Downloader) parseFb(url string) (fetcher.FetcherBuilder, error) {
|
||||||
schema := util.ParseSchema(url)
|
schema := util.ParseSchema(url)
|
||||||
fetchBuilder, ok := d.fetchBuilders[schema]
|
fetchBuilder, ok := d.fetchBuilders[schema]
|
||||||
if !ok {
|
if !ok {
|
||||||
fetchBuilder, ok = d.fetchBuilders[util.FileSchema]
|
fetchBuilder, ok = d.fetchBuilders[util.FileSchema]
|
||||||
}
|
}
|
||||||
if ok {
|
if ok {
|
||||||
fetcher := fetchBuilder.Build()
|
return fetchBuilder, nil
|
||||||
fetcher.Setup(d.controller)
|
|
||||||
return fetchBuilder, fetcher, nil
|
|
||||||
}
|
}
|
||||||
return nil, nil, errors.New("unsupported protocol")
|
return nil, errors.New("unsupported protocol")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) Resolve(req *base.Request) (*base.Resource, error) {
|
func (d *Downloader) setupFetcher(fetcher fetcher.Fetcher) {
|
||||||
_, fetcher, err := d.buildFetcher(req.URL)
|
ctl := controller.NewController()
|
||||||
|
ctl.GetConfig = func(v any) (bool, error) {
|
||||||
|
return d.getProtocolConfig(fetcher.Name(), v)
|
||||||
|
}
|
||||||
|
fetcher.Setup(ctl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) Resolve(req *base.Request) (rr *ResolveResult, err error) {
|
||||||
|
fb, err := d.parseFb(req.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
res, err := fetcher.Resolve(req)
|
fetcher := fb.Build()
|
||||||
|
d.setupFetcher(fetcher)
|
||||||
|
err = fetcher.Resolve(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
|
}
|
||||||
|
rrId, err := gonanoid.New()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.fetcherMap[rrId] = fetcher
|
||||||
|
rr = &ResolveResult{
|
||||||
|
ID: rrId,
|
||||||
|
Res: fetcher.Meta().Res,
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
func (d *Downloader) DirectCreate(req *base.Request, opts *base.Options) (taskId string, err error) {
|
||||||
|
rr, err := d.Resolve(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return d.Create(rr.ID, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) Create(res *base.Resource, opts *base.Options) (taskId string, err error) {
|
func (d *Downloader) Create(rrId string, opts *base.Options) (taskId string, err error) {
|
||||||
if opts == nil {
|
if opts == nil {
|
||||||
opts = &base.Options{}
|
opts = &base.Options{}
|
||||||
}
|
}
|
||||||
if !res.Range || opts.Connections < 1 {
|
fetcher, ok := d.fetcherMap[rrId]
|
||||||
opts.Connections = 1
|
if !ok {
|
||||||
|
return "", errors.New("invalid resource id")
|
||||||
}
|
}
|
||||||
|
meta := fetcher.Meta()
|
||||||
|
meta.Opts = opts
|
||||||
|
res := meta.Res
|
||||||
if len(opts.SelectFiles) == 0 {
|
if len(opts.SelectFiles) == 0 {
|
||||||
opts.SelectFiles = make([]int, 0)
|
opts.SelectFiles = make([]int, 0)
|
||||||
for i := range res.Files {
|
for i := range res.Files {
|
||||||
opts.SelectFiles = append(opts.SelectFiles, i)
|
opts.SelectFiles = append(opts.SelectFiles, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if opts.Path == "" {
|
||||||
|
exist, storeConfig, err := d.GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if exist {
|
||||||
|
opts.Path = storeConfig.DownloadDir
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fetcherBuilder, fetcher, err := d.buildFetcher(res.Req.URL)
|
fb, err := d.parseFb(fetcher.Meta().Req.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = fetcher.Create(res, opts)
|
|
||||||
|
// check if the download file is duplicated and rename it automatically.
|
||||||
|
files := res.Files
|
||||||
|
if res.RootDir != "" {
|
||||||
|
fullDirPath := path.Join(opts.Path, res.RootDir)
|
||||||
|
newName, err := util.CheckDuplicateAndRename(fullDirPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
res.RootDir = newName
|
||||||
|
} else if len(files) == 1 {
|
||||||
|
fullPath := meta.Filepath(files[0])
|
||||||
|
newName, err := util.CheckDuplicateAndRename(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
opts.Name = newName
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fetcher.Create(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
task := NewTask()
|
task := NewTask()
|
||||||
task.fetcherBuilder = fetcherBuilder
|
task.fetcherBuilder = fb
|
||||||
task.fetcher = fetcher
|
task.fetcher = fetcher
|
||||||
task.Res = res
|
task.Meta = fetcher.Meta()
|
||||||
task.Opts = opts
|
|
||||||
task.Progress = &Progress{}
|
task.Progress = &Progress{}
|
||||||
task.Status = base.DownloadStatusRunning
|
task.Status = base.DownloadStatusRunning
|
||||||
// calculate select files size
|
// calculate select files size
|
||||||
@@ -235,7 +274,6 @@ func (d *Downloader) Create(res *base.Resource, opts *base.Options) (taskId stri
|
|||||||
}
|
}
|
||||||
d.emit(EventKeyStart, task)
|
d.emit(EventKeyStart, task)
|
||||||
taskId = task.ID
|
taskId = task.ID
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := fetcher.Start()
|
err := fetcher.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -244,6 +282,7 @@ func (d *Downloader) Create(res *base.Resource, opts *base.Options) (taskId stri
|
|||||||
}
|
}
|
||||||
d.watch(task)
|
d.watch(task)
|
||||||
}()
|
}()
|
||||||
|
delete(d.fetcherMap, rrId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +312,7 @@ func (d *Downloader) Continue(id string) (err error) {
|
|||||||
defer task.lock.Unlock()
|
defer task.lock.Unlock()
|
||||||
if task.Status == base.DownloadStatusPause || task.Status == base.DownloadStatusError {
|
if task.Status == base.DownloadStatusPause || task.Status == base.DownloadStatusError {
|
||||||
task.Status = base.DownloadStatusRunning
|
task.Status = base.DownloadStatusRunning
|
||||||
|
task.Progress.Speed = 0
|
||||||
err = d.restoreFetcher(task)
|
err = d.restoreFetcher(task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -318,11 +358,17 @@ func (d *Downloader) Delete(id string, force bool) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if force {
|
if force {
|
||||||
names := make([]string, 0)
|
if task.Meta.Res.RootDir != "" {
|
||||||
for _, file := range task.Res.Files {
|
if err = os.RemoveAll(path.Join(task.Meta.Opts.Path, task.Meta.Res.RootDir)); err != nil {
|
||||||
names = append(names, util.Filepath(file.Path, file.Name, task.Opts.Name))
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, file := range task.Meta.Res.Files {
|
||||||
|
if err = util.SafeRemove(task.Meta.Filepath(file)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
util.SafeRemoveAll(task.Opts.Path, names)
|
|
||||||
}
|
}
|
||||||
d.emit(EventKeyDelete, task)
|
d.emit(EventKeyDelete, task)
|
||||||
task = nil
|
task = nil
|
||||||
@@ -380,14 +426,37 @@ func (d *Downloader) GetTasks() []*Task {
|
|||||||
return d.tasks
|
return d.tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) GetConfig(v any) (bool, error) {
|
func (d *Downloader) GetConfig() (bool, *DownloaderStoreConfig, error) {
|
||||||
return d.storage.Get(bucketConfig, "config", v)
|
var cfg DownloaderStoreConfig
|
||||||
|
exist, err := d.storage.Get(bucketConfig, "config", &cfg)
|
||||||
|
if err != nil {
|
||||||
|
return false, nil, err
|
||||||
|
}
|
||||||
|
return exist, &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) PutConfig(v any) error {
|
func (d *Downloader) PutConfig(v *DownloaderStoreConfig) error {
|
||||||
return d.storage.Put(bucketConfig, "config", v)
|
return d.storage.Put(bucketConfig, "config", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) getProtocolConfig(name string, v any) (bool, error) {
|
||||||
|
exist, cfg, err := d.GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if cfg.ProtocolConfig == nil || cfg.ProtocolConfig[name] == nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if err := util.MapToStruct(cfg.ProtocolConfig[name], v); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait task done
|
||||||
func (d *Downloader) watch(task *Task) {
|
func (d *Downloader) watch(task *Task) {
|
||||||
err := task.fetcher.Wait()
|
err := task.fetcher.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -397,7 +466,7 @@ func (d *Downloader) watch(task *Task) {
|
|||||||
task.Progress.Used = task.timer.Used()
|
task.Progress.Used = task.timer.Used()
|
||||||
if task.Size == 0 {
|
if task.Size == 0 {
|
||||||
task.Size = task.fetcher.Progress().TotalDownloaded()
|
task.Size = task.fetcher.Progress().TotalDownloaded()
|
||||||
task.Res.Size = task.Size
|
task.Meta.Res.Size = task.Size
|
||||||
}
|
}
|
||||||
used := task.Progress.Used / int64(time.Second)
|
used := task.Progress.Used / int64(time.Second)
|
||||||
if used == 0 {
|
if used == 0 {
|
||||||
@@ -414,30 +483,30 @@ func (d *Downloader) watch(task *Task) {
|
|||||||
|
|
||||||
func (d *Downloader) restoreFetcher(task *Task) error {
|
func (d *Downloader) restoreFetcher(task *Task) error {
|
||||||
if task.fetcher == nil {
|
if task.fetcher == nil {
|
||||||
fetcherBuilder, _, err := d.buildFetcher(task.Res.Req.URL)
|
fb, err := d.parseFb(task.Meta.Req.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
task.fetcherBuilder = fetcherBuilder
|
task.fetcherBuilder = fb
|
||||||
err = func() error {
|
err = func() error {
|
||||||
v, f := fetcherBuilder.Restore()
|
v, f := fb.Restore()
|
||||||
if v != nil {
|
if v != nil {
|
||||||
err := d.storage.Pop(bucketSave, task.ID, v)
|
err := d.storage.Pop(bucketSave, task.ID, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
task.fetcher = f(task.Res, task.Opts, v)
|
|
||||||
}
|
}
|
||||||
|
task.fetcher = f(task.Meta, v)
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
// TODO log
|
||||||
}
|
}
|
||||||
if task.fetcher == nil {
|
if task.fetcher == nil {
|
||||||
task.fetcher = fetcherBuilder.Build()
|
task.fetcher = fb.Build()
|
||||||
task.fetcher.Create(task.Res, task.Opts)
|
|
||||||
}
|
}
|
||||||
task.fetcher.Setup(d.controller)
|
d.setupFetcher(task.fetcher)
|
||||||
|
task.fetcher.Create(task.Meta.Opts)
|
||||||
go d.watch(task)
|
go d.watch(task)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -446,6 +515,7 @@ func (d *Downloader) restoreFetcher(task *Task) error {
|
|||||||
func initTask(task *Task) {
|
func initTask(task *Task) {
|
||||||
task.timer = &util.Timer{}
|
task.timer = &util.Timer{}
|
||||||
task.lock = &sync.Mutex{}
|
task.lock = &sync.Mutex{}
|
||||||
|
task.speedArr = make([]int64, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultDownloader = NewDownloader(nil)
|
var defaultDownloader = NewDownloader(nil)
|
||||||
@@ -466,7 +536,7 @@ func (b *boot) Extra(extra interface{}) *boot {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *boot) Resolve() (*base.Resource, error) {
|
func (b *boot) Resolve() (*ResolveResult, error) {
|
||||||
return defaultDownloader.Resolve(&base.Request{
|
return defaultDownloader.Resolve(&base.Request{
|
||||||
URL: b.url,
|
URL: b.url,
|
||||||
Extra: b.extra,
|
Extra: b.extra,
|
||||||
@@ -479,12 +549,12 @@ func (b *boot) Listener(listener Listener) *boot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *boot) Create(opts *base.Options) (string, error) {
|
func (b *boot) Create(opts *base.Options) (string, error) {
|
||||||
res, err := b.Resolve()
|
rr, err := b.Resolve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defaultDownloader.Listener(b.listener)
|
defaultDownloader.Listener(b.listener)
|
||||||
return defaultDownloader.Create(res, opts)
|
return defaultDownloader.Create(rr.ID, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Boot() *boot {
|
func Boot() *boot {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package download
|
package download
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/monkeyWie/gopeed/internal/test"
|
"github.com/GopeedLab/gopeed/internal/test"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/protocol/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -21,13 +22,12 @@ func TestDownloader_Resolve(t *testing.T) {
|
|||||||
req := &base.Request{
|
req := &base.Request{
|
||||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
}
|
}
|
||||||
res, err := downloader.Resolve(req)
|
rr, err := downloader.Resolve(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
want := &base.Resource{
|
want := &base.Resource{
|
||||||
Name: test.BuildName,
|
Name: test.BuildName,
|
||||||
Req: req,
|
|
||||||
Size: test.BuildSize,
|
Size: test.BuildSize,
|
||||||
Range: true,
|
Range: true,
|
||||||
Files: []*base.FileInfo{
|
Files: []*base.FileInfo{
|
||||||
@@ -38,8 +38,8 @@ func TestDownloader_Resolve(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(want, res) {
|
if !reflect.DeepEqual(want, rr.Res) {
|
||||||
t.Errorf("Resolve() got = %v, want %v", res, want)
|
t.Errorf("Resolve() got = %v, want %v", rr.Res, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ func TestDownloader_Create(t *testing.T) {
|
|||||||
req := &base.Request{
|
req := &base.Request{
|
||||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
}
|
}
|
||||||
res, err := downloader.Resolve(req)
|
rr, err := downloader.Resolve(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -67,10 +67,12 @@ func TestDownloader_Create(t *testing.T) {
|
|||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
_, err = downloader.Create(res, &base.Options{
|
_, err = downloader.Create(rr.ID, &base.Options{
|
||||||
Path: test.Dir,
|
Path: test.Dir,
|
||||||
Name: test.DownloadName,
|
Name: test.DownloadName,
|
||||||
|
Extra: http.OptsExtra{
|
||||||
Connections: 4,
|
Connections: 4,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -97,15 +99,17 @@ func TestDownloader_StoreAndRestore(t *testing.T) {
|
|||||||
req := &base.Request{
|
req := &base.Request{
|
||||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
}
|
}
|
||||||
res, err := downloader.Resolve(req)
|
rr, err := downloader.Resolve(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := downloader.Create(res, &base.Options{
|
id, err := downloader.Create(rr.ID, &base.Options{
|
||||||
Path: test.Dir,
|
Path: test.Dir,
|
||||||
Name: test.DownloadName,
|
Name: test.DownloadName,
|
||||||
|
Extra: http.OptsExtra{
|
||||||
Connections: 4,
|
Connections: 4,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -148,3 +152,58 @@ func TestDownloader_StoreAndRestore(t *testing.T) {
|
|||||||
|
|
||||||
downloader.Clear()
|
downloader.Clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDownloader_Protocol_Config(t *testing.T) {
|
||||||
|
downloader := NewDownloader(nil)
|
||||||
|
if err := downloader.Setup(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer downloader.Clear()
|
||||||
|
|
||||||
|
var httpCfg map[string]any
|
||||||
|
exits, err := downloader.getProtocolConfig("http", &httpCfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if exits {
|
||||||
|
t.Errorf("getProtocolConfig() got = %v, want %v", exits, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
storeCfg := &DownloaderStoreConfig{
|
||||||
|
RefreshInterval: 500,
|
||||||
|
DownloadDir: "./downloads",
|
||||||
|
ProtocolConfig: map[string]any{
|
||||||
|
"http": map[string]any{
|
||||||
|
"connections": 4,
|
||||||
|
},
|
||||||
|
"bt": map[string]any{
|
||||||
|
"trackerSubscribeUrls": []string{
|
||||||
|
"https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/best.txt",
|
||||||
|
},
|
||||||
|
"trackers": []string{
|
||||||
|
"udp://tracker.coppersurfer.tk:6969/announce",
|
||||||
|
"udp://tracker.leechers-paradise.org:6969/announce",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Extra: map[string]any{
|
||||||
|
"theme": "dark",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := downloader.PutConfig(storeCfg); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
exits, newStoreCfg, err := downloader.GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !exits {
|
||||||
|
t.Errorf("GetConfig() got = %v, want %v", exits, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !test.JsonEqual(storeCfg, newStoreCfg) {
|
||||||
|
t.Errorf("GetConfig() got = %v, want %v", test.ToJson(storeCfg), test.ToJson(newStoreCfg))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/protocol/bt"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/protocol/http"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ResolveResult struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Res *base.Resource `json:"res"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Meta *fetcher.FetcherMeta `json:"meta"`
|
||||||
|
Status base.Status `json:"status"`
|
||||||
|
Progress *Progress `json:"progress"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
|
||||||
|
fetcherBuilder fetcher.FetcherBuilder
|
||||||
|
fetcher fetcher.Fetcher
|
||||||
|
timer *util.Timer
|
||||||
|
lock *sync.Mutex
|
||||||
|
speedArr []int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTask() *Task {
|
||||||
|
id, err := gonanoid.New()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return &Task{
|
||||||
|
ID: id,
|
||||||
|
Status: base.DownloadStatusReady,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Task) clone() *Task {
|
||||||
|
return &Task{
|
||||||
|
ID: t.ID,
|
||||||
|
Meta: t.Meta,
|
||||||
|
Status: t.Status,
|
||||||
|
Progress: t.Progress,
|
||||||
|
Size: t.Size,
|
||||||
|
CreatedAt: t.CreatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Task) calcSpeed(downloaded int64, usedTime float64) int64 {
|
||||||
|
t.speedArr = append(t.speedArr, downloaded)
|
||||||
|
if len(t.speedArr) > 6 {
|
||||||
|
t.speedArr = t.speedArr[1:]
|
||||||
|
}
|
||||||
|
var total int64
|
||||||
|
for _, v := range t.speedArr {
|
||||||
|
total += v
|
||||||
|
}
|
||||||
|
return int64(float64(total) / float64(len(t.speedArr)) / usedTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
type DownloaderConfig struct {
|
||||||
|
Controller *controller.Controller
|
||||||
|
FetchBuilders []fetcher.FetcherBuilder
|
||||||
|
Storage Storage
|
||||||
|
|
||||||
|
*DownloaderStoreConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *DownloaderConfig) Init() *DownloaderConfig {
|
||||||
|
if cfg.Controller == nil {
|
||||||
|
cfg.Controller = controller.NewController()
|
||||||
|
}
|
||||||
|
if len(cfg.FetchBuilders) == 0 {
|
||||||
|
cfg.FetchBuilders = []fetcher.FetcherBuilder{
|
||||||
|
new(http.FetcherBuilder),
|
||||||
|
new(bt.FetcherBuilder),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cfg.Storage == nil {
|
||||||
|
cfg.Storage = NewMemStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.DownloaderStoreConfig == nil {
|
||||||
|
cfg.DownloaderStoreConfig = &DownloaderStoreConfig{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.RefreshInterval == 0 {
|
||||||
|
cfg.RefreshInterval = 350
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloaderStoreConfig is the config that can restore the downloader.
|
||||||
|
type DownloaderStoreConfig struct {
|
||||||
|
RefreshInterval int `json:"refreshInterval"` // RefreshInterval time duration to refresh task progress(ms)
|
||||||
|
DownloadDir string `json:"downloadDir"` // DownloadDir is the directory to save the downloaded files
|
||||||
|
ProtocolConfig map[string]any `json:"protocolConfig"` // ProtocolConfig is special config for each protocol
|
||||||
|
Extra map[string]any `json:"extra"` // Extra is the extra config
|
||||||
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package download
|
|
||||||
|
|
||||||
import (
|
|
||||||
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
||||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/util"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Task struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Res *base.Resource `json:"res"`
|
|
||||||
Opts *base.Options `json:"opts"`
|
|
||||||
Status base.Status `json:"status"`
|
|
||||||
Progress *Progress `json:"progress"`
|
|
||||||
Size int64 `json:"size"`
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
|
|
||||||
fetcherBuilder fetcher.FetcherBuilder
|
|
||||||
fetcher fetcher.Fetcher
|
|
||||||
timer *util.Timer
|
|
||||||
lock *sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTask() *Task {
|
|
||||||
id, err := gonanoid.New()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return &Task{
|
|
||||||
ID: id,
|
|
||||||
Status: base.DownloadStatusReady,
|
|
||||||
CreatedAt: time.Now(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Task) clone() *Task {
|
|
||||||
return &Task{
|
|
||||||
ID: t.ID,
|
|
||||||
Res: t.Res,
|
|
||||||
Opts: t.Opts,
|
|
||||||
Status: t.Status,
|
|
||||||
Progress: t.Progress,
|
|
||||||
Size: t.Size,
|
|
||||||
CreatedAt: t.CreatedAt,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
type ReqExtra struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Header map[string]string `json:"header"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OptsExtra struct {
|
||||||
|
Connections int `json:"connections"`
|
||||||
|
}
|
||||||
+121
-44
@@ -1,92 +1,119 @@
|
|||||||
package rest
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"io"
|
||||||
"github.com/monkeyWie/gopeed/pkg/download"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/util"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Resolve(w http.ResponseWriter, r *http.Request) {
|
func Resolve(w http.ResponseWriter, r *http.Request) {
|
||||||
var req base.Request
|
var req base.Request
|
||||||
if util.ReadJson(w, r, &req) {
|
if ReadJson(r, w, &req) {
|
||||||
resource, err := Downloader.Resolve(&req)
|
rr, err := Downloader.Resolve(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, model.NewResultWithData(resource))
|
WriteJson(w, model.NewOkResult(rr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTask(w http.ResponseWriter, r *http.Request) {
|
func CreateTask(w http.ResponseWriter, r *http.Request) {
|
||||||
var req model.CreateTask
|
var req model.CreateTask
|
||||||
if util.ReadJson(w, r, &req) {
|
if ReadJson(r, w, &req) {
|
||||||
if req.Opts.Connections == 0 {
|
var (
|
||||||
req.Opts.Connections = getServerConfig().Connections
|
taskId string
|
||||||
}
|
err error
|
||||||
if req.Opts.Path == "" {
|
)
|
||||||
req.Opts.Path = getServerConfig().DownloadDir
|
if req.Rid != "" {
|
||||||
}
|
taskId, err = Downloader.Create(req.Rid, req.Opts)
|
||||||
taskId, err := Downloader.Create(req.Res, req.Opts)
|
} else if req.Req != nil {
|
||||||
if err != nil {
|
taskId, err = Downloader.DirectCreate(req.Req, req.Opts)
|
||||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
} else {
|
||||||
|
WriteJson(w, model.NewErrorResult("param invalid: rid or req", model.CodeInvalidParam))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, model.NewResultWithData(taskId))
|
if err != nil {
|
||||||
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewOkResult(taskId))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PauseTask(w http.ResponseWriter, r *http.Request) {
|
func PauseTask(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
taskId := vars["id"]
|
taskId := vars["id"]
|
||||||
if err := Downloader.Pause(taskId); err != nil {
|
if taskId == "" {
|
||||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
WriteJson(w, model.NewErrorResult("param invalid: id", model.CodeInvalidParam))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, nil)
|
if err := Downloader.Pause(taskId); err != nil {
|
||||||
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewNilResult())
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContinueTask(w http.ResponseWriter, r *http.Request) {
|
func ContinueTask(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
taskId := vars["id"]
|
taskId := vars["id"]
|
||||||
if err := Downloader.Continue(taskId); err != nil {
|
if taskId == "" {
|
||||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
WriteJson(w, model.NewErrorResult("param invalid: id", model.CodeInvalidParam))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, nil)
|
if err := Downloader.Continue(taskId); err != nil {
|
||||||
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewNilResult())
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTask(w http.ResponseWriter, r *http.Request) {
|
func DeleteTask(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
taskId := vars["id"]
|
taskId := vars["id"]
|
||||||
force := r.FormValue("force")
|
force := r.FormValue("force")
|
||||||
if err := Downloader.Delete(taskId, force == "true"); err != nil {
|
if taskId == "" {
|
||||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
WriteJson(w, model.NewErrorResult("param invalid: id", model.CodeInvalidParam))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, nil)
|
if err := Downloader.Delete(taskId, force == "true"); err != nil {
|
||||||
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewNilResult())
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTask(w http.ResponseWriter, r *http.Request) {
|
func GetTask(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
taskId := vars["id"]
|
taskId := vars["id"]
|
||||||
task := Downloader.GetTask(taskId)
|
if taskId == "" {
|
||||||
if task == nil {
|
WriteJson(w, model.NewErrorResult("param invalid: id", model.CodeInvalidParam))
|
||||||
util.WriteJson(w, http.StatusNotFound, model.NewResultWithMsg("task not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, model.NewResultWithData(task))
|
task := Downloader.GetTask(taskId)
|
||||||
|
if task == nil {
|
||||||
|
WriteJson(w, model.NewErrorResult("task not found", model.CodeTaskNotFound))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewOkResult(task))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTasks(w http.ResponseWriter, r *http.Request) {
|
func GetTasks(w http.ResponseWriter, r *http.Request) {
|
||||||
status := r.FormValue("status")
|
status := r.FormValue("status")
|
||||||
if status == "" {
|
if status == "" {
|
||||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg("param is required: status"))
|
status = strings.Join([]string{
|
||||||
return
|
string(base.DownloadStatusReady),
|
||||||
|
string(base.DownloadStatusRunning),
|
||||||
|
string(base.DownloadStatusPause),
|
||||||
|
string(base.DownloadStatusError),
|
||||||
|
string(base.DownloadStatusDone),
|
||||||
|
}, ",")
|
||||||
}
|
}
|
||||||
statusArr := strings.Split(status, ",")
|
statusArr := strings.Split(status, ",")
|
||||||
tasks := Downloader.GetTasks()
|
tasks := Downloader.GetTasks()
|
||||||
@@ -98,26 +125,76 @@ func GetTasks(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, model.NewResultWithData(result))
|
WriteJson(w, model.NewOkResult(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
util.WriteJsonOk(w, model.NewResultWithData(getServerConfig()))
|
WriteJson(w, model.NewOkResult(getServerConfig()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutConfig(w http.ResponseWriter, r *http.Request) {
|
func PutConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
var cfg model.ServerConfig
|
var cfg download.DownloaderStoreConfig
|
||||||
if util.ReadJson(w, r, &cfg) {
|
if ReadJson(r, w, &cfg) {
|
||||||
if err := Downloader.PutConfig(&cfg); err != nil {
|
if err := Downloader.PutConfig(&cfg); err != nil {
|
||||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
util.WriteJsonOk(w, nil)
|
WriteJson(w, model.NewNilResult())
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServerConfig() *model.ServerConfig {
|
func DoProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
var cfg model.ServerConfig
|
target := r.Header.Get("X-Target-Uri")
|
||||||
Downloader.GetConfig(&cfg)
|
if target == "" {
|
||||||
return &cfg
|
writeError(w, "param invalid: X-Target-Uri")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetUrl, err := url.Parse(target)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.RequestURI = ""
|
||||||
|
r.URL = targetUrl
|
||||||
|
r.Host = targetUrl.Host
|
||||||
|
resp, err := http.DefaultClient.Do(r)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
for k, vv := range resp.Header {
|
||||||
|
for _, v := range vv {
|
||||||
|
w.Header().Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.WriteHeader(resp.StatusCode)
|
||||||
|
buf, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(buf)
|
||||||
|
//var reader io.ReadCloser
|
||||||
|
//switch resp.Header.Get("Content-Encoding") {
|
||||||
|
//case "gzip":
|
||||||
|
// reader, err = gzip.NewReader(resp.Body)
|
||||||
|
// defer reader.Close()
|
||||||
|
//default:
|
||||||
|
// reader = resp.Body
|
||||||
|
//}
|
||||||
|
//if _, err := io.Copy(w, resp.Body); err != nil {
|
||||||
|
// writeError(w, err.Error())
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, msg string) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getServerConfig() *download.DownloaderStoreConfig {
|
||||||
|
_, cfg, _ := Downloader.GetConfig()
|
||||||
|
return cfg
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
Protocol string `json:"protocol"`
|
||||||
|
Action string `json:"action"`
|
||||||
|
Params any `json:"params"`
|
||||||
|
}
|
||||||
@@ -1,21 +1,47 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
type RespCode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
CodeOk RespCode = 0
|
||||||
|
// CodeError is the common error code
|
||||||
|
CodeError RespCode = 1000
|
||||||
|
// CodeUnauthorized is the error code for unauthorized
|
||||||
|
CodeUnauthorized RespCode = 1001
|
||||||
|
// CodeInvalidParam is the error code for invalid parameter
|
||||||
|
CodeInvalidParam RespCode = 1002
|
||||||
|
// CodeTaskNotFound is the error code for task not found
|
||||||
|
CodeTaskNotFound RespCode = 2001
|
||||||
|
)
|
||||||
|
|
||||||
type Result[T any] struct {
|
type Result[T any] struct {
|
||||||
|
Code RespCode `json:"code"`
|
||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
Data T `json:"data"`
|
Data T `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResult[T any](msg string, data T) *Result[T] {
|
func NewOkResult[T any](data T) *Result[T] {
|
||||||
return &Result[T]{
|
return &Result[T]{
|
||||||
Msg: msg,
|
Code: CodeOk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResultWithMsg(msg string) *Result[any] {
|
func NewNilResult() *Result[any] {
|
||||||
return NewResult[any](msg, nil)
|
return &Result[any]{
|
||||||
|
Code: CodeOk,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResultWithData[T any](data T) *Result[T] {
|
func NewErrorResult(msg string, code ...RespCode) *Result[any] {
|
||||||
return NewResult[T]("", data)
|
// if code is not provided, the default code is CodeError
|
||||||
|
c := CodeError
|
||||||
|
if len(code) > 0 {
|
||||||
|
c = code[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Result[any]{
|
||||||
|
Code: c,
|
||||||
|
Msg: msg,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@ type StartConfig struct {
|
|||||||
Storage Storage `json:"storage"`
|
Storage Storage `json:"storage"`
|
||||||
StorageDir string `json:"storageDir"`
|
StorageDir string `json:"storageDir"`
|
||||||
RefreshInterval int `json:"refreshInterval"`
|
RefreshInterval int `json:"refreshInterval"`
|
||||||
|
ApiToken string `json:"apiToken"`
|
||||||
|
|
||||||
WebEnable bool
|
WebEnable bool
|
||||||
WebFS fs.FS
|
WebFS fs.FS
|
||||||
@@ -24,6 +25,9 @@ func (cfg *StartConfig) Init() *StartConfig {
|
|||||||
if cfg.Network == "" {
|
if cfg.Network == "" {
|
||||||
cfg.Network = "tcp"
|
cfg.Network = "tcp"
|
||||||
}
|
}
|
||||||
|
if cfg.Address == "" {
|
||||||
|
cfg.Address = "127.0.0.1:0"
|
||||||
|
}
|
||||||
if cfg.Storage == "" {
|
if cfg.Storage == "" {
|
||||||
cfg.Storage = StorageBolt
|
cfg.Storage = StorageBolt
|
||||||
}
|
}
|
||||||
@@ -32,27 +36,3 @@ func (cfg *StartConfig) Init() *StartConfig {
|
|||||||
}
|
}
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerConfig is present in the database
|
|
||||||
type ServerConfig struct {
|
|
||||||
Host string `json:"host"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
|
|
||||||
Connections int `json:"connections"`
|
|
||||||
DownloadDir string `json:"downloadDir"`
|
|
||||||
|
|
||||||
Extra map[string]any `json:"extra"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *ServerConfig) Init() *ServerConfig {
|
|
||||||
if cfg.Host == "" {
|
|
||||||
cfg.Host = "127.0.0.1"
|
|
||||||
}
|
|
||||||
if cfg.Port < 0 {
|
|
||||||
cfg.Port = 0
|
|
||||||
}
|
|
||||||
if cfg.Connections <= 0 {
|
|
||||||
cfg.Connections = 16
|
|
||||||
}
|
|
||||||
return cfg
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import "github.com/monkeyWie/gopeed/pkg/base"
|
import "github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
|
||||||
type CreateTask struct {
|
type CreateTask struct {
|
||||||
Res *base.Resource `json:"res"`
|
Rid string `json:"rid"`
|
||||||
|
Req *base.Request `json:"req"`
|
||||||
Opts *base.Options `json:"opts"`
|
Opts *base.Options `json:"opts"`
|
||||||
}
|
}
|
||||||
+33
-29
@@ -2,14 +2,14 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/monkeyWie/gopeed/pkg/download"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/util"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -63,22 +63,18 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
} else {
|
} else {
|
||||||
downloadCfg.Storage = download.NewMemStorage()
|
downloadCfg.Storage = download.NewMemStorage()
|
||||||
}
|
}
|
||||||
|
downloadCfg.Init()
|
||||||
downloadCfg.RefreshInterval = startCfg.RefreshInterval
|
downloadCfg.RefreshInterval = startCfg.RefreshInterval
|
||||||
Downloader = download.NewDownloader(downloadCfg.Init())
|
Downloader = download.NewDownloader(downloadCfg)
|
||||||
if err := Downloader.Setup(); err != nil {
|
if err := Downloader.Setup(); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
serverConfig := getAndPutServerConfig(startCfg)
|
|
||||||
var address string
|
|
||||||
if startCfg.Network == "unix" {
|
if startCfg.Network == "unix" {
|
||||||
address = startCfg.Address
|
util.SafeRemove(startCfg.Address)
|
||||||
util.SafeRemove(address)
|
|
||||||
} else {
|
|
||||||
address = net.JoinHostPort(serverConfig.Host, strconv.Itoa(serverConfig.Port))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err := net.Listen(startCfg.Network, address)
|
listener, err := net.Listen(startCfg.Network, startCfg.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -93,33 +89,41 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
r.Methods(http.MethodGet).Path("/api/v1/tasks").HandlerFunc(GetTasks)
|
r.Methods(http.MethodGet).Path("/api/v1/tasks").HandlerFunc(GetTasks)
|
||||||
r.Methods(http.MethodGet).Path("/api/v1/config").HandlerFunc(GetConfig)
|
r.Methods(http.MethodGet).Path("/api/v1/config").HandlerFunc(GetConfig)
|
||||||
r.Methods(http.MethodPut).Path("/api/v1/config").HandlerFunc(PutConfig)
|
r.Methods(http.MethodPut).Path("/api/v1/config").HandlerFunc(PutConfig)
|
||||||
|
r.Path("/api/v1/proxy").HandlerFunc(DoProxy)
|
||||||
if startCfg.WebEnable {
|
if startCfg.WebEnable {
|
||||||
r.PathPrefix("/").Handler(http.FileServer(http.FS(startCfg.WebFS)))
|
r.PathPrefix("/").Handler(http.FileServer(http.FS(startCfg.WebFS)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if startCfg.ApiToken != "" {
|
||||||
|
r.Use(func(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Header.Get("X-Api-Token") != startCfg.ApiToken {
|
||||||
|
WriteJson(w, model.NewErrorResult("invalid token", model.CodeUnauthorized))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
srv = &http.Server{Handler: handlers.CORS(
|
srv = &http.Server{Handler: handlers.CORS(
|
||||||
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
|
handlers.AllowedHeaders([]string{"Content-Type", "X-Api-Token", "X-Target-Uri"}),
|
||||||
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}),
|
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}),
|
||||||
handlers.AllowedOrigins([]string{"*"}),
|
handlers.AllowedOrigins([]string{"*"}),
|
||||||
)(r)}
|
)(r)}
|
||||||
return srv, listener, nil
|
return srv, listener, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAndPutServerConfig(startCfg *model.StartConfig) *model.ServerConfig {
|
func ReadJson(r *http.Request, w http.ResponseWriter, v any) bool {
|
||||||
var serverConfig model.ServerConfig
|
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
|
||||||
exist, err := Downloader.GetConfig(&serverConfig)
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
if err != nil {
|
return false
|
||||||
// TODO log
|
|
||||||
}
|
}
|
||||||
// first start
|
return true
|
||||||
if !exist {
|
|
||||||
if startCfg.Network == "tcp" {
|
|
||||||
host, port, _ := net.SplitHostPort(startCfg.Address)
|
|
||||||
serverConfig.Host = host
|
|
||||||
serverConfig.Port, _ = strconv.Atoi(port)
|
|
||||||
}
|
}
|
||||||
serverConfig.Init()
|
|
||||||
Downloader.PutConfig(serverConfig)
|
func WriteJson(w http.ResponseWriter, v any) {
|
||||||
}
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
return &serverConfig
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(v)
|
||||||
}
|
}
|
||||||
+124
-43
@@ -2,13 +2,14 @@ package rest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/monkeyWie/gopeed/internal/test"
|
"github.com/GopeedLab/gopeed/internal/test"
|
||||||
"github.com/monkeyWie/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/monkeyWie/gopeed/pkg/download"
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -22,10 +23,17 @@ import (
|
|||||||
var (
|
var (
|
||||||
restPort int
|
restPort int
|
||||||
|
|
||||||
taskReq = &base.Request{}
|
taskReq = &base.Request{
|
||||||
|
Extra: map[string]any{
|
||||||
|
"method": "",
|
||||||
|
"header": map[string]string{
|
||||||
|
"User-Agent": "gopeed",
|
||||||
|
},
|
||||||
|
"body": "",
|
||||||
|
},
|
||||||
|
}
|
||||||
taskRes = &base.Resource{
|
taskRes = &base.Resource{
|
||||||
Name: test.BuildName,
|
Name: test.BuildName,
|
||||||
Req: taskReq,
|
|
||||||
Size: test.BuildSize,
|
Size: test.BuildSize,
|
||||||
Range: true,
|
Range: true,
|
||||||
Files: []*base.FileInfo{
|
Files: []*base.FileInfo{
|
||||||
@@ -37,20 +45,22 @@ var (
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
createReq = &model.CreateTask{
|
createReq = &model.CreateTask{
|
||||||
Res: taskRes,
|
Req: taskReq,
|
||||||
Opts: &base.Options{
|
Opts: &base.Options{
|
||||||
Path: test.Dir,
|
Path: test.Dir,
|
||||||
Name: test.DownloadName,
|
Name: test.DownloadName,
|
||||||
Connections: 2,
|
Extra: map[string]any{
|
||||||
|
"connections": 2,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestResolve(t *testing.T) {
|
func TestResolve(t *testing.T) {
|
||||||
doTest(func() {
|
doTest(func() {
|
||||||
resp := httpRequestCheckOk[*base.Resource](http.MethodPost, "/api/v1/resolve", taskReq)
|
resp := httpRequestCheckOk[*download.ResolveResult](http.MethodPost, "/api/v1/resolve", taskReq)
|
||||||
if !reflect.DeepEqual(taskRes, resp) {
|
if !test.JsonEqual(taskRes, resp.Res) {
|
||||||
t.Errorf("Resolve() got = %v, want %v", resp, taskRes)
|
t.Errorf("Resolve() got = %v, want %v", test.ToJson(resp.Res), test.ToJson(taskRes))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -137,19 +147,20 @@ func TestPauseAndContinueTask(t *testing.T) {
|
|||||||
func TestDeleteTask(t *testing.T) {
|
func TestDeleteTask(t *testing.T) {
|
||||||
doTest(func() {
|
doTest(func() {
|
||||||
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
||||||
|
time.Sleep(time.Millisecond * 200)
|
||||||
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId, nil)
|
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId, nil)
|
||||||
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
||||||
checkCode(code, http.StatusNotFound)
|
checkCode(code, model.CodeTaskNotFound)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteTaskForce(t *testing.T) {
|
func TestDeleteTaskForce(t *testing.T) {
|
||||||
doTest(func() {
|
doTest(func() {
|
||||||
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
||||||
time.Sleep(time.Millisecond * 500)
|
time.Sleep(time.Millisecond * 200)
|
||||||
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId+"?force=true", nil)
|
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId+"?force=true", nil)
|
||||||
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
||||||
checkCode(code, http.StatusNotFound)
|
checkCode(code, model.CodeTaskNotFound)
|
||||||
if _, err := os.Stat(test.DownloadFile); !errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(test.DownloadFile); !errors.Is(err, os.ErrNotExist) {
|
||||||
t.Errorf("DeleteTaskForce() got = %v, want %v", err, os.ErrNotExist)
|
t.Errorf("DeleteTaskForce() got = %v, want %v", err, os.ErrNotExist)
|
||||||
}
|
}
|
||||||
@@ -168,8 +179,7 @@ func TestGetTasks(t *testing.T) {
|
|||||||
|
|
||||||
httpRequestCheckOk[string](http.MethodPost, fmt.Sprintf("/api/v1/tasks?status=%s,%s",
|
httpRequestCheckOk[string](http.MethodPost, fmt.Sprintf("/api/v1/tasks?status=%s,%s",
|
||||||
base.DownloadStatusReady, base.DownloadStatusRunning), createReq)
|
base.DownloadStatusReady, base.DownloadStatusRunning), createReq)
|
||||||
code, _ := httpRequest[[]*download.Task](http.MethodGet, "/api/v1/tasks", nil)
|
httpRequestCheckOk[[]*download.Task](http.MethodGet, "/api/v1/tasks", nil)
|
||||||
checkCode(code, http.StatusBadRequest)
|
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
r := httpRequestCheckOk[[]*download.Task](http.MethodGet, fmt.Sprintf("/api/v1/tasks?status=%s",
|
r := httpRequestCheckOk[[]*download.Task](http.MethodGet, fmt.Sprintf("/api/v1/tasks?status=%s",
|
||||||
@@ -187,26 +197,77 @@ func TestGetTasks(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetAndPutConfig(t *testing.T) {
|
func TestGetAndPutConfig(t *testing.T) {
|
||||||
doTest(func() {
|
doTest(func() {
|
||||||
cfg := httpRequestCheckOk[*model.ServerConfig](http.MethodGet, "/api/v1/config", nil)
|
cfg := httpRequestCheckOk[*download.DownloaderStoreConfig](http.MethodGet, "/api/v1/config", nil)
|
||||||
|
|
||||||
cfg.Port = 8888
|
|
||||||
cfg.Connections = 32
|
|
||||||
cfg.DownloadDir = "./download"
|
cfg.DownloadDir = "./download"
|
||||||
cfg.Extra = map[string]any{
|
cfg.Extra = map[string]any{
|
||||||
|
"serverConfig": &Config{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 8080,
|
||||||
|
},
|
||||||
"theme": "dark",
|
"theme": "dark",
|
||||||
}
|
}
|
||||||
httpRequestCheckOk[any](http.MethodPut, "/api/v1/config", cfg)
|
httpRequestCheckOk[any](http.MethodPut, "/api/v1/config", cfg)
|
||||||
|
|
||||||
newCfg := httpRequestCheckOk[*model.ServerConfig](http.MethodGet, "/api/v1/config", nil)
|
newCfg := httpRequestCheckOk[*download.DownloaderStoreConfig](http.MethodGet, "/api/v1/config", nil)
|
||||||
if !reflect.DeepEqual(newCfg, cfg) {
|
if !test.JsonEqual(cfg, newCfg) {
|
||||||
t.Errorf("GetAndPutConfig() got = %v, want %v", newCfg, cfg)
|
t.Errorf("GetAndPutConfig() got = %v, want %v", test.ToJson(newCfg), test.ToJson(cfg))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDoProxy(t *testing.T) {
|
||||||
|
doTest(func() {
|
||||||
|
code, respBody := doHttpRequest0(http.MethodGet, "/api/v1/proxy", map[string]string{
|
||||||
|
"X-Target-Uri": "https://github.com/GopeedLab/gopeed/raw/695da7ea87d2b455552b709d3cb4d7879484d4d1/README.md",
|
||||||
|
}, nil)
|
||||||
|
if code != http.StatusOK {
|
||||||
|
t.Errorf("DoProxy() got = %v, want %v", code, http.StatusOK)
|
||||||
|
}
|
||||||
|
want := "4ee193b676f1ebb2ad810e016350d52a"
|
||||||
|
got := fmt.Sprintf("%x", md5.Sum(respBody))
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("DoProxy() got = %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
doTest(func() {
|
||||||
|
code, _ := doHttpRequest0(http.MethodGet, "/api/v1/proxy", map[string]string{
|
||||||
|
"X-Target-Uri": "https://github.com/GopeedLab/gopeed/raw/695da7ea87d2b455552b709d3cb4d7879484d4d1/NOT_FOUND",
|
||||||
|
}, nil)
|
||||||
|
if code != http.StatusNotFound {
|
||||||
|
t.Errorf("DoProxy() got = %v, want %v", code, http.StatusNotFound)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthorization(t *testing.T) {
|
||||||
|
var cfg = &model.StartConfig{}
|
||||||
|
cfg.Init()
|
||||||
|
cfg.ApiToken = "123456"
|
||||||
|
fileListener := doStart(cfg)
|
||||||
|
defer func() {
|
||||||
|
if err := fileListener.Close(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
Stop()
|
||||||
|
}()
|
||||||
|
|
||||||
|
code, _ := doHttpRequest[any](http.MethodGet, "/api/v1/config", nil, nil)
|
||||||
|
checkCode(code, model.CodeUnauthorized)
|
||||||
|
|
||||||
|
code, _ = doHttpRequest[any](http.MethodGet, "/api/v1/config", map[string]string{
|
||||||
|
"X-Api-Token": cfg.ApiToken,
|
||||||
|
}, nil)
|
||||||
|
checkCode(code, model.CodeOk)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func doTest(handler func()) {
|
func doTest(handler func()) {
|
||||||
testFunc := func(storage model.Storage) {
|
testFunc := func(storage model.Storage) {
|
||||||
fileListener := doStart(storage)
|
var cfg = &model.StartConfig{}
|
||||||
|
cfg.Init()
|
||||||
|
cfg.Storage = storage
|
||||||
|
fileListener := doStart(cfg)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := fileListener.Close(); err != nil {
|
if err := fileListener.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -221,10 +282,8 @@ func doTest(handler func()) {
|
|||||||
testFunc(model.StorageBolt)
|
testFunc(model.StorageBolt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func doStart(storage model.Storage) net.Listener {
|
func doStart(cfg *model.StartConfig) net.Listener {
|
||||||
port, err := Start((&model.StartConfig{
|
port, err := Start(cfg)
|
||||||
Storage: storage,
|
|
||||||
}).Init())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -232,42 +291,64 @@ func doStart(storage model.Storage) net.Listener {
|
|||||||
return test.StartTestFileServer()
|
return test.StartTestFileServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpRequest[T any](method string, path string, req any) (int, *model.Result[T]) {
|
func doHttpRequest0(method string, path string, headers map[string]string, body any) (int, []byte) {
|
||||||
var body io.Reader
|
var reader io.Reader
|
||||||
if req != nil {
|
if body != nil {
|
||||||
buf, _ := json.Marshal(req)
|
buf, _ := json.Marshal(body)
|
||||||
body = bytes.NewBuffer(buf)
|
reader = bytes.NewBuffer(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest(method, fmt.Sprintf("http://127.0.0.1:%d%s", restPort, path), body)
|
request, err := http.NewRequest(method, fmt.Sprintf("http://127.0.0.1:%d%s", restPort, path), reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
if headers != nil {
|
||||||
|
for k, v := range headers {
|
||||||
|
request.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
response, err := http.DefaultClient.Do(request)
|
response, err := http.DefaultClient.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
respBody, err := io.ReadAll(response.Body)
|
||||||
var r model.Result[T]
|
if err != nil {
|
||||||
if err := json.NewDecoder(response.Body).Decode(&r); err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return response.StatusCode, &r
|
|
||||||
|
return response.StatusCode, respBody
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpRequestCheckOk[T any](method string, path string, req any) T {
|
func doHttpRequest[T any](method string, path string, headers map[string]string, body any) (int, *model.Result[T]) {
|
||||||
code, result := httpRequest[T](method, path, req)
|
statusCode, respBody := doHttpRequest0(method, path, headers, body)
|
||||||
|
if statusCode != http.StatusOK {
|
||||||
|
panic(fmt.Sprintf("http request failed, status code: %d", statusCode))
|
||||||
|
}
|
||||||
|
|
||||||
|
var r model.Result[T]
|
||||||
|
if err := json.Unmarshal(respBody, &r); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return int(r.Code), &r
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpRequest[T any](method string, path string, body any) (int, *model.Result[T]) {
|
||||||
|
return doHttpRequest[T](method, path, nil, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpRequestCheckOk[T any](method string, path string, body any) T {
|
||||||
|
code, result := httpRequest[T](method, path, body)
|
||||||
checkOk(code)
|
checkOk(code)
|
||||||
return result.Data
|
return result.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkOk(code int) {
|
func checkOk(code int) {
|
||||||
checkCode(code, 200)
|
checkCode(code, model.CodeOk)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkCode(code int, exceptCode int) {
|
func checkCode(code int, exceptCode model.RespCode) {
|
||||||
if code != exceptCode {
|
if code != int(exceptCode) {
|
||||||
panic(fmt.Sprintf("code got = %d, want %d", code, exceptCode))
|
panic(fmt.Sprintf("code got = %d, want %d", code, exceptCode))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ReadJson(w http.ResponseWriter, r *http.Request, v any) bool {
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
|
|
||||||
WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteJsonOk(w http.ResponseWriter, v any) {
|
|
||||||
WriteJson(w, 200, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteJson(w http.ResponseWriter, code int, v any) {
|
|
||||||
w.WriteHeader(code)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
json.NewEncoder(w).Encode(v)
|
|
||||||
}
|
|
||||||
Loaded 50 of 159 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user