Skip to content

fix image api missing filename bug #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 29 additions & 3 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,32 @@
return
}

// WrapReader wraps an io.Reader with filename and Content-type.
func WrapReader(rdr io.Reader, filename string, contentType string) io.Reader {
return file{rdr, filename, contentType}

Check warning on line 137 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L136-L137

Added lines #L136 - L137 were not covered by tests
}

type file struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the file struct should be put here

io.Reader
name string
contentType string
}

func (f file) Name() string {
if f.name != "" {
return f.name
} else if named, ok := f.Reader.(interface{ Name() string }); ok {
return named.Name()
}
return ""

Check warning on line 152 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L146-L152

Added lines #L146 - L152 were not covered by tests
}

func (f file) ContentType() string {
return f.contentType

Check warning on line 156 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L155-L156

Added lines #L155 - L156 were not covered by tests
}

// ImageEditRequest represents the request structure for the image API.
// Use WrapReader to wrap an io.Reader with filename and Content-type.
type ImageEditRequest struct {
Image io.Reader `json:"image,omitempty"`
Mask io.Reader `json:"mask,omitempty"`
Expand All @@ -150,15 +175,15 @@
body := &bytes.Buffer{}
builder := c.createFormBuilder(body)

// image, filename is not required
// image, filename verification can be postponed
err = builder.CreateFormFileReader("image", request.Image, "")
if err != nil {
return
}

// mask, it is optional
if request.Mask != nil {
// mask, filename is not required
// filename verification can be postponed
err = builder.CreateFormFileReader("mask", request.Mask, "")
if err != nil {
return
Expand Down Expand Up @@ -206,6 +231,7 @@
}

// ImageVariRequest represents the request structure for the image API.
// Use WrapReader to wrap an io.Reader with filename and Content-type.
type ImageVariRequest struct {
Image io.Reader `json:"image,omitempty"`
Model string `json:"model,omitempty"`
Expand All @@ -221,7 +247,7 @@
body := &bytes.Buffer{}
builder := c.createFormBuilder(body)

// image, filename is not required
// image, filename verification can be postponed
err = builder.CreateFormFileReader("image", request.Image, "")
if err != nil {
return
Expand Down
17 changes: 15 additions & 2 deletions internal/form_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@
}

// CreateFormFileReader creates a form field with a file reader.
// The filename in parameters can be an empty string.
// The filename in Content-Disposition is required, But it can be an empty string.
// The filename in Content-Disposition is required.
func (fb *DefaultFormBuilder) CreateFormFileReader(fieldname string, r io.Reader, filename string) error {
if filename == "" {
if f, ok := r.(interface{ Name() string }); ok {
filename = f.Name()
}

Check warning on line 47 in internal/form_builder.go

View check run for this annotation

Codecov / codecov/patch

internal/form_builder.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}
var contentType string
Copy link
Contributor Author

@Axb12 Axb12 Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the default Content-Type is set to application/octet-stream here

if f, ok := r.(interface{ ContentType() string }); ok {
contentType = f.ContentType()
}

Check warning on line 52 in internal/form_builder.go

View check run for this annotation

Codecov / codecov/patch

internal/form_builder.go#L51-L52

Added lines #L51 - L52 were not covered by tests

h := make(textproto.MIMEHeader)
h.Set(
"Content-Disposition",
Expand All @@ -51,6 +60,10 @@
escapeQuotes(filepath.Base(filename)),
),
)
// content type is optional, but it can be set
if contentType != "" {
h.Set("Content-Type", contentType)
}

Check warning on line 66 in internal/form_builder.go

View check run for this annotation

Codecov / codecov/patch

internal/form_builder.go#L65-L66

Added lines #L65 - L66 were not covered by tests

fieldWriter, err := fb.writer.CreatePart(h)
if err != nil {
Expand Down
Loading