diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-11-21 15:33:57 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-11-21 15:33:57 -0500 |
| commit | 36dc9ff10971cf97eb077907072c519cb5349fe4 (patch) | |
| tree | faf3ff607714ee8b155c801b91a04e2cc8cb2279 /cmd/web/install.go | |
| parent | 4ed57528379c9d1ac0f5bc77b95439bbba3d4488 (diff) | |
| download | steam-export-36dc9ff10971cf97eb077907072c519cb5349fe4.tar.gz steam-export-36dc9ff10971cf97eb077907072c519cb5349fe4.tar.xz | |
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
Diffstat (limited to 'cmd/web/install.go')
| -rw-r--r-- | cmd/web/install.go | 116 |
1 files changed, 116 insertions, 0 deletions
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) +} |
