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.

.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.

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.

1 participant