-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
143 lines (118 loc) · 3.02 KB
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd
import (
_ "embed"
"encoding/json"
"encoding/xml"
"fmt"
"github.com/wheresalice/invidious2newpipe/lib"
"hash/adler32"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/spf13/cobra"
)
var (
workPath = "subs"
)
// webCmd represents the web command
var webCmd = &cobra.Command{
Use: "web",
Short: "Run invidious2newpipe as a webserver",
Run: func(cmd *cobra.Command, args []string) {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
log.Println("invidious2newpipe -- Starting ")
log.Printf("http://localhost:%v\n", port)
subsDir := os.Getenv("SUBS_DIR")
workPath = path.Join(subsDir, workPath)
os.Mkdir(workPath, 0700)
log.Printf("Using %v to store subs", workPath)
if value, ok := os.LookupEnv("PORT"); ok {
port = value
}
http.HandleFunc("/", reqHandler)
log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil))
},
}
var (
port = "5000"
)
//go:embed index.html
var index string
func handleGetSubs(w http.ResponseWriter, r *http.Request) {
reqPath := filepath.Clean(r.URL.Path)
log.Printf("GET %v", reqPath)
if (reqPath == "/") || (reqPath == "/index.html") {
fmt.Fprintf(w, index)
} else {
// otherwise, if the requested paste exists, we serve it...
subs, err := os.ReadFile(path.Join(workPath, reqPath))
if err != nil {
http.NotFound(w, r)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(subs))
}
}
func getHash(content string) string {
h := adler32.New()
h.Write([]byte(content))
return fmt.Sprintf("%x", h.Sum(nil))
}
func handlePutSubs(w http.ResponseWriter, r *http.Request) {
err1 := r.ParseForm()
err2 := r.ParseMultipartForm(int64(2 * 4096))
if err1 != nil && err2 != nil {
// Invalid POST -- let's serve the default file
fmt.Fprintf(w, index)
} else {
file, handler, err := r.FormFile("subs")
if err != nil {
fmt.Fprintf(w, "error retrieving file: %v", err)
return
}
defer file.Close()
log.Printf("Uploaded File: %+v\n", handler.Filename)
log.Printf("File Size: %+v\n", handler.Size)
log.Printf("MIME Header: %+v\n", handler.Header)
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Fprintf(w, "failed to read file: %v", err)
}
hash := getHash(string(fileBytes))
var opml lib.Opml
err = xml.Unmarshal(fileBytes, &opml)
if err != nil {
fmt.Fprintf(w, "failed to parse: %v", err)
}
var newpipe lib.NewPipe
for _, s := range opml.Body.Outline.Outline {
newpipe.Subscriptions = append(newpipe.Subscriptions, lib.Subscriptions{
Name: s.Title,
URL: lib.XmlUrlToChanelUrl(s.XmlUrl),
ServiceID: 0,
})
}
output, err := json.Marshal(newpipe)
if err != nil {
fmt.Fprintf(w, "failed to marshal content: %v", err)
}
os.WriteFile(path.Join(workPath, hash), output, 0600)
http.Redirect(w, r, hash, 301)
}
}
func reqHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
handleGetSubs(w, r)
case "POST":
handlePutSubs(w, r)
default:
http.NotFound(w, r)
}
}
func init() {
rootCmd.AddCommand(webCmd)
}