From 36dc9ff10971cf97eb077907072c519cb5349fe4 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 21 Nov 2020 15:33:57 -0500 Subject: Most of the functionality I want is there now, it's not pretty though. Build the web server and the command line in the build script Allow for deletion of games from the library Move the download function out of main.go Add more information to the index template and handler. Allow for downloads from URLs and installation from local files. Allow changing of the library path via a command line flag Automatically start the web browser on windows --- cmd/web/install.go | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 cmd/web/install.go (limited to 'cmd/web/install.go') diff --git a/cmd/web/install.go b/cmd/web/install.go new file mode 100644 index 0000000..cd2f03a --- /dev/null +++ b/cmd/web/install.go @@ -0,0 +1,116 @@ +package main + +import ( + "net/url" + "os" + "strings" + "fmt" + "net/http" + "sync" +) + +type statusInfo struct { + Running bool + Error error + Url string +} + +var ( + status = struct { + m *sync.RWMutex + s *statusInfo + }{ + m: &sync.RWMutex{}, + s: &statusInfo{Running: false}, + } + + getPath = make(chan string) +) + +func installHttp(u string) error { + resp, err := http.Get(u) + if err != nil { + return fmt.Errorf("Installer: getting %w", err) + } + + err = Lib.Extract(resp.Body) + if err != nil { + return fmt.Errorf("Installer: extracting %w", err) + } + resp.Body.Close() + return nil +} + +func installPath(p string) error { + fh, err := os.Open(p) + if err != nil { + return fmt.Errorf("Installer: opening %w", err) + } + + err = Lib.Extract(fh) + if err != nil { + return fmt.Errorf("Installer: opening %w", err) + } + fh.Close() + return nil +} + +func installer(urls <-chan string) { + var err error + for u := range urls { + status.m.Lock() + status.s.Running = true + status.s.Url = u + status.m.Unlock() + + if strings.HasPrefix(u, "http") { + err = installHttp(u) + } else { + err = installPath(u) + } + + status.m.Lock() + status.s.Running = false + status.s.Error = err + status.m.Unlock() + + reloadLib() + } +} + +func gameInstaller(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + Logger.Printf("Installer: While parsing form: %s", err) + http.Error(w, fmt.Sprintf("Invalid form: %s", err), 400) + return + } + + uri := r.Form.Get("uri") + + if strings.HasPrefix(uri, "http") { + _, err := url.Parse(uri) + if err != nil { + Logger.Printf("Installer: While parsing url: %s", err) + http.Error(w, fmt.Sprintf("Invalid url: %s", err), 400) + return + } + } else { + fi, err := os.Stat(uri) + if err != nil { + Logger.Printf("Installer: While parsing url: %s", err) + http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400) + return + } + + if !fi.Mode().IsRegular() { + Logger.Printf("Installer: While parsing url: %s", err) + http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400) + return + } + } + + getPath <- uri + + http.Redirect(w, r, "/", 302) +} -- cgit v1.2.3