Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/scanui.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ func uploadFileCmd(index int, web webapi.Service, filename, token string) tea.Cm
}

if !exists {
_, err = web.Scan(filename, token, osFlag, enableDetonationFlag, timeoutFlag)
file, err := web.Scan(filename, token, osFlag, enableDetonationFlag, timeoutFlag)
if err != nil {
return fileUploadedMsg{index: index, err: fmt.Errorf("upload: %w", err)}
}
// Use the SHA256 from the server response. For single-file ZIPs,
// the server extracts the file and returns the child's hash, not
// the ZIP's hash.
return fileUploadedMsg{index: index, sha256: file.SHA256}
} else if forceRescanFlag {
err = web.Rescan(sha256, token, osFlag, enableDetonationFlag, timeoutFlag)
if err != nil {
Expand Down
25 changes: 16 additions & 9 deletions internal/webapi/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s Service) ListFiles(authToken string, page int) (*Pages, error) {

}

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

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

// Read the response.
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return nil, err
}
resp.Body.Close()
return body.String(), nil

if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("upload failed: HTTP %d: %s", resp.StatusCode, body)
}

var file entity.File
if err := json.Unmarshal(body, &file); err != nil {
return nil, fmt.Errorf("failed to parse upload response: %w", err)
}
return &file, nil
}

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