use crate::as4::mime_packaging::{MimeAttachment, MimePackageBuilder, PayloadFilename};
use crate::core::{AsxError, ErrorCode, ErrorContext, Result};
pub fn package_as_mime(
soap_body: Vec<u8>,
payload: Vec<u8>,
payload_content_id: &str,
payload_content_type: &str,
soap_content_type: &str,
payload_filename: Option<&PayloadFilename>,
) -> Result<(Vec<u8>, String)> {
package_as_mime_with_extra_attachments(
soap_body,
payload,
payload_content_id,
payload_content_type,
soap_content_type,
payload_filename,
Vec::new(),
)
}
#[allow(clippy::too_many_arguments)]
pub fn package_as_mime_with_extra_attachments(
soap_body: Vec<u8>,
payload: Vec<u8>,
payload_content_id: &str,
payload_content_type: &str,
soap_content_type: &str,
payload_filename: Option<&PayloadFilename>,
extra_attachments: Vec<MimeAttachment>,
) -> Result<(Vec<u8>, String)> {
let disposition = match payload_filename {
Some(name) => format!("attachment; filename=\"{}\"", name.as_str()),
None => "attachment".to_string(),
};
let payload_attachment =
MimeAttachment::new(payload_content_id, payload_content_type, payload, "binary")
.with_disposition(disposition);
let builder = MimePackageBuilder::new()
.with_root_soap_content_type(soap_content_type)
.with_soap_body(soap_body)
.add_attachment(payload_attachment)
.add_attachments(extra_attachments);
let content_type = format!(
"{}; start=\"<soap-body@example.com>\"",
builder.content_type(),
);
let mime_body = builder.build().map_err(|e| {
AsxError::new(
ErrorCode::PolicyViolation,
format!("MIME packaging failed: {}", e),
ErrorContext::new("send_mime_package"),
)
})?;
Ok((mime_body, content_type))
}