feat: torrent file support on web (#171)

This commit is contained in:
Levi authored and GitHub committed 2023-07-28 21:48:20 +08:00
1 parent 81518d794f
commit a5d8713ff7
12 files changed
+288 -55

No files matched your search

+32 -3
View File
@@ -1,13 +1,42 @@
package util
import "strings"
import (
"encoding/base64"
"regexp"
"strings"
)
const FileSchema = "FILE"
func ParseSchema(url string) string {
index := strings.Index(url, ":")
if index == -1 {
return FileSchema
// check is file path
if IsExistsFile(url) {
return FileSchema
}
return ""
}
return strings.ToUpper(url[:index])
schema := url[:index]
if schema == "data" {
schema, _ = ParseDataUri(url)
}
return strings.ToUpper(schema)
}
// ParseDataUri parses a data URI and returns the MIME type and decode data.
func ParseDataUri(uri string) (string, []byte) {
re := regexp.MustCompile(`^data:(.*);base64,(.*)$`)
matches := re.FindStringSubmatch(uri)
if len(matches) != 3 {
return "", nil
}
mime := matches[1]
base64Data := matches[2]
// 解码Base64数据
data, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return "", nil
}
return mime, data
}