aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/install.go
blob: d9a2379f4e2f62c188ab477778e5c2a390f59ccd (plain) (blame)
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
package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"
)

type statusInfo struct {
	Running     bool
	Error       error
	Url         string
	Transferred int64
	Size        int64
	Start       *time.Time
}

var (
	status = struct {
		m *sync.RWMutex
		s *statusInfo
	}{
		m: &sync.RWMutex{},
		s: &statusInfo{Running: false},
	}

	getPath = make(chan string)
)

func statsHandler(w http.ResponseWriter, r *http.Request) {
	status.m.RLock()
	defer status.m.RUnlock()

	w.Header().Add("Content-type", "application/json")

	enc := json.NewEncoder(w)

	err := enc.Encode(status.s)
	if err != nil {
		Logger.Println("While encoding status: ", err)
	}
	return
}

func installHttp(u string) error {
	Logger.Println("Installer: loading from url")
	resp, err := http.Get(u)
	if err != nil {
		return fmt.Errorf("Installer: getting %w", err)
	}

	estSize, err := strconv.ParseInt(resp.Header.Get("Estimated-size"), 10, 64)
	if err != nil {
		return fmt.Errorf("Failed to convert estimated size header: %w", err)
	}

	status.m.Lock()
	status.s.Size = estSize
	status.m.Unlock()

	rdr, wrtr := io.Pipe()

	go func() {
		err = Lib.Extract(rdr)
		if err != nil {
			Logger.Printf("Installer: extracting %w", err)
		}
		resp.Body.Close()
	}()

	var total int64
	start := time.Now()
	status.m.Lock()
	status.s.Start = &start
	status.m.Unlock()
	for {
		var n int64
		n, err = io.CopyN(wrtr, resp.Body, 100*1024*1024)
		if err == io.EOF {
			break
		} else if err != nil {
			Logger.Printf("Error encountered read from response body in installer: %s", err)
			break
		}

		total += n
		mb := float64(total / 1024 / 1024)
		rate := mb / time.Since(start).Seconds()

		Logger.Printf("Downloading from %s, Size: %s, %0.1f%% Done, Rate: %.2f mb/s",
			u, formatBytes(estSize), float64(total)/float64(estSize)*100, rate)

		status.m.Lock()
		status.s.Transferred = total
		status.m.Unlock()
	}

	if err == io.EOF {
		return nil
	}

	return err
}

func installPath(p string) error {
	Logger.Println("Installer: loading from filesystem")
	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
}

// installer handles installing games either from a local path or a
// remote URL
func installer(urls <-chan string) {
	var err error
	for u := range urls {
		status.m.Lock()
		Logger.Printf("Installer: running for URI: %s", u)
		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
		Logger.Printf("Installer: Completed request %s Errors: %s", u, err)
		status.m.Unlock()

		reloadLib()
	}
}

func gameInstaller(w http.ResponseWriter, r *http.Request) {
	if unauthorizedIfNotLocal(w, r) {
		return
	}

	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
		}
	}

	Logger.Printf("Installer: Sending request for: %s to channel", uri)
	getPath <- uri

	http.Redirect(w, r, "/", 302)
}