Add VidHub endpoints

This commit is contained in:
Ben Adrian Sarmiento
2024-07-11 15:53:20 +02:00
parent f8f68e8225
commit 396a8781aa
10 changed files with 182 additions and 86 deletions

View File

@@ -13,26 +13,29 @@ import (
"github.com/debridmediamanager/zurg/pkg/dav"
)
// ServeRootDirectoryForDav serves the root directory for DAV
// The first entry is the root directory e.g. /root/
// Followed by the directories e.g. movies
func ServeRootDirectoryForDav(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(addSlash(""), ""))
buf.WriteString(dav.Directory(addSlash(""), ""))
directories := torMgr.DirectoryMap.Keys()
sort.Strings(directories)
for _, directory := range directories {
if strings.HasPrefix(directory, "int__") {
continue
}
buf.WriteString(dav.Directory(directory, ""))
buf.WriteString(dav.Directory(filepath.Base(directory), ""))
}
buf.WriteString(dav.Directory(config.DOWNLOADS, ""))
buf.WriteString(dav.Directory(filepath.Base(config.DOWNLOADS), ""))
_, size := version.GetFile()
buf.WriteString(dav.File(version.FILE, size, ""))
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}
func ServeTorrentsListForDav(directory string, torMgr *torrent.TorrentManager) ([]byte, error) {
func ServeGroupDirectoryForDav(directory string, torMgr *torrent.TorrentManager) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -40,7 +43,7 @@ func ServeTorrentsListForDav(directory string, torMgr *torrent.TorrentManager) (
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(addSlash(directory), ""))
buf.WriteString(dav.Directory(addSlash(directory), ""))
torrentNames := torrents.Keys()
sort.Strings(torrentNames)
for _, torrentName := range torrentNames {
@@ -48,13 +51,13 @@ func ServeTorrentsListForDav(directory string, torMgr *torrent.TorrentManager) (
if !ok {
continue
}
buf.WriteString(dav.Directory(torMgr.GetKey(tor), tor.Added))
buf.WriteString(dav.Directory(filepath.Base(torMgr.GetKey(tor)), tor.Added))
}
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}
func ServeFilesListForDav(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
func ServeTorrentFilesForDav(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -72,7 +75,7 @@ func ServeFilesListForDav(directory, torrentName string, torMgr *torrent.Torrent
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(addSlash(filepath.Join(directory, torMgr.GetKey(tor))), tor.Added))
buf.WriteString(dav.Directory(addSlash(filepath.Join(directory, torMgr.GetKey(tor))), tor.Added))
filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
@@ -98,32 +101,10 @@ func ServeFilesListForDav(directory, torrentName string, torMgr *torrent.Torrent
return buf.Bytes(), nil
}
func HandleSingleFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
}
tor, ok := torrents.Get(torrentName)
if !ok {
return nil, fmt.Errorf("cannot find torrent %s", torrentName)
}
file, ok := tor.SelectedFiles.Get(fileName)
if !ok || file.State.Is("deleted_file") {
return nil, fmt.Errorf("cannot find file %s", fileName)
}
func ServeDownloadsForDav(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(addSlash(filepath.Join(directory, torMgr.GetKey(tor))), tor.Added))
buf.WriteString(dav.File(fileName, file.Bytes, file.Ended))
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}
func ServeDownloadsListForDav(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, ""))
buf.WriteString(dav.Directory(addSlash(config.DOWNLOADS), ""))
filenames := torMgr.DownloadMap.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
@@ -136,3 +117,26 @@ func ServeDownloadsListForDav(torMgr *torrent.TorrentManager) ([]byte, error) {
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}
// HandleSingleFile is for PROPFIND requests for a single file, shared by Infuse and VidHub
func HandleSingleFile(directory, torrentName, filename string, torMgr *torrent.TorrentManager) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
}
tor, ok := torrents.Get(torrentName)
if !ok {
return nil, fmt.Errorf("cannot find torrent %s", torrentName)
}
file, ok := tor.SelectedFiles.Get(filename)
if !ok || file.State.Is("deleted_file") {
return nil, fmt.Errorf("cannot find file %s", filename)
}
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.Directory(addSlash(filepath.Join(directory, torMgr.GetKey(tor))), tor.Added))
buf.WriteString(dav.File(filename, file.Bytes, file.Ended))
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}

View File

@@ -12,6 +12,9 @@ import (
"github.com/debridmediamanager/zurg/pkg/dav"
)
// ServeRootDirectoryForInfuse serves the root directory for Infuse
// There is no root directory, just the directories e.g. movies
// Actual path is based on configured base path and current directory
func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
@@ -23,10 +26,10 @@ func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error)
if strings.HasPrefix(directory, "int__") {
continue
}
buf.WriteString(dav.BaseDirectory(directory, ""))
buf.WriteString(dav.Directory(directory, ""))
}
buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, ""))
buf.WriteString(dav.Directory(config.DOWNLOADS, ""))
_, size := version.GetFile()
buf.WriteString(dav.File(version.FILE, size, ""))
@@ -35,7 +38,7 @@ func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error)
return buf.Bytes(), nil
}
func ServeTorrentsListForInfuse(directory string, torMgr *torrent.TorrentManager) ([]byte, error) {
func ServeGroupDirectoryForInfuse(directory string, torMgr *torrent.TorrentManager) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -52,14 +55,14 @@ func ServeTorrentsListForInfuse(directory string, torMgr *torrent.TorrentManager
if !ok {
continue
}
buf.WriteString(dav.BaseDirectory(torMgr.GetKey(tor), tor.Added))
buf.WriteString(dav.Directory(torMgr.GetKey(tor), tor.Added))
}
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}
func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
func ServeTorrentFilesForInfuse(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -107,7 +110,7 @@ func ServeFilesListForInfuse(directory, torrentName string, torMgr *torrent.Torr
return buf.Bytes(), nil
}
func ServeDownloadsListForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) {
func ServeDownloadsForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")

14
internal/dav/util.go Normal file
View File

@@ -0,0 +1,14 @@
package dav
import (
"path/filepath"
"strings"
)
func addSlash(input string) string {
p := filepath.Join("/", input)
if p == "/" || strings.HasSuffix(p, "/") {
return p
}
return p + "/"
}

View File

@@ -13,6 +13,9 @@ import (
"github.com/debridmediamanager/zurg/pkg/dav"
)
// ServeRootDirectoryForVidHub serves the root directory for VidHub
// The first entry is the root directory e.g. /root/
// Followed by the absolute path of directories e.g. /root/movies/
func ServeRootDirectoryForVidHub(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
@@ -33,7 +36,7 @@ func ServeRootDirectoryForVidHub(torMgr *torrent.TorrentManager) ([]byte, error)
return buf.Bytes(), nil
}
func ServeTorrentsListForVidHub(directory string, torMgr *torrent.TorrentManager) ([]byte, error) {
func ServeGroupDirectoryForVidHub(directory string, torMgr *torrent.TorrentManager) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -56,7 +59,7 @@ func ServeTorrentsListForVidHub(directory string, torMgr *torrent.TorrentManager
return buf.Bytes(), nil
}
func ServeFilesListForVidHub(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
func ServeTorrentFilesForVidHub(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -101,7 +104,7 @@ func ServeFilesListForVidHub(directory, torrentName string, torMgr *torrent.Torr
return buf.Bytes(), nil
}
func ServeDownloadsListForVidHub(torMgr *torrent.TorrentManager) ([]byte, error) {
func ServeDownloadsForVidHub(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
prefixPath := addSlash(config.DOWNLOADS)
@@ -118,11 +121,3 @@ func ServeDownloadsListForVidHub(torMgr *torrent.TorrentManager) ([]byte, error)
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}
func addSlash(input string) string {
p := filepath.Join("/", input)
if p == "/" || strings.HasSuffix(p, "/") {
return p
}
return p + "/"
}

View File

@@ -28,6 +28,7 @@ type RootResponse struct {
Html string `json:"html"`
Dav string `json:"dav"`
Infuse string `json:"infuse"`
VidHub string `json:"vidhub"`
Logs string `json:"logs"`
UserInfo *realdebrid.User `json:"user_info"`
LibrarySize int `json:"library_size"` // Number of torrents in the library
@@ -104,6 +105,7 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
Html: fmt.Sprintf("//%s/http/", req.Host),
Dav: fmt.Sprintf("//%s/dav/", req.Host),
Infuse: fmt.Sprintf("//%s/infuse/", req.Host),
VidHub: fmt.Sprintf("//%s/vidhub/", req.Host),
Logs: fmt.Sprintf("//%s/logs/", req.Host),
UserInfo: userInfo,
TrafficServedPerAPI: trafficFromAPI,
@@ -255,6 +257,10 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>Infuse</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>VidHub</td>
<td colspan="2"><a href="%s">%s</a></td>
</tr>
<tr>
<td>Logs</td>
<td colspan="2"><a href="%s">%s</a></td>
@@ -268,6 +274,8 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Dav,
response.Infuse,
response.Infuse,
response.VidHub,
response.VidHub,
response.Logs,
response.Logs,
)

View File

@@ -50,6 +50,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Use(hs.basicAuth)
}
router.Use(hs.options)
router.NotFound(hs.handleNotFound)
router.Get("/", hs.handleHome)
router.Get("/stats", hs.handleHomeJson)
@@ -86,7 +87,6 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.MethodFunc("PROPFIND", fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavTorrentsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavFilesList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{filename}", hs.davCheckSingleFileHandler)
router.Get("/infuse/", hs.handleInfuseRoot)
router.Get(fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList)
@@ -102,13 +102,16 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Get("/vidhub/{directory}/", hs.handleVidHubTorrentsList)
router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s/", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/", hs.handleVidHubTorrentsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
// router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s/", config.DOWNLOADS), hs.handleVidHubDownloadsList)
// router.MethodFunc("PROPFIND", "/vidhub/{directory}/", hs.handleVidHubTorrentsList)
// router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
// router.MethodFunc("PROPFIND", "/vidhub", hs.handleVidHubRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubTorrentsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler)
// note: reused handlers for dav and infuse
router.Delete("/{mountType}/{directory}/{torrent}/", hs.deleteTorrentHandler)
router.Delete("/{mountType}/{directory}/{torrent}/{filename}", hs.deleteFileHandler)
@@ -207,17 +210,17 @@ func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.R
}
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeTorrentsListForDav
handlerFunc := dav.ServeGroupDirectoryForDav
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeTorrentsListForInfuse
handlerFunc := dav.ServeGroupDirectoryForInfuse
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeTorrentsListForVidHub
handlerFunc := dav.ServeGroupDirectoryForVidHub
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
@@ -256,15 +259,15 @@ func (hs *Handlers) handleHttpFilesList(resp http.ResponseWriter, req *http.Requ
}
func (hs *Handlers) handleDavFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeFilesListForDav, "text/xml; charset=\"utf-8\"")
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeFilesListForInfuse, "text/xml; charset=\"utf-8\"")
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeFilesListForVidHub, "text/xml; charset=\"utf-8\"")
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle downloads list request
@@ -278,21 +281,21 @@ func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.
func (hs *Handlers) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsListForDav(torMgr)
return dav.ServeDownloadsForDav(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsListForInfuse(torMgr)
return dav.ServeDownloadsForInfuse(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsListForVidHub(torMgr)
return dav.ServeDownloadsForVidHub(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
@@ -337,7 +340,7 @@ func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Req
// other handlers
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
func (hs *Handlers) checkSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
@@ -494,3 +497,8 @@ func (hs *Handlers) uploadLogsHandler(resp http.ResponseWriter, req *http.Reques
}
http.Redirect(resp, req, url, http.StatusFound)
}
func (hs *Handlers) handleNotFound(resp http.ResponseWriter, req *http.Request) {
hs.log.Debugf("Not found: %s %s", req.Method, req.URL)
http.NotFound(resp, req)
}

View File

@@ -405,6 +405,7 @@ func (t *TorrentManager) mountNewDownloads() {
filename = fmt.Sprintf("%s (%sp)%s", trimmed, parts[1], ext)
}
}
// t.log.Debugf("Download dump: %+v", downloads[i])
t.DownloadMap.Set(filename, &downloads[i])
mountedCount++

View File

@@ -140,8 +140,10 @@ func (dl *Downloader) DownloadFile(
}
}
if cfg.ShouldServeFromRclone() {
// log.Debugf("Redirecting to %s", unrestrict.Download)
redirect(resp, req, unrestrict.Download)
} else {
// log.Debugf("Streaming %s", unrestrict.Download)
dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
}
}
@@ -156,8 +158,10 @@ func (dl *Downloader) DownloadLink(
log *logutil.Logger,
) {
if cfg.ShouldServeFromRclone() {
// log.Debugf("Redirecting to %s", unrestrict.Download)
redirect(resp, req, unrestrict.Download)
} else {
log.Debugf("Streaming %s", unrestrict.Download)
dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
}
}

View File

@@ -2,37 +2,74 @@ package dav
import (
"fmt"
"html"
"path/filepath"
"strings"
)
// optimized versions, no more marshalling
func BaseDirectory(path, added string) string {
return fmt.Sprintf("<d:response><d:href>%s</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape(path), added)
}
func Directory(path, added string) string {
path = filepath.Base(path)
return fmt.Sprintf("<d:response><d:href>%s</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape(path), added)
}
func BaseFile(path string, fileSize int64, added string) string {
return fmt.Sprintf("<d:response><d:href>%s</d:href><d:propstat><d:prop><d:resourcetype></d:resourcetype><d:getcontentlength>%d</d:getcontentlength><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape(path), fileSize, added)
return fmt.Sprintf(`<d:response>
<d:href>%s</d:href>
<d:propstat>
<d:prop>
<d:resourcetype>
<d:collection/>
</d:resourcetype>
<d:getlastmodified>%s</d:getlastmodified>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>`, customPathEscape(path), added)
}
func File(path string, fileSize int64, added string) string {
path = filepath.Base(path)
return fmt.Sprintf("<d:response><d:href>%s</d:href><d:propstat><d:prop><d:resourcetype></d:resourcetype><d:getcontentlength>%d</d:getcontentlength><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape(path), fileSize, added)
return fmt.Sprintf(`<d:response>
<d:href>%s</d:href>
<d:propstat>
<d:prop>
<d:getcontentlength>%d</d:getcontentlength>
<d:getlastmodified>%s</d:getlastmodified>
<d:resourcetype></d:resourcetype>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>`, customPathEscape(path), fileSize, added)
}
func VidHubDirectory(path, added string) string {
if !strings.HasSuffix(path, "/") {
path += "/"
}
return fmt.Sprintf("<d:response><d:href>%s</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape2(path), added)
return fmt.Sprintf(`<d:response>
<d:href>%s</d:href>
<d:propstat>
<d:prop>
<d:displayname>%s</d:displayname>
<d:resourcetype>
<d:collection/>
</d:resourcetype>
<d:getlastmodified>%s</d:getlastmodified>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>`, customPathEscape(path), html.EscapeString(filepath.Base(path)), added)
}
func VidHubFile(path string, fileSize int64, added string) string {
return fmt.Sprintf("<d:response><d:href>%s</d:href><d:propstat><d:prop><d:resourcetype></d:resourcetype><d:getcontentlength>%d</d:getcontentlength><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape2(path), fileSize, added)
filename := filepath.Base(path)
return fmt.Sprintf(`<d:response>
<d:href>%s</d:href>
<d:propstat>
<d:prop>
<d:displayname>%s</d:displayname>
<d:getcontentlength>%d</d:getcontentlength>
<d:getlastmodified>%s</d:getlastmodified>
<d:getcontenttype>%s</d:getcontenttype>
<d:resourcetype></d:resourcetype>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>`, customPathEscape(path), html.EscapeString(filename), fileSize, added, getContentType(filename))
}

View File

@@ -2,6 +2,7 @@ package dav
import (
"net/url"
"path/filepath"
"strings"
)
@@ -15,17 +16,10 @@ func customPathEscape(input string) string {
segments[i] = escapedSegment
}
escapedPath := strings.Join(segments, "/")
// Convert any XML-escaped sequences back to URL-escaped sequences
escapedPath = strings.ReplaceAll(escapedPath, "$", "%24")
escapedPath = strings.ReplaceAll(escapedPath, "&", "%26")
escapedPath = strings.ReplaceAll(escapedPath, "+", "%2B")
escapedPath = strings.ReplaceAll(escapedPath, ":", "%3A")
escapedPath = strings.ReplaceAll(escapedPath, "@", "%40")
return escapedPath
return escapeForXML(escapedPath)
}
func customPathEscape2(input string) string {
// Convert any XML-escaped sequences back to URL-escaped sequences
func escapeForXML(input string) string {
input = strings.ReplaceAll(input, "$", "%24")
input = strings.ReplaceAll(input, "&", "%26")
input = strings.ReplaceAll(input, "+", "%2B")
@@ -33,3 +27,31 @@ func customPathEscape2(input string) string {
input = strings.ReplaceAll(input, "@", "%40")
return input
}
func getContentType(filename string) string {
filename = strings.ToLower(filename)
switch filepath.Ext(filename) {
case ".avi":
return "video/x-msvideo"
case ".m2ts":
return "video/mp2t"
case ".m4v":
return "video/x-m4v"
case ".mkv":
return "video/x-matroska"
case ".mov":
return "video/quicktime"
case ".mp4":
return "video/mp4"
case ".mpg":
return "video/mpeg"
case ".mpeg":
return "video/mpeg"
case ".ts":
return "video/mp2t"
case ".wmv":
return "video/x-ms-wmv"
default:
return "application/octet-stream"
}
}