Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3010423
parse-body.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
parse-body.ts
View Options
import
{
Middleware
}
from
"@koa/router"
;
import
{
promises
as
fs
}
from
"fs"
;
import
koaBody
from
"koa-body"
;
import
File
from
"../data-structures/file"
;
import
{
ValidationError
}
from
"../response/errors"
;
export
default
function
parseBody
()
:
Middleware
{
const
koaParser
=
koaBody
({
multipart
:
true
});
return
async
(
ctx
,
next
)
=>
{
await
koaParser
(
ctx
,
()
=>
Promise
.
resolve
());
if
(
!
ctx
.
request
.
body
)
{
ctx
.
request
.
body
=
{};
}
const
promises
:
Promise
<
void
>
[]
=
[];
if
(
ctx
.
request
.
files
)
{
for
(
const
file_name
in
ctx
.
request
.
files
)
{
const
file
=
ctx
.
request
.
files
[
file_name
];
if
(
Array
.
isArray
(
file
))
{
throw
new
ValidationError
(
"Multiple files within one field are not supported"
);
}
if
(
file
.
type
===
"application/json"
&&
file
.
name
===
"blob"
)
{
promises
.
push
(
fs
.
readFile
(
file
.
path
,
"utf-8"
).
then
((
string
)
=>
{
ctx
.
request
.
body
[
file_name
]
=
JSON
.
parse
(
string
);
})
);
}
else
{
promises
.
push
(
File
.
fromPath
(
ctx
.
$app
,
file
.
path
,
file
.
name
).
then
(
(
sealious_file
)
=>
{
ctx
.
request
.
body
[
file_name
]
=
sealious_file
;
}
)
);
}
}
}
await
Promise
.
all
(
promises
);
ctx
.
$app
.
Logger
.
info
(
"REQUEST"
,
"Parsed body"
,
ctx
.
request
.
body
);
ctx
.
$body
=
ctx
.
request
.
body
;
await
next
();
};
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, May 7, 19:42 (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
628895
Default Alt Text
parse-body.ts (1 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment