Load document from file path
(
&self,
file_path: &str,
document_type: &str,
)
| 111 | |
| 112 | /// Load document from file path |
| 113 | async fn load_from_file( |
| 114 | &self, |
| 115 | file_path: &str, |
| 116 | document_type: &str, |
| 117 | ) -> GraphBitResult<DocumentContent> { |
| 118 | let path = Path::new(file_path); |
| 119 | |
| 120 | // Check if file exists |
| 121 | if !path.exists() { |
| 122 | return Err(GraphBitError::validation( |
| 123 | "document_loader", |
| 124 | format!("File not found: {file_path}"), |
| 125 | )); |
| 126 | } |
| 127 | |
| 128 | // Check file size |
| 129 | let metadata = std::fs::metadata(path).map_err(|e| { |
| 130 | GraphBitError::validation( |
| 131 | "document_loader", |
| 132 | format!("Failed to read file metadata: {e}"), |
| 133 | ) |
| 134 | })?; |
| 135 | |
| 136 | let file_size = metadata.len() as usize; |
| 137 | if file_size > self.config.max_file_size { |
| 138 | return Err(GraphBitError::validation( |
| 139 | "document_loader", |
| 140 | format!( |
| 141 | "File size ({file_size} bytes) exceeds maximum allowed size ({} bytes)", |
| 142 | self.config.max_file_size |
| 143 | ), |
| 144 | )); |
| 145 | } |
| 146 | |
| 147 | // Extract content based on document type |
| 148 | let content = match document_type.to_lowercase().as_str() { |
| 149 | "txt" => Self::extract_text_content(file_path).await?, |
| 150 | "pdf" => Self::extract_pdf_content(file_path).await?, |
| 151 | "docx" => Self::extract_docx_content(file_path).await?, |
| 152 | "json" => Self::extract_json_content(file_path).await?, |
| 153 | "csv" => Self::extract_csv_content(file_path).await?, |
| 154 | "xml" => Self::extract_xml_content(file_path).await?, |
| 155 | "html" => Self::extract_html_content(file_path).await?, |
| 156 | "xlsb" | "xlsx" | "xls" => Self::extract_excel_content(file_path).await?, |
| 157 | _ => { |
| 158 | return Err(GraphBitError::validation( |
| 159 | "document_loader", |
| 160 | format!( |
| 161 | "Unsupported document type: {document_type}. Supported types: {:?}", |
| 162 | Self::supported_types() |
| 163 | ), |
| 164 | )) |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | let mut doc_metadata = HashMap::new(); |
| 169 | doc_metadata.insert( |
| 170 | "file_size".to_string(), |
no test coverage detected