Skip to content

Commit

Permalink
Add support for torrent files over HTTP(S)
Browse files Browse the repository at this point in the history
Closes #6.
  • Loading branch information
FIGBERT committed Aug 14, 2022
1 parent 9fbe3dd commit fd55a97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
38 changes: 28 additions & 10 deletions internal/torrent/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package torrent

import (
"net/http"
"strings"
"time"

"github.com/smmr-software/mabel/internal/list"
Expand All @@ -17,18 +19,34 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

// addFromFile takes a metainfo (.torrent) file and adds the torrent to
// the client. If it is a new torrent, the torrent info is added to the
// addMetaInfoFile takes a metainfo (.torrent) file and adds the torrent
// to the client. The file can either be local or accessible over
// HTTP(S). If it is a new torrent, the torrent info is added to the
// Bubbles list.
func addFromFile(input *string, dir *storage.ClientImpl, client *torrent.Client, l *clist.Model, theme *styles.ColorTheme) (tea.Cmd, error) {
path, err := home.Expand(*input)
if err != nil {
return nil, err
}
func addMetaInfoFile(input *string, dir *storage.ClientImpl, client *torrent.Client, l *clist.Model, theme *styles.ColorTheme) (tea.Cmd, error) {
var meta *metainfo.MetaInfo

meta, err := metainfo.LoadFromFile(path)
if err != nil {
return nil, err
if strings.HasPrefix(*input, "http") {
response, err := http.Get(*input)
if err != nil {
return nil, err
}

meta, err = metainfo.Load(response.Body)
defer response.Body.Close()
if err != nil {
return nil, err
}
} else {
path, err := home.Expand(*input)
if err != nil {
return nil, err
}

meta, err = metainfo.LoadFromFile(path)
if err != nil {
return nil, err
}
}

spec := torrent.TorrentSpecFromMetaInfo(meta)
Expand Down
2 changes: 1 addition & 1 deletion internal/torrent/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func AddTorrent(t, dir *string, client *torrent.Client, l *list.Model, theme *st
} else if strings.HasPrefix(*t, infohashPrefix) || len(*t) == hashLength {
return addInfoHash(t, &store, client, l, theme)
} else {
return addFromFile(t, &store, client, l, theme)
return addMetaInfoFile(t, &store, client, l, theme)
}
}

Expand Down

0 comments on commit fd55a97

Please sign in to comment.