Skip to content

Commit 9d9b555

Browse files
committed
Checks resp.StatusCode != 201 and returns an error with the response body
1 parent cb15aeb commit 9d9b555

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

cmd/scanui.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ func uploadFileCmd(index int, web webapi.Service, filename, token string) tea.Cm
8888
}
8989

9090
if !exists {
91-
_, err = web.Scan(filename, token, osFlag, enableDetonationFlag, timeoutFlag)
91+
file, err := web.Scan(filename, token, osFlag, enableDetonationFlag, timeoutFlag)
9292
if err != nil {
9393
return fileUploadedMsg{index: index, err: fmt.Errorf("upload: %w", err)}
9494
}
95+
// Use the SHA256 from the server response. For single-file ZIPs,
96+
// the server extracts the file and returns the child's hash, not
97+
// the ZIP's hash.
98+
return fileUploadedMsg{index: index, sha256: file.SHA256}
9599
} else if forceRescanFlag {
96100
err = web.Rescan(sha256, token, osFlag, enableDetonationFlag, timeoutFlag)
97101
if err != nil {

internal/webapi/files.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s Service) ListFiles(authToken string, page int) (*Pages, error) {
117117

118118
}
119119

120-
func (s Service) Scan(filepath string, authToken, preferredOS string, enableDetonation bool, timeout int) (string, error) {
120+
func (s Service) Scan(filepath string, authToken, preferredOS string, enableDetonation bool, timeout int) (*entity.File, error) {
121121
params := map[string]string{
122122
"skip_detonation": strconv.FormatBool(!enableDetonation),
123123
"os": preferredOS,
@@ -127,7 +127,7 @@ func (s Service) Scan(filepath string, authToken, preferredOS string, enableDeto
127127
// Create a new file upload request.
128128
request, err := s.newfileUploadRequest("file", filepath, params)
129129
if err != nil {
130-
return "", err
130+
return nil, err
131131
}
132132

133133
// Add our auth token.
@@ -136,17 +136,24 @@ func (s Service) Scan(filepath string, authToken, preferredOS string, enableDeto
136136
// Perform the http request.
137137
resp, err := s.client.Do(request)
138138
if err != nil {
139-
return "", err
139+
return nil, err
140140
}
141+
defer resp.Body.Close()
141142

142-
// Read the response.
143-
body := &bytes.Buffer{}
144-
_, err = body.ReadFrom(resp.Body)
143+
body, err := io.ReadAll(resp.Body)
145144
if err != nil {
146-
return "", err
145+
return nil, err
147146
}
148-
resp.Body.Close()
149-
return body.String(), nil
147+
148+
if resp.StatusCode != http.StatusCreated {
149+
return nil, fmt.Errorf("upload failed: HTTP %d: %s", resp.StatusCode, body)
150+
}
151+
152+
var file entity.File
153+
if err := json.Unmarshal(body, &file); err != nil {
154+
return nil, fmt.Errorf("failed to parse upload response: %w", err)
155+
}
156+
return &file, nil
150157
}
151158

152159
func (s Service) Rescan(sha256, authToken, preferredOS string, enableDetonation bool, timeout int) error {

0 commit comments

Comments
 (0)