-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.8.0: Fixed optional envvar and added ingest script
- Loading branch information
1 parent
df7e2c5
commit ac04d47
Showing
6 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const axios = require('axios'); | ||
|
||
function isValidFile(filePath) { | ||
return fs.statSync(filePath).isFile() && !path.basename(filePath).startsWith('.'); | ||
} | ||
|
||
async function sendFileToApi(filePath, apiUrl) { | ||
const content = fs.readFileSync(filePath, { encoding: 'base64' }); | ||
const metadata = { source: path.basename(filePath) }; | ||
const payload = { content, metadata }; | ||
|
||
try { | ||
const response = await axios.post(apiUrl, payload); | ||
return response; | ||
} catch (error) { | ||
console.error(`Error sending file: ${error.message}`); | ||
return { status: error.response ? error.response.status : 500 }; | ||
} | ||
} | ||
|
||
async function ingestFiles(directoryPath, apiUrl) { | ||
if (!fs.existsSync(directoryPath) || !fs.statSync(directoryPath).isDirectory()) { | ||
console.error(`Error: ${directoryPath} is not a valid directory.`); | ||
return; | ||
} | ||
|
||
const files = fs.readdirSync(directoryPath, { withFileTypes: true }); | ||
|
||
for (const file of files) { | ||
const filePath = path.join(directoryPath, file.name); | ||
if (file.isDirectory()) { | ||
await ingestFiles(filePath, apiUrl); | ||
} else if (isValidFile(filePath)) { | ||
console.log(`Sending file: ${filePath}`); | ||
const response = await sendFileToApi(filePath, apiUrl); | ||
if (response.status === 200) { | ||
console.log(`Successfully ingested: ${filePath}`); | ||
} else { | ||
console.log(`Failed to ingest ${filePath}. Status code: ${response.status}`); | ||
} | ||
} else { | ||
console.log(`Skipping invalid or hidden file: ${filePath}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
ingestFiles | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters