Skip to content

Commit

Permalink
Added uploading of torrent file
Browse files Browse the repository at this point in the history
  • Loading branch information
glblduh committed May 18, 2022
1 parent 0879e0b commit 69274d9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ For manual infohash, display name, and trackers
}
```

### Uploading a torrent file
`POST /api/addtorrentfile`

Attach the file in the `torrent` field in `multipart/form-data`

### Selecting a file for download
`POST /api/selectfile`

Expand Down
51 changes: 37 additions & 14 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -48,20 +49,7 @@ func apiAddTorrent(w http.ResponseWriter, r *http.Request) {
}

/* Creates the response body*/
res := apiAddTorrentRes{
Name: t.Name(),
InfoHash: t.InfoHash().String(),
TotalPeers: t.Stats().TotalPeers,
ActivePeers: t.Stats().ActivePeers,
PendingPeers: t.Stats().PendingPeers,
HalfOpenPeers: t.Stats().HalfOpenPeers,
}
for _, f := range t.Files() {
res.Files = append(res.Files, apiTorrentFiles{
FileName: f.DisplayPath(),
FileSizeBytes: int(f.Length()),
})
}
res := createAddTorrentRes(t)
encodeRes(w, &res)
return
}
Expand Down Expand Up @@ -318,3 +306,38 @@ func apiDownloadFile(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, f.DisplayPath(), time.Now(), reader)
return
}

func apiAddTorrentFile(w http.ResponseWriter, r *http.Request) {
/* Gets file from form */
torrfile, _, err := r.FormFile("torrent")
if err != nil {
errorRes(w, err.Error(), http.StatusInternalServerError)
return
}
defer torrfile.Close()

/* Loads torrent file to the BitTorrent client */
/* Loads the torrent file as a MetaInfo */
mi, mierr := metainfo.Load(torrfile)
if mierr != nil {
errorRes(w, mierr.Error(), http.StatusInternalServerError)
return
}
/* Makes torrent spec from given MetaInfo */
spec, specerr := torrent.TorrentSpecFromMetaInfoErr(mi)
if specerr != nil {
errorRes(w, specerr.Error(), http.StatusInternalServerError)
return
}
/* Adds torrent spec to the BitTorrent client */
t, terr := btEngine.addTorrent(spec, false)
if terr != nil {
errorRes(w, terr.Error(), http.StatusInternalServerError)
return
}

/* Create response */
res := createAddTorrentRes(t)
encodeRes(w, &res)
return
}
19 changes: 19 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,22 @@ func newBtCliConfs(dir string, noup bool) *torrent.ClientConfig {
func safenDisplayPath(displaypath string) string {
return strings.Join(strings.Split(displaypath, "/"), " - ")
}

// Create response for apiAddTorrent and apiAddTorrentFile
func createAddTorrentRes(t *torrent.Torrent) apiAddTorrentRes {
res := apiAddTorrentRes{
Name: t.Name(),
InfoHash: t.InfoHash().String(),
TotalPeers: t.Stats().TotalPeers,
ActivePeers: t.Stats().ActivePeers,
PendingPeers: t.Stats().PendingPeers,
HalfOpenPeers: t.Stats().HalfOpenPeers,
}
for _, f := range t.Files() {
res.Files = append(res.Files, apiTorrentFiles{
FileName: f.DisplayPath(),
FileSizeBytes: int(f.Length()),
})
}
return res
}
1 change: 1 addition & 0 deletions torrenttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
/* POST */
r.HandleFunc("/api/addtorrent", apiAddTorrent).Methods("POST")
r.HandleFunc("/api/selectfile", apiTorrentSelectFile).Methods("POST")
r.HandleFunc("/api/addtorrentfile", apiAddTorrentFile).Methods("POST")

/* DELETE */
r.HandleFunc("/api/removetorrent", apiRemoveTorrent).Methods("DELETE")
Expand Down

0 comments on commit 69274d9

Please sign in to comment.