Skip to content

Conversation

@crazytonyli
Copy link
Contributor

At the moment, the file upload API is hand-written and hard-coded for the wp/v2/media endpoint. This PR implements a general solution for sending multipart/form-data POST requests.

The existing core media upload endpoint has been migrated to the new approach. To summarize, this is how to implement an endpoint that accepts file uploading via multipart/form-data requests:

#[derive(WpDerivedRequest)]
enum MediaRequest {
    // 1. Use `multipart = true` to mark the endpoint as supporting uploading files.
    #[post(url = "/media", params = &MediaCreateParams, output = MediaWithEditContext, multipart = true)]
    Create,
}

pub struct MediaCreateParams {
    // ...

    // 2. skip serializing the file-related properties.
    #[serde(skip)]
    pub file_path: String,
}

// 3. Implement the `RequiresMultipartForm` trait to provide the files to be uploaded.
impl RequiresMultipartForm for MediaCreateParams {
    fn multipart_form_files(&self) -> HashMap<String, MultipartFormFile> {
        // Key: the parameter name of the form field.
        // Value: file path, mime type, etc.
    }
}

throw RequestExecutionException.MediaFileNotFound(filePath = fileInfo.filePath)
}
val mimeType = fileInfo.mimeType ?: "application/octet-stream"
val requestBody = getRequestBody(file, mimeType, uploadListener)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oguzkocer I'm not sure if the progress reporting is still working as expected. The previous implementation only supported uploading one file, but the new implementation supports uploading many files. I'm not sure if the uploadListener still works.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be OK for this PR because /media only supports uploading a single file.

I am working on adding support for multiple files. However, since I also have a few other fixes/improvements in the Kotlin wrapper, so I want to split it into its own PR.

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 am working on adding support for multiple files.

Just to double check, by "support", do you mean support tracking the progress of uploading multiple files?

Copy link
Contributor

Choose a reason for hiding this comment

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

Kind of, it's tracking cumulative progress. #979.

.map(|(i, file_path)| {
(
// TODO: The backend is not ready yet. This name may need to be changed.
format!("attachment_{i}"),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jkmassel FYI, I presumed the attachment parameter is named "attachment_x" (and the app can control how many attachments are allowed). Still, we can always change this implementation after you have implemented the server side.

Copy link
Contributor

@oguzkocer oguzkocer left a comment

Choose a reason for hiding this comment

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

Looks good :shipit:

throw RequestExecutionException.MediaFileNotFound(filePath = fileInfo.filePath)
}
val mimeType = fileInfo.mimeType ?: "application/octet-stream"
val requestBody = getRequestBody(file, mimeType, uploadListener)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be OK for this PR because /media only supports uploading a single file.

I am working on adding support for multiple files. However, since I also have a few other fixes/improvements in the Kotlin wrapper, so I want to split it into its own PR.

@crazytonyli crazytonyli merged commit 26dcca9 into trunk Oct 27, 2025
22 checks passed
@crazytonyli crazytonyli deleted the multipart-upload branch October 27, 2025 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants