44 "bytes"
55 "context"
66 "fmt"
7+ "github.com/linuxsuren/http-downloader/pkg/exec"
78 "net/http"
89 "net/url"
910 sysos "os"
@@ -126,7 +127,7 @@ func (o *downloadOption) fetch() (err error) {
126127 go func () {
127128 // no need to handle the error due to this is a background task
128129 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 {} ))
130131 }
131132 o .wait .Done ()
132133 }()
@@ -169,7 +170,11 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
169170
170171 targetURL := args [0 ]
171172 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://" ) {
173178 ins := & installer.Installer {
174179 Provider : o .Provider ,
175180 OS : o .OS ,
@@ -188,10 +193,6 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
188193 }
189194 o .URL = targetURL
190195
191- if o .ProxyGitHub != "" {
192- o .URL = strings .Replace (o .URL , "github.com" , fmt .Sprintf ("%s/github.com" , o .ProxyGitHub ), 1 )
193- }
194-
195196 if o .Output == "" {
196197 var urlObj * url.URL
197198 if urlObj , err = url .Parse (o .URL ); err == nil {
@@ -246,6 +247,11 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
246247 return
247248 }
248249
250+ if strings .HasPrefix (o .URL , "magnet:?" ) {
251+ err = downloadMagnetFile (o .ProxyGitHub , o .URL )
252+ return
253+ }
254+
249255 cmd .Printf ("start to download from %s\n " , o .URL )
250256 if o .Thread <= 1 {
251257 downloader := & net.ContinueDownloader {}
@@ -262,3 +268,26 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
262268 }
263269 return
264270}
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