Skip to content

Commit

Permalink
Added FileSizeReadable and DownloadedBytesReadable in the torrent…
Browse files Browse the repository at this point in the history
… stat
  • Loading branch information
glblduh committed May 24, 2022
1 parent 595803b commit d694272
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
11 changes: 8 additions & 3 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/dustin/go-humanize"
"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -242,10 +243,14 @@ func apiTorrentStats(w http.ResponseWriter, r *http.Request) {
/* Setting the files available in the torrent */
for _, tf := range v.Torrent.Files() {
tfname := tf.DisplayPath()
tfbc := tf.BytesCompleted()
tflen := tf.Length()
curf := apiTorrentStatsTorrentsFiles{
FileName: tfname,
FileSizeBytes: int(tf.Length()),
BytesDownloaded: int(tf.BytesCompleted()),
FileName: tfname,
FileSizeBytes: int(tflen),
FileSizeReadable: humanize.Bytes(uint64(tflen)),
DownloadedBytes: int(tfbc),
DownloadedReadable: humanize.Bytes(uint64(tfbc)),
}
if tf.BytesCompleted() > 0 {
curf.Stream = createFileLink(tstats.InfoHash, tfname, false)
Expand Down
6 changes: 4 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ func createAddTorrentRes(t *torrent.Torrent) apiAddTorrentRes {
HalfOpenPeers: t.Stats().HalfOpenPeers,
}
for _, f := range t.Files() {
tfsz := f.Length()
res.Files = append(res.Files, apiTorrentFiles{
FileName: f.DisplayPath(),
FileSizeBytes: int(f.Length()),
FileName: f.DisplayPath(),
FileSizeBytes: int(tfsz),
FileSizeReadable: humanize.Bytes(uint64(tfsz)),
})
}
return res
Expand Down
17 changes: 10 additions & 7 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ type (

// Struct for files in torrent
apiTorrentFiles struct {
FileName string `json:"filename"`
FileSizeBytes int `json:"filesizebytes"`
FileName string `json:"filename"`
FileSizeBytes int `json:"filesizebytes"`
FileSizeReadable string `json:"filesize"`
}

// Expected request body to selectFile
Expand Down Expand Up @@ -155,10 +156,12 @@ type (
}

apiTorrentStatsTorrentsFiles struct {
FileName string `json:"filename"`
FileSizeBytes int `json:"filesizebytes"`
BytesDownloaded int `json:"bytesdownloaded"`
Stream string `json:"stream,omitempty"`
Download string `json:"download,omitempty"`
FileName string `json:"filename"`
FileSizeBytes int `json:"filesizebytes"`
FileSizeReadable string `json:"filesize"`
DownloadedBytes int `json:"downloadedbytes"`
DownloadedReadable string `json:"downloaded"`
Stream string `json:"stream,omitempty"`
Download string `json:"download,omitempty"`
}
)

0 comments on commit d694272

Please sign in to comment.