4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
+ "github.com/linuxsuren/http-downloader/pkg/exec"
7
8
"net/http"
8
9
"net/url"
9
10
sysos "os"
@@ -126,7 +127,7 @@ func (o *downloadOption) fetch() (err error) {
126
127
go func () {
127
128
// no need to handle the error due to this is a background task
128
129
if o .fetcher != nil {
129
- err = o .fetcher .FetchLatestRepo (o .Provider , installer .ConfigBranch , bytes .NewBuffer (nil ))
130
+ err = o .fetcher .FetchLatestRepo (o .Provider , installer .ConfigBranch , bytes .NewBuffer ([] byte {} ))
130
131
}
131
132
o .wait .Done ()
132
133
}()
@@ -169,7 +170,11 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
169
170
170
171
targetURL := args [0 ]
171
172
o .Package = & installer.HDConfig {}
172
- if ! strings .HasPrefix (targetURL , "http://" ) && ! strings .HasPrefix (targetURL , "https://" ) {
173
+ if strings .HasPrefix (targetURL , "magnet:?" ) {
174
+ // download via external tool
175
+ o .URL = targetURL
176
+ return
177
+ } else if ! strings .HasPrefix (targetURL , "http://" ) && ! strings .HasPrefix (targetURL , "https://" ) {
173
178
ins := & installer.Installer {
174
179
Provider : o .Provider ,
175
180
OS : o .OS ,
@@ -188,10 +193,6 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
188
193
}
189
194
o .URL = targetURL
190
195
191
- if o .ProxyGitHub != "" {
192
- o .URL = strings .Replace (o .URL , "github.com" , fmt .Sprintf ("%s/github.com" , o .ProxyGitHub ), 1 )
193
- }
194
-
195
196
if o .Output == "" {
196
197
var urlObj * url.URL
197
198
if urlObj , err = url .Parse (o .URL ); err == nil {
@@ -246,6 +247,11 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
246
247
return
247
248
}
248
249
250
+ if strings .HasPrefix (o .URL , "magnet:?" ) {
251
+ err = downloadMagnetFile (o .ProxyGitHub , o .URL )
252
+ return
253
+ }
254
+
249
255
cmd .Printf ("start to download from %s\n " , o .URL )
250
256
if o .Thread <= 1 {
251
257
downloader := & net.ContinueDownloader {}
@@ -262,3 +268,26 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
262
268
}
263
269
return
264
270
}
271
+
272
+ func downloadMagnetFile (proxyGitHub , target string ) (err error ) {
273
+ targetCmd := "gotorrent"
274
+ execer := exec.DefaultExecer {}
275
+ is := installer.Installer {
276
+ Provider : "github" ,
277
+ Execer : execer ,
278
+ ProxyGitHub : proxyGitHub ,
279
+ }
280
+ if err = is .CheckDepAndInstall (map [string ]string {
281
+ targetCmd : "linuxsuren/gotorrent" ,
282
+ }); err != nil {
283
+ return
284
+ }
285
+
286
+ var targetBinary string
287
+ if targetBinary , err = execer .LookPath (targetCmd ); err == nil {
288
+ sysCallArgs := []string {targetCmd }
289
+ sysCallArgs = append (sysCallArgs , []string {"download" , target }... )
290
+ err = execer .SystemCall (targetBinary , sysCallArgs , sysos .Environ ())
291
+ }
292
+ return
293
+ }
0 commit comments