fix: Upload to HRDLog
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -117,61 +116,9 @@ func UploadHRDLog(ctx context.Context, client *http.Client, callsign, code, adif
|
||||
return UploadResult{OK: false, Message: reason}, fmt.Errorf("hrdlog: upload failed: %s", reason)
|
||||
}
|
||||
|
||||
// UploadHRDLogADIF pushes a WHOLE ADIF document (header + many records) to
|
||||
// HRDLog.net in one NewEntry request — HRDLog parses every <eor> record and
|
||||
// replies "<insert>N" with the number actually inserted (duplicates aren't
|
||||
// counted but aren't errors). Use this for bulk uploads instead of calling
|
||||
// UploadHRDLog once per QSO.
|
||||
func UploadHRDLogADIF(ctx context.Context, client *http.Client, callsign, code, adifDoc string) (UploadResult, error) {
|
||||
callsign = strings.ToUpper(strings.TrimSpace(callsign))
|
||||
code = strings.TrimSpace(code)
|
||||
if callsign == "" {
|
||||
return UploadResult{}, fmt.Errorf("hrdlog: station callsign not set")
|
||||
}
|
||||
if code == "" {
|
||||
return UploadResult{}, fmt.Errorf("hrdlog: upload code not set")
|
||||
}
|
||||
if strings.TrimSpace(adifDoc) == "" {
|
||||
return UploadResult{}, fmt.Errorf("hrdlog: empty adif")
|
||||
}
|
||||
|
||||
body, err := hrdlogPost(ctx, client, callsign, code, adifDoc)
|
||||
if err != nil {
|
||||
return UploadResult{OK: false, Message: body}, err
|
||||
}
|
||||
if reason := authErrHRDLog(body); reason != "" {
|
||||
return UploadResult{OK: false, Message: reason}, fmt.Errorf("hrdlog: %s", reason)
|
||||
}
|
||||
if n, ok := parseHRDLogInsert(body); ok {
|
||||
return UploadResult{OK: true, Message: fmt.Sprintf("%d added", n)}, nil
|
||||
}
|
||||
if strings.Contains(strings.ToLower(body), "<error>") {
|
||||
return UploadResult{OK: false, Message: body}, fmt.Errorf("hrdlog: %s", body)
|
||||
}
|
||||
return UploadResult{OK: true, Message: "uploaded"}, nil
|
||||
}
|
||||
|
||||
// parseHRDLogInsert reads N from "<insert>N" (or "<insert>N</insert>").
|
||||
func parseHRDLogInsert(body string) (int, bool) {
|
||||
b := strings.ToLower(body)
|
||||
i := strings.Index(b, "<insert>")
|
||||
if i < 0 {
|
||||
return 0, false
|
||||
}
|
||||
rest := b[i+len("<insert>"):]
|
||||
j := 0
|
||||
for j < len(rest) && rest[j] >= '0' && rest[j] <= '9' {
|
||||
j++
|
||||
}
|
||||
if j == 0 {
|
||||
return 0, false
|
||||
}
|
||||
n, err := strconv.Atoi(rest[:j])
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
// NOTE: HRDLog's NewEntry.aspx inserts ONLY the first record of a multi-record
|
||||
// ADIFData, so there is no batch upload — callers must POST one record per
|
||||
// request (see UploadHRDLog). The bulk uploader in app.go does exactly that.
|
||||
|
||||
// TestHRDLog validates the configured HRDLog credentials with a REAL request:
|
||||
// it posts an empty ADIF so nothing is inserted, then checks for HRDLog's auth
|
||||
|
||||
Reference in New Issue
Block a user