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

@@ -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"
}
}