Skip to content
Open
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
11 changes: 5 additions & 6 deletions internal/cmdutil/fileupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ func BuildFormdata(fileIO fileio.FileIO, fieldName, filePath string, isStdin boo
if err != nil {
return nil, output.ErrValidation("cannot open file: %s", filePath)
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return nil, output.ErrValidation("--file: failed to read %s: %v", filePath, err)
}
fd.AddFile(fieldName, bytes.NewReader(data))
// Keep the concrete file reader instead of copying it into a
// bytes.Reader. The Lark SDK preserves multipart filenames only when
// it receives an *os.File; replacing it with an anonymous reader makes
// the SDK fall back to "unknown-file".
fd.AddFile(fieldName, f)
}

// Add top-level JSON keys as text form fields.
Expand Down
22 changes: 22 additions & 0 deletions internal/cmdutil/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -276,6 +277,10 @@ func TestBuildFormdata(t *testing.T) {
if fd == nil {
t.Fatal("expected non-nil Formdata")
}

if got := formdataFieldTypeName(t, fd, "photo"); got != "*os.File" {
t.Fatalf("formdata file field type = %s, want *os.File", got)
}
})

t.Run("file not found", func(t *testing.T) {
Expand Down Expand Up @@ -337,6 +342,23 @@ func TestBuildFormdata(t *testing.T) {
})
}

func formdataFieldTypeName(t *testing.T, fd any, field string) string {
t.Helper()

fields := reflect.ValueOf(fd).Elem().FieldByName("fields")
if !fields.IsValid() {
t.Fatal("Formdata.fields is not available")
}
value := fields.MapIndex(reflect.ValueOf(field))
if !value.IsValid() {
t.Fatalf("Formdata field %q is missing", field)
}
if value.Kind() == reflect.Interface {
value = value.Elem()
}
return value.Type().String()
}

// TestFormatFormFieldValue locks in the fix for the float64 -> scientific
// notation bug. JSON numbers unmarshal to float64, and fmt's default %v for
// float64 delegates to %g which switches to scientific notation at ~1e6
Expand Down
Loading