aboutsummaryrefslogtreecommitdiff
path: root/steam/http.go
blob: df756e43676039812c7aa7920ef635918f06d1bb (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
package steam

import (
	"fmt"
	"net/http"
	"os"
	"strconv"
)

// ExtractFile is a wrapper around Extract that handles an HTTP endpoint.
// this spawns an "extractFile" on the library. Status will be updated there
// as this goes along. Non fatal and fatal errors will be populated there
func (l *Library) ExtractFile(fn string) (*Game, error) {
	g := &Game{}
	j := newJob("extractFile", g)
	defer j.done()

	l.status.addJob(j)

	fi, err := os.Stat(fn)
	if err != nil {
		j.addError(err)
		return g, err
	}
	j.setSize(fi.Size())

	fh, err := os.Open(fn)
	if err != nil {
		j.addError(err)
		return g, err
	}

	return l.extractUpdate(j, g, fh)
}

// ExtractHTTP is a wrapper around Extract that handles an HTTP endpoint.
// this spawns an "extractHTTP" on the library. Status will be updated there
// as this goes along. Non fatal and fatal errors will be populated there
func (l *Library) ExtractHTTP(url string) (*Game, error) {
	g := &Game{}
	j := newJob("extractHTTP", g)
	defer j.done()

	l.status.addJob(j)

	resp, err := http.Get(url)
	if err != nil {
		j.addError(err)
		return g, err
	}

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

	j.setSize(estSize)

	return l.extractUpdate(j, g, resp.Body)
}