-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfetch.go
More file actions
198 lines (154 loc) · 4.08 KB
/
fetch.go
File metadata and controls
198 lines (154 loc) · 4.08 KB
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
195
196
197
198
package main
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"github.com/usher2/u2ckdump/internal/logger"
)
// DumpAnswer - "vigruzki" json API.
type DumpAnswer struct {
ArchStatus int `json:"a"`
ArchSize int `json:"as"`
CRC string `json:"crc"`
CacheExpirationTime int `json:"ct"`
ID string `json:"id"`
Size int `json:"s"`
DbUpdateTime int64 `json:"u"`
UpdateTime int64 `json:"ut"`
UrgentUpdateTime int64 `json:"utu"`
}
// Errors
var (
ErrNot200HTTPCode = errors.New("not 200 HTTP code")
ErrEmptyAnswer = errors.New("empty answer")
)
// GetLastDumpID - fetch last dump ID from "vigruzki".
func GetLastDumpID(ts int64, u, key string) (*DumpAnswer, error) {
answer := make([]DumpAnswer, 0)
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/last", u), nil)
if err != nil {
return nil, fmt.Errorf("construct request: %w", err)
}
q := req.URL.Query()
q.Add("ts", fmt.Sprintf("%d", ts))
req.URL.RawQuery = q.Encode()
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
if resp.StatusCode != 200 {
logger.Debug.Printf("%s\n", resp.Body)
return nil, fmt.Errorf("%w: %d", ErrNot200HTTPCode, resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(&answer)
if err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
if len(answer) == 0 {
return nil, fmt.Errorf("answers: %w", ErrEmptyAnswer)
}
return &answer[0], nil
}
// FetchDump - fetch dump from "vigruzki".
func FetchDump(id, filename, u, key string) error {
client := &http.Client{}
tfn := fmt.Sprintf("%s-tmp", filename)
out, err := os.Create(tfn)
if err != nil {
return err
}
defer out.Close()
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/get/%s", u, id), nil)
if err != nil {
return fmt.Errorf("%w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key))
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("%w: %d", ErrNot200HTTPCode, resp.StatusCode)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("body copy: %w", err)
}
err = os.Rename(tfn, filename)
if err != nil {
return fmt.Errorf("file rename: %w", err)
}
return nil
}
// ReadCurrentDumpID - read saved current dump id.
func ReadCurrentDumpID(filename string) (*DumpAnswer, error) {
result := DumpAnswer{}
if _, err := os.Stat(filename); err == nil {
dat, err := os.ReadFile(filename)
if err != nil {
return &result, fmt.Errorf("read file: %w", err)
}
err = json.Unmarshal(dat, &result)
if err != nil {
return &result, fmt.Errorf("unmarshal: %w", err)
}
}
return &result, nil
}
// WriteCurrentDumpID - save current dump id.
func WriteCurrentDumpID(filename string, dump *DumpAnswer) error {
dat, err := json.Marshal(dump)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
err = os.WriteFile(filename, dat, 0644)
if err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
// DumpUnzip - unzip dump file.
func DumpUnzip(src, filename string) error {
tmpfilename := fmt.Sprintf("%s-temp", filename)
r, err := zip.OpenReader(src)
if err != nil {
return fmt.Errorf("open zip arch: %w", err)
}
defer r.Close()
for _, f := range r.File {
// look over file list and handle this one
if f.Name != "dump.xml" {
continue
}
if f.FileInfo().IsDir() {
return fmt.Errorf("file is dir")
}
rc, err := f.Open()
if err != nil {
return fmt.Errorf("open zipped file: %w", err)
}
defer rc.Close()
f, err := os.Create(tmpfilename)
if err != nil {
return fmt.Errorf("create tmpfile: %w", err)
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return fmt.Errorf("write unzipped: %w", err)
}
break
}
err = os.Rename(tmpfilename, filename)
if err != nil {
return fmt.Errorf("file rename: %w", err)
}
return nil
}