diff --git a/backend/imagekit/client/upload.go b/backend/imagekit/client/upload.go index 7964f8c46..630f068c4 100644 --- a/backend/imagekit/client/upload.go +++ b/backend/imagekit/client/upload.go @@ -72,7 +72,7 @@ func (ik *ImageKit) Upload(ctx context.Context, file io.Reader, param UploadPara response := &UploadResult{} - formReader, contentType, _, err := rest.MultipartUpload(ctx, file, formParams, "file", param.FileName) + formReader, contentType, _, err := rest.MultipartUpload(ctx, file, formParams, "file", param.FileName, "application/octet-stream") if err != nil { return nil, nil, fmt.Errorf("failed to make multipart upload: %w", err) diff --git a/backend/pcloud/pcloud.go b/backend/pcloud/pcloud.go index ff7fa1a68..ff9f2c5ae 100644 --- a/backend/pcloud/pcloud.go +++ b/backend/pcloud/pcloud.go @@ -1327,7 +1327,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op // opts.Body=0), so upload it as a multipart form POST with // Content-Length set. if size == 0 { - formReader, contentType, overhead, err := rest.MultipartUpload(ctx, in, opts.Parameters, "content", leaf) + formReader, contentType, overhead, err := rest.MultipartUpload(ctx, in, opts.Parameters, "content", leaf, opts.ContentType) if err != nil { return fmt.Errorf("failed to make multipart upload for 0 length file: %w", err) } diff --git a/backend/pikpak/pikpak.go b/backend/pikpak/pikpak.go index 7cce6695e..142905475 100644 --- a/backend/pikpak/pikpak.go +++ b/backend/pikpak/pikpak.go @@ -1384,7 +1384,7 @@ func (f *Fs) uploadByForm(ctx context.Context, in io.Reader, name string, size i for i := range iVal.NumField() { params.Set(iTyp.Field(i).Tag.Get("json"), iVal.Field(i).String()) } - formReader, contentType, overhead, err := rest.MultipartUpload(ctx, in, params, "file", name) + formReader, contentType, overhead, err := rest.MultipartUpload(ctx, in, params, "file", name, "application/octet-stream") if err != nil { return fmt.Errorf("failed to make multipart upload: %w", err) } diff --git a/backend/seafile/webapi.go b/backend/seafile/webapi.go index 967dbabf0..dc9da9e16 100644 --- a/backend/seafile/webapi.go +++ b/backend/seafile/webapi.go @@ -688,7 +688,7 @@ func (f *Fs) upload(ctx context.Context, in io.Reader, uploadLink, filePath stri "need_idx_progress": {"true"}, "replace": {"1"}, } - formReader, contentType, _, err := rest.MultipartUpload(ctx, in, parameters, "file", f.opt.Enc.FromStandardName(filename)) + formReader, contentType, _, err := rest.MultipartUpload(ctx, in, parameters, "file", f.opt.Enc.FromStandardName(filename), "application/octet-stream") if err != nil { return nil, fmt.Errorf("failed to make multipart upload: %w", err) } diff --git a/backend/zoho/zoho.go b/backend/zoho/zoho.go index 30adb413e..3b40e0828 100644 --- a/backend/zoho/zoho.go +++ b/backend/zoho/zoho.go @@ -817,7 +817,7 @@ func (f *Fs) upload(ctx context.Context, name string, parent string, size int64, params.Set("filename", url.QueryEscape(name)) params.Set("parent_id", parent) params.Set("override-name-exist", strconv.FormatBool(true)) - formReader, contentType, overhead, err := rest.MultipartUpload(ctx, in, nil, "content", name) + formReader, contentType, overhead, err := rest.MultipartUpload(ctx, in, nil, "content", name, "application/octet-stream") if err != nil { return nil, fmt.Errorf("failed to make multipart upload: %w", err) } diff --git a/fs/operations/rc_test.go b/fs/operations/rc_test.go index 2d8d0f2af..f8b3cb6f3 100644 --- a/fs/operations/rc_test.go +++ b/fs/operations/rc_test.go @@ -561,7 +561,7 @@ func TestUploadFile(t *testing.T) { assert.NoError(t, currentFile.Close()) }() - formReader, contentType, _, err := rest.MultipartUpload(ctx, currentFile, url.Values{}, "file", testFileName) + formReader, contentType, _, err := rest.MultipartUpload(ctx, currentFile, url.Values{}, "file", testFileName, "application/octet-stream") require.NoError(t, err) httpReq := httptest.NewRequest("POST", "/", formReader) @@ -587,7 +587,7 @@ func TestUploadFile(t *testing.T) { assert.NoError(t, currentFile2.Close()) }() - formReader, contentType, _, err = rest.MultipartUpload(ctx, currentFile2, url.Values{}, "file", testFileName) + formReader, contentType, _, err = rest.MultipartUpload(ctx, currentFile2, url.Values{}, "file", testFileName, "application/octet-stream") require.NoError(t, err) httpReq = httptest.NewRequest("POST", "/", formReader) diff --git a/lib/rest/rest.go b/lib/rest/rest.go index 39eedfc60..f8771f106 100644 --- a/lib/rest/rest.go +++ b/lib/rest/rest.go @@ -395,23 +395,7 @@ func CreateFormFile(w *multipart.Writer, fieldname, filename, contentType string // the int64 returned is the overhead in addition to the file contents, in case Content-Length is required // // NB This doesn't allow setting the content type of the attachment -func MultipartUpload(ctx context.Context, in io.Reader, params url.Values, contentName, fileName string) (io.ReadCloser, string, int64, error) { - return multipartUpload(ctx, in, params, contentName, fileName, "") -} - -// multipartUpload creates an io.Reader which produces an encoded a -// multipart form upload from the params passed in and the passed in -// -// in - the body of the file (may be nil) -// params - the form parameters -// fileName - is the name of the attached file -// contentName - the name of the parameter for the file -// contentType - the content type for the upload -// -// the int64 returned is the overhead in addition to the file contents, in case Content-Length is required -// -// NB This doesn't allow setting the content type of the attachment -func multipartUpload(ctx context.Context, in io.Reader, params url.Values, contentName, fileName, contentType string) (io.ReadCloser, string, int64, error) { +func MultipartUpload(ctx context.Context, in io.Reader, params url.Values, contentName, fileName string, contentType string) (io.ReadCloser, string, int64, error) { bodyReader, bodyWriter := io.Pipe() writer := multipart.NewWriter(bodyWriter) formContentType := writer.FormDataContentType() @@ -568,7 +552,7 @@ func (api *Client) callCodec(ctx context.Context, opts *Opts, request any, respo opts = opts.Copy() var overhead int64 - opts.Body, opts.ContentType, overhead, err = multipartUpload(ctx, opts.Body, params, opts.MultipartContentName, opts.MultipartFileName, opts.MultipartContentType) + opts.Body, opts.ContentType, overhead, err = MultipartUpload(ctx, opts.Body, params, opts.MultipartContentName, opts.MultipartFileName, opts.MultipartContentType) if err != nil { return nil, err }