Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPfs to download any content with wget or curl #459

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions server/web/api/httpfs/httpfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package httpfs

import (
"strings"
"net/http"
"net/url"
"strconv"
"server/log"
"github.com/gin-gonic/gin"
"server/torr"
"github.com/pkg/errors"
"github.com/anacrolix/torrent"

"server/torr/state"

sf "github.com/sa-/slicefunk"
)

func listDir(path string, folders [] string, files []string) string {
result := `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Directory listing for ` + path + `</title>
</head><body><h1>Directory listing for ` + path + `</h1><hr><ul>`

for _, str := range folders {
result += `<li><a href="` + url.PathEscape(str) + `/">` + str + `/</a></li>`
}
for _, str := range files {
result += `<li><a href="` + url.PathEscape(str) + `">` + str + `</a></li>`
}
result += `</ul><hr></body></html>`
return result
}



func HandleHttpfs(c *gin.Context) {
path := c.Param("path")
log.TLogln("URL path", path)
path = strings.Trim(path, "/")

if path == "" {
newArray := sf.Map(torr.ListTorrent(), func(item * torr.Torrent) string { return item.Title })
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, listDir(path, newArray, []string{}))
} else {
folders := strings.Split(path, "/")
trName := folders[0]
folderPath := strings.Join(folders[1:], "/")
// for _, item := range torr.ListTorrent() {
// log.TLogln("Torrents found", item.Title)
// }
requestedTorrents := sf.Filter(torr.ListTorrent(), func(item * torr.Torrent) bool { return item.Title == trName })
if len(requestedTorrents) != 1 {
c.AbortWithError(http.StatusBadRequest, errors.New("Torrent not found" + trName + ": " + strconv.Itoa(len(requestedTorrents))))
return
}
requestedTorrent := torr.GetTorrent(requestedTorrents[0].Hash().HexString())
if requestedTorrent == nil {
c.AbortWithStatus(http.StatusNotFound)
return
}

if requestedTorrent.Stat == state.TorrentInDB {
requestedTorrent = torr.LoadTorrent(requestedTorrent)
if requestedTorrent == nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("error get torrent info"))
return
}
}

newArray := sf.Filter(requestedTorrent.Files(), func(item * torrent.File) bool { return strings.HasPrefix(item.Path(), folderPath) })

// for _, item := range newArray {
// log.TLogln("Filtered files", item.Path(), " sha1: ")// + item.FileInfo())
// }
//
if len(newArray) == 1 && newArray[0].Path() == folderPath {
log.TLogln("Downloading file:", requestedTorrent.Title, ":", newArray[0].Path())
var index = -1
for i, item := range requestedTorrent.Files() {
if item.Path() == folderPath {
index = i + 1
break
}
}
names := strings.Split(newArray[0].Path(), "/")
// torr.Preload(requestedTorrent, index)
c.Header("Content-Disposition", `attachment; filename="`+names[len(names) - 1]+`"`)
c.Header("Content-Type", "application/octet-stream")
requestedTorrent.Stream(index, c.Request, c.Writer)
} else {
c.Header("Content-Type", "text/html; charset=utf-8")
folders := []string{}
files := []string{}

for _, item := range newArray {
p := strings.TrimPrefix(item.Path(), folderPath + "/")
if strings.Contains(p, "/") {
folders = append(folders, strings.Split(p, "/")[0])
} else {
files = append(files, p)
}
}
folders = sf.Unique(folders)
c.String(200, listDir(path, folders, files))
}
}

}
5 changes: 5 additions & 0 deletions server/web/api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
config "server/settings"
"server/web/auth"
"net/http"
"server/web/api/httpfs"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -50,4 +52,7 @@ func SetupRoute(route gin.IRouter) {
}

authorized.GET("/ffp/:hash/:id", ffp)

authorized.GET("/httpfs", func(c *gin.Context) { c.Redirect(http.StatusFound, "/httpfs/") })
authorized.GET("/httpfs/*path", httpfs.HandleHttpfs)
}
10 changes: 9 additions & 1 deletion web/src/components/App/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Divider from '@material-ui/core/Divider'
import ListItem from '@material-ui/core/ListItem'
import ListItemIcon from '@material-ui/core/ListItemIcon'
import ListItemText from '@material-ui/core/ListItemText'
import { CreditCard as CreditCardIcon } from '@material-ui/icons'
import { CreditCard as CreditCardIcon, Http as HttpIcon } from '@material-ui/icons'
import List from '@material-ui/core/List'
import { useTranslation } from 'react-i18next'
import AddDialogButton from 'components/Add'
Expand Down Expand Up @@ -60,6 +60,14 @@ const Sidebar = ({ isDrawerOpen, setIsDonationDialogOpen, isOffline, isLoading,
<Divider />

<List>
<ListItem button onClick={() => window.open('httpfs', '_blank')}>
<ListItemIcon>
<HttpIcon />
</ListItemIcon>

<ListItemText primary={t('HttpFS')} />
</ListItem>

<SettingsDialog isOffline={isOffline} isLoading={isLoading} />

<AboutDialog />
Expand Down