Load document from URL
(
&self,
url: &str,
document_type: &str,
)
| 187 | |
| 188 | /// Load document from URL |
| 189 | async fn load_from_url( |
| 190 | &self, |
| 191 | url: &str, |
| 192 | document_type: &str, |
| 193 | ) -> GraphBitResult<DocumentContent> { |
| 194 | // Validate URL format |
| 195 | if !url.starts_with("http://") && !url.starts_with("https://") { |
| 196 | return Err(GraphBitError::validation( |
| 197 | "document_loader", |
| 198 | format!("Invalid URL format: {url}"), |
| 199 | )); |
| 200 | } |
| 201 | |
| 202 | // Create HTTP client with timeout and user agent |
| 203 | let client = reqwest::Client::builder() |
| 204 | .timeout(std::time::Duration::from_secs(30)) |
| 205 | .user_agent("GraphBit Document Loader/1.0") |
| 206 | .build() |
| 207 | .map_err(|e| { |
| 208 | GraphBitError::validation( |
| 209 | "document_loader", |
| 210 | format!("Failed to create HTTP client: {e}"), |
| 211 | ) |
| 212 | })?; |
| 213 | |
| 214 | // Fetch the document |
| 215 | let response = client.get(url).send().await.map_err(|e| { |
| 216 | GraphBitError::validation("document_loader", format!("Failed to fetch URL {url}: {e}")) |
| 217 | })?; |
| 218 | |
| 219 | // Check response status |
| 220 | if !response.status().is_success() { |
| 221 | return Err(GraphBitError::validation( |
| 222 | "document_loader", |
| 223 | format!("HTTP error {}: {url}", response.status()), |
| 224 | )); |
| 225 | } |
| 226 | |
| 227 | // Check content length |
| 228 | if let Some(content_length) = response.content_length() { |
| 229 | if content_length as usize > self.config.max_file_size { |
| 230 | return Err(GraphBitError::validation( |
| 231 | "document_loader", |
| 232 | format!( |
| 233 | "Remote file size ({content_length} bytes) exceeds maximum allowed size ({} bytes)", |
| 234 | self.config.max_file_size |
| 235 | ), |
| 236 | )); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Get content type from response headers |
| 241 | let content_type = response |
| 242 | .headers() |
| 243 | .get("content-type") |
| 244 | .and_then(|h| h.to_str().ok()) |
| 245 | .unwrap_or("") |
| 246 | .to_lowercase(); |
no test coverage detected