Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Create Llama
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
Create Llama
Commits
3e8057a8
Commit
3e8057a8
authored
5 months ago
by
Marcus Schiesser
Browse files
Options
Downloads
Patches
Plain Diff
improve saveDocument
parent
12ed570a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
templates/components/llamaindex/typescript/documents/helper.ts
+29
-15
29 additions, 15 deletions
...ates/components/llamaindex/typescript/documents/helper.ts
with
29 additions
and
15 deletions
templates/components/llamaindex/typescript/documents/helper.ts
+
29
−
15
View file @
3e8057a8
import
fs
from
"
fs
"
;
import
fs
from
"
node:fs
"
;
import
path
from
"
node:path
"
;
import
{
getExtractors
}
from
"
../../engine/loader
"
;
const
MIME_TYPE_TO_EXT
:
Record
<
string
,
string
>
=
{
...
...
@@ -19,7 +20,8 @@ export async function storeAndParseFile(
if
(
!
fileExt
)
throw
new
Error
(
`Unsupported document type:
${
mimeType
}
`
);
const
documents
=
await
loadDocuments
(
fileBuffer
,
mimeType
);
await
saveDocument
(
filename
,
fileBuffer
);
const
filepath
=
path
.
join
(
UPLOADED_FOLDER
,
filename
);
await
saveDocument
(
filepath
,
fileBuffer
);
for
(
const
document
of
documents
)
{
document
.
metadata
=
{
...
document
.
metadata
,
...
...
@@ -41,19 +43,31 @@ async function loadDocuments(fileBuffer: Buffer, mimeType: string) {
return
await
reader
.
loadDataAsContent
(
fileBuffer
);
}
export
async
function
saveDocument
(
filename
:
string
,
fileBuffer
:
Buffer
)
{
const
filepath
=
`
${
UPLOADED_FOLDER
}
/
${
filename
}
`
;
const
fileurl
=
`
${
process
.
env
.
FILESERVER_URL_PREFIX
}
/
${
filepath
}
`
;
// Save document to file server and return the file url
export
async
function
saveDocument
(
filepath
:
string
,
content
:
string
|
Buffer
)
{
if
(
path
.
isAbsolute
(
filepath
))
{
throw
new
Error
(
"
Absolute file paths are not allowed.
"
);
}
const
fileName
=
path
.
basename
(
filepath
);
if
(
!
/^
[
a-zA-Z0-9_.-
]
+$/
.
test
(
fileName
))
{
throw
new
Error
(
"
File name is not allowed to contain any special characters.
"
,
);
}
if
(
!
process
.
env
.
FILESERVER_URL_PREFIX
)
{
throw
new
Error
(
"
FILESERVER_URL_PREFIX environment variable is not set.
"
);
}
if
(
!
fs
.
existsSync
(
UPLOADED_FOLDER
))
{
fs
.
mkdirSync
(
UPLOADED_FOLDER
,
{
recursive
:
true
});
const
dirPath
=
path
.
dirname
(
filepath
);
await
fs
.
promises
.
mkdir
(
dirPath
,
{
recursive
:
true
});
if
(
typeof
content
===
"
string
"
)
{
await
fs
.
promises
.
writeFile
(
filepath
,
content
,
"
utf-8
"
);
}
else
{
await
fs
.
promises
.
writeFile
(
filepath
,
content
);
}
await
fs
.
promises
.
writeFile
(
filepath
,
fileBuffer
);
console
.
log
(
`Saved document file to
${
filepath
}
.\nURL:
${
fileurl
}
`
);
return
{
filename
,
filepath
,
fileurl
,
};
const
fileurl
=
`
${
process
.
env
.
FILESERVER_URL_PREFIX
}
/
${
filepath
}
`
;
console
.
log
(
`Saved document to
${
filepath
}
. Reachable at URL:
${
fileurl
}
`
);
return
fileurl
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment