-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.go
71 lines (61 loc) · 1.11 KB
/
demo.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
package servemetf
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)
func download(url string, file io.Writer) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
return err
}
func (c Context) DownloadDemo(id int, steamID, fileName string) error {
url, err := c.GetZipFileURL(id, steamID)
if err != nil {
return err
}
file, err := ioutil.TempFile("", fmt.Sprintf("servemetf_%d.zip", id))
if err != nil {
return err
}
defer func() {
file.Close()
os.Remove(file.Name())
}()
err = download(url, file)
if err != nil {
return err
}
r, err := zip.OpenReader(file.Name())
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
if strings.HasSuffix(f.Name, ".dem") {
reader, err := f.Open()
if err != nil {
return err
}
err = saveDemo(reader, fileName)
if err != nil {
return err
}
reader.Close()
break
}
}
return nil
}
func saveDemo(r io.Reader, name string) error {
bytes, _ := ioutil.ReadAll(r)
return ioutil.WriteFile(name, bytes, 0755)
}