1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// FilePayload protocol type
//
// Represents a file to be uploaded with explicit name, MIME type, and buffer.
//
// See: https://playwright.dev/docs/api/class-locator#locator-set-input-files
/// FilePayload represents a file for advanced file uploads.
///
/// Allows explicit control over filename, MIME type, and file contents
/// when uploading files to input elements.
///
/// # Example
///
/// ```ignore
/// # use playwright_rs::protocol::FilePayload;
/// let file = FilePayload::builder()
/// .name("document.pdf".to_string())
/// .mime_type("application/pdf".to_string())
/// .buffer(vec![/* PDF bytes */])
/// .build();
/// ```
///
/// See: <https://playwright.dev/docs/api/class-locator#locator-set-input-files>
#[derive(Debug, Clone)]
pub struct FilePayload {
/// File name
pub name: String,
/// MIME type
pub mime_type: String,
/// File contents as bytes
pub buffer: Vec<u8>,
}
use crate::error::Result;
use mime_guess::from_path;
use std::fs;
use std::path::Path;
impl FilePayload {
/// Creates a new builder for FilePayload
pub fn builder() -> FilePayloadBuilder {
FilePayloadBuilder::default()
}
/// Creates a FilePayload from a file path.
///
/// Automatically detects the MIME type based on the file extension.
/// Reads the file into memory.
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref();
let name = path
.file_name()
.ok_or_else(|| crate::Error::InvalidPath(format!("Path {:?} has no filename", path)))?
.to_string_lossy()
.into_owned();
let mime_type = from_path(path).first_or_octet_stream().to_string();
let buffer = fs::read(path)?;
Ok(Self {
name,
mime_type,
buffer,
})
}
/// Creates a FilePayload from a file path with an explicit MIME type.
pub fn from_file<P: AsRef<Path>>(path: P, mime_type: &str) -> Result<Self> {
let path = path.as_ref();
let name = path
.file_name()
.ok_or_else(|| crate::Error::InvalidPath(format!("Path {:?} has no filename", path)))?
.to_string_lossy()
.into_owned();
let buffer = fs::read(path)?;
Ok(Self {
name,
mime_type: mime_type.to_string(),
buffer,
})
}
}
/// Builder for FilePayload
#[derive(Debug, Clone, Default)]
pub struct FilePayloadBuilder {
name: Option<String>,
mime_type: Option<String>,
buffer: Option<Vec<u8>>,
}
impl FilePayloadBuilder {
/// Sets the file name
pub fn name(mut self, name: String) -> Self {
self.name = Some(name);
self
}
/// Sets the MIME type
pub fn mime_type(mut self, mime_type: String) -> Self {
self.mime_type = Some(mime_type);
self
}
/// Sets the file buffer (contents as bytes)
pub fn buffer(mut self, buffer: Vec<u8>) -> Self {
self.buffer = Some(buffer);
self
}
/// Builds the FilePayload
///
/// # Panics
///
/// Panics if any required field (name, mime_type, buffer) is missing
pub fn build(self) -> FilePayload {
FilePayload {
name: self.name.expect("name is required for FilePayload"),
mime_type: self
.mime_type
.expect("mime_type is required for FilePayload"),
buffer: self.buffer.expect("buffer is required for FilePayload"),
}
}
}