mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 16:04:24 +00:00
Multipart image uploads were read through io.LimitReader(part, 20MB), so any image larger than 20MB was silently cut short. The OAuth path (and the Grok media path) then base64-encoded the truncated bytes and sent them upstream, which answered 400 invalid_image_file. The API key path forwards the raw multipart body and was not affected, so the same request worked or failed depending on which account served it. - Raise the per-part limit to 50MB, OpenAI's documented per-image limit for GPT image edits, and return 400 when a part exceeds it. - Read Grok media parts whole; the body is already bounded by the gateway request body limit. - Log each upload's content type and size on the images routing line.
65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func buildImagesEditMultipartBody(t *testing.T, imageSize int) ([]byte, string, []byte) {
|
|
t.Helper()
|
|
image := bytes.Repeat([]byte{0xAB}, imageSize)
|
|
var body bytes.Buffer
|
|
writer := multipart.NewWriter(&body)
|
|
require.NoError(t, writer.WriteField("model", "gpt-image-2"))
|
|
require.NoError(t, writer.WriteField("prompt", "replace background"))
|
|
part, err := writer.CreateFormFile("image", "source.png")
|
|
require.NoError(t, err)
|
|
_, err = part.Write(image)
|
|
require.NoError(t, err)
|
|
require.NoError(t, writer.Close())
|
|
return body.Bytes(), writer.FormDataContentType(), image
|
|
}
|
|
|
|
func parseImagesEditMultipart(t *testing.T, body []byte, contentType string) (*OpenAIImagesRequest, error) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/images/edits", bytes.NewReader(body))
|
|
req.Header.Set("Content-Type", contentType)
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
c.Request = req
|
|
svc := &OpenAIGatewayService{}
|
|
return svc.ParseOpenAIImagesRequest(c, body)
|
|
}
|
|
|
|
func TestParseOpenAIImagesRequest_MultipartKeepsUploadAbove20MB(t *testing.T) {
|
|
body, contentType, image := buildImagesEditMultipartBody(t, 21<<20)
|
|
|
|
parsed, err := parseImagesEditMultipart(t, body, contentType)
|
|
require.NoError(t, err)
|
|
require.Len(t, parsed.Uploads, 1)
|
|
require.Equal(t, len(image), len(parsed.Uploads[0].Data))
|
|
require.True(t, bytes.Equal(image, parsed.Uploads[0].Data))
|
|
}
|
|
|
|
func TestParseOpenAIImagesRequest_MultipartRejectsUploadAbovePartLimit(t *testing.T) {
|
|
body, contentType, _ := buildImagesEditMultipartBody(t, openAIImageMaxUploadPartSize+1)
|
|
|
|
parsed, err := parseImagesEditMultipart(t, body, contentType)
|
|
require.Nil(t, parsed)
|
|
require.ErrorContains(t, err, `multipart field "image" exceeds the 50 MB per-part limit`)
|
|
}
|
|
|
|
func TestParseGrokMediaRequest_MultipartKeepsUploadAbove20MB(t *testing.T) {
|
|
body, contentType, image := buildImagesEditMultipartBody(t, 21<<20)
|
|
|
|
info := ParseGrokMediaRequest(contentType, body)
|
|
require.Len(t, info.Uploads, 1)
|
|
require.Equal(t, len(image), len(info.Uploads[0].Data))
|
|
}
|