-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
220 lines (195 loc) · 5.97 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"os/user"
"runtime"
"runtime/debug"
"sync"
"syscall"
"time"
. "ytd/clipboard"
. "ytd/models"
. "ytd/plugins"
_ "embed"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/mac"
)
var plugins []Plugin = []Plugin{&Yt{Name: "youtube"}}
//go:embed .version
var version string
//go:embed frontend/dist/assets/*
var static embed.FS
var pwaUrl = "https://ytd.surge.sh"
var appState *AppState
var newEntries = make(chan *GenericEntry)
var maxConvertJobs = runtime.NumCPU()
type JsonError struct {
Msg string `json:"msg"`
}
func (e *JsonError) ToJSON() string {
j, err := json.Marshal(e)
if err != nil {
return `{"msg": "json.Marshal() failed"}`
}
return string(j)
}
func panicHandler() error {
if panicPayload := recover(); panicPayload != nil {
stack := string(debug.Stack())
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "================================================================================")
fmt.Fprintln(os.Stderr, "Ytd has encountered a fatal error. This is a bug!")
fmt.Fprintln(os.Stderr, "We would appreciate a report: https://github.com/marcio199226/ytd/issues/")
fmt.Fprintln(os.Stderr, "Please provide all of the below text in your report.")
fmt.Fprintln(os.Stderr, "================================================================================")
fmt.Fprintf(os.Stderr, "Ytd Version: %s\n", version)
fmt.Fprintf(os.Stderr, "Go Version: %s\n", runtime.Version())
fmt.Fprintf(os.Stderr, "Go Compiler: %s\n", runtime.Compiler)
fmt.Fprintf(os.Stderr, "Architecture: %s\n", runtime.GOARCH)
fmt.Fprintf(os.Stderr, "Operating System: %s\n", runtime.GOOS)
fmt.Fprintf(os.Stderr, "Panic: %s\n\n", panicPayload)
fmt.Fprintln(os.Stderr, stack)
return fmt.Errorf("panic: %s", panicPayload)
}
return nil
}
func cors(fs http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fs.ServeHTTP(w, r)
}
}
func main() {
wg := &sync.WaitGroup{}
wg.Add(2)
app := &AppState{PwaUrl: pwaUrl}
ctx, cancelCtx := context.WithCancel(context.Background())
app.Context = ctx
exitChan := make(chan os.Signal, 1)
signal.Notify(exitChan,
os.Interrupt,
os.Kill,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
changes := make(chan string, 10)
stopCh := make(chan struct{}, 1)
go MonitorClipboard(time.Second, ctx, wg, stopCh, changes)
go func(wg *sync.WaitGroup) {
sig := <-exitChan
fmt.Println("Received signal", sig)
// send the shutdown signal through the context.Context and to the global context passed down
cancelCtx()
wg.Done()
stopCh <- struct{}{}
return
}(wg)
// initialize plugins
usr, _ := user.Current()
currentDir := fmt.Sprintf("%v/songs", usr.HomeDir)
for _, plugin := range plugins {
log.Println("download to dir=", fmt.Sprintf("%s/%s", currentDir, plugin.GetName()))
plugin.SetDir(fmt.Sprintf("%s/%s", currentDir, plugin.GetName()))
plugin.Initialize()
}
go func() {
// Watch for changes of local clipboard
for {
select {
case <-stopCh:
fmt.Printf("stopped manually")
time.Sleep(time.Second)
wg.Wait()
os.Exit(0)
return
default:
change, ok := <-changes /* */
if ok && change != "" {
log.Printf("change received: '%s'", change)
if app.Config.ClipboardWatch {
app.AddToDownload(change, true)
}
} else {
log.Println("channel has been closed. exiting...")
time.Sleep(time.Millisecond)
}
}
}
}()
go func() {
fs := http.StripPrefix("/static/", http.FileServer(http.FS(static)))
http.Handle("/tracks/", http.StripPrefix("/tracks/", http.FileServer(http.Dir(currentDir))))
http.Handle("/static/", cors(fs))
// api for mobile/pwa app
ytTracks := http.StripPrefix("/app/download", http.FileServer(http.Dir(currentDir)))
http.Handle("/app/download/", cors(ytTracks))
http.HandleFunc("/app/state", func(w http.ResponseWriter, r *http.Request) {
var buffer bytes.Buffer
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, X-API-KEY")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, X-API-KEY")
// check api-key if needed
var xApiKey = r.Header.Get("X-API-KEY")
if xApiKey == "" {
xApiKey = r.URL.Query().Get("api-key")
}
if appState.Config.IsNgrokApiKeyEnabled() && (appState.Config.GetNgrokApiKkey() == "" || xApiKey != appState.Config.GetNgrokApiKkey()) {
jsonErr := &JsonError{Msg: "Wrong api key"}
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(&buffer).Encode(&jsonErr)
w.Write(buffer.Bytes())
return
}
err := json.NewEncoder(&buffer).Encode(appState.GetAll())
if err != nil {
http.Error(w, err.Error(), 400)
return
}
w.Write(buffer.Bytes())
})
http.ListenAndServe(":8080", nil)
}()
defer panicHandler()
app.PreWailsInit(ctx)
err := wails.Run(&options.App{
Width: 1024,
Height: 768,
MinWidth: 1024,
MinHeight: 768,
StartHidden: app.canStartAtLogin && app.Config.StartAtLogin,
HideWindowOnClose: app.Config.RunInBackgroundOnClose,
DisableResize: false,
Fullscreen: false,
Startup: app.WailsInit,
Shutdown: app.WailsShutdown,
Mac: &mac.Options{
WebviewIsTransparent: true,
WindowBackgroundIsTranslucent: true,
TitleBar: mac.TitleBarHiddenInset(),
ActivationPolicy: mac.NSApplicationActivationPolicyAccessory,
},
Title: "ytd",
Bind: []interface{}{
app,
app.offlinePlaylistService,
app.ngrok,
},
Frameless: false,
})
if err != nil {
log.Fatalln(err)
}
}