Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DHFS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
DragonHive
WyvernWorks
Experiments
DHFS
Commits
3146b1ba
Commit
3146b1ba
authored
2 years ago
by
Anthropy
Browse files
Options
Downloads
Patches
Plain Diff
added an initial version which can down/upload files and fetch metadata from the /files/ endpoint
parent
6e53a283
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
experiments/chatgpt/blobstorage-nodejs/v1.js
+66
-0
66 additions, 0 deletions
experiments/chatgpt/blobstorage-nodejs/v1.js
with
66 additions
and
0 deletions
experiments/chatgpt/blobstorage-nodejs/v1.js
0 → 100644
+
66
−
0
View file @
3146b1ba
const
express
=
require
(
'
express
'
);
const
fs
=
require
(
'
fs
'
);
const
path
=
require
(
'
path
'
);
const
multer
=
require
(
'
multer
'
);
const
config
=
require
(
'
./config.json
'
);
const
app
=
express
();
const
upload
=
multer
({
dest
:
config
.
storageDirectory
});
// Endpoint to upload files
app
.
post
(
'
/upload
'
,
upload
.
single
(
'
file
'
),
async
(
req
,
res
)
=>
{
const
file
=
req
.
file
;
if
(
!
file
)
{
return
res
.
status
(
400
).
send
(
"
No file uploaded
"
);
}
const
metadata
=
req
.
body
.
metadata
||
{};
const
metadataPath
=
path
.
join
(
config
.
metadataDirectory
,
file
.
originalname
);
try
{
fs
.
writeFileSync
(
metadataPath
,
JSON
.
stringify
(
metadata
));
res
.
send
(
"
File uploaded successfully
"
);
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
.
message
);
}
});
// Endpoint to fetch file info and download link
app
.
get
(
'
/files/*
'
,
async
(
req
,
res
)
=>
{
const
fileName
=
req
.
params
[
0
];
const
filePath
=
path
.
join
(
config
.
storageDirectory
,
fileName
);
const
metadataPath
=
path
.
join
(
config
.
metadataDirectory
,
fileName
);
try
{
if
(
fs
.
existsSync
(
filePath
))
{
const
metadata
=
fs
.
existsSync
(
metadataPath
)
?
JSON
.
parse
(
fs
.
readFileSync
(
metadataPath
,
'
utf8
'
))
:
{
message
:
"
No metadata found for this file
"
};
res
.
setHeader
(
'
Content-Type
'
,
'
application/json
'
);
res
.
json
({
fileName
,
metadata
,
downloadUrl
:
`http://
${
req
.
headers
.
host
}
/download/
${
fileName
}
`
});
}
else
{
res
.
status
(
404
).
send
(
"
File not found
"
);
}
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
.
message
);
}
});
// Endpoint to download file
app
.
get
(
'
/download/*
'
,
(
req
,
res
)
=>
{
const
fileName
=
req
.
params
[
0
];
const
filePath
=
path
.
join
(
config
.
storageDirectory
,
fileName
);
if
(
fs
.
existsSync
(
filePath
))
{
res
.
download
(
filePath
);
}
else
{
res
.
status
(
404
).
send
(
"
File not found
"
);
}
});
app
.
listen
(
3000
,
()
=>
console
.
log
(
'
App is listening on port 3000
'
));
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