Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
pse-trapp-public
IntentFinder
Commits
3649239c
Commit
3649239c
authored
May 16, 2021
by
Chiraz Boukadida
Browse files
#30
Implementation Endpoint Hochladen einer DOCX-Datei
parent
20974698
Changes
5
Show whitespace changes
Inline
Side-by-side
src/ktor-server/src/main/kotlin/de/h_da/fbi/smebt/intentfinder/client/Application.kt
0 → 100644
View file @
3649239c
package
de.h_da.fbi.smebt.intentfinder.client
import
de.h_da.fbi.smebt.intentfinder.server.sources.DocxReader
import
io.ktor.client.*
import
io.ktor.client.engine.cio.*
import
io.ktor.client.request.forms.*
import
io.ktor.client.statement.*
import
io.ktor.http.*
import
java.io.File
suspend
fun
main
()
{
val
client
=
HttpClient
(
CIO
)
val
response
:
HttpResponse
=
client
.
submitFormWithBinaryData
(
url
=
"http://localhost:8080/file"
,
formData
=
formData
{
append
(
"description"
,
"docx Document"
)
append
(
"Dok"
,
File
(
"uploads/Dok.docx"
).
readBytes
(),
Headers
.
build
{
append
(
HttpHeaders
.
ContentType
,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
append
(
HttpHeaders
.
ContentDisposition
,
"filename=Dok.docx"
)
})
}
)
println
(
response
.
readText
())
println
(
response
.
status
)
try
{
val
reader
=
DocxReader
(
"uploads/Dok.docx"
)
val
fileContent
=
reader
.
readFile
()
println
(
fileContent
)
}
catch
(
exception
:
Exception
)
{
print
(
exception
.
message
)
}
}
\ No newline at end of file
src/ktor-server/src/main/kotlin/de/h_da/fbi/smebt/intentfinder/server/Application.kt
View file @
3649239c
...
...
@@ -3,17 +3,21 @@ package de.h_da.fbi.smebt.intentfinder.server
import
de.h_da.fbi.smebt.intentfinder.server.nlp.PythonBridge
import
io.ktor.application.*
import
io.ktor.features.*
import
io.ktor.http.*
import
io.ktor.http.content.*
import
io.ktor.request.*
import
io.ktor.response.*
import
io.ktor.routing.*
import
io.ktor.serialization.*
import
kotlinx.serialization.json.Json
import
registerUploadRoutes
import
java.io.File
fun
main
(
args
:
Array
<
String
>):
Unit
=
io
.
ktor
.
server
.
netty
.
EngineMain
.
main
(
args
)
fun
Application
.
module
()
{
fun
Application
.
module
(
testing
:
Boolean
=
false
)
{
install
(
ContentNegotiation
)
{
json
(
Json
{
prettyPrint
=
true
...
...
@@ -28,6 +32,34 @@ fun Application.module() {
get
(
"/"
)
{
call
.
respondText
(
"IntentFinder is available"
)
}
var
fileDescription
=
""
var
fileName
=
""
post
(
"/file"
)
{
val
multipartData
=
call
.
receiveMultipart
()
multipartData
.
forEachPart
{
part
->
when
(
part
)
{
is
PartData
.
FormItem
->
{
fileDescription
=
part
.
value
}
is
PartData
.
FileItem
->
{
fileName
=
part
.
originalFileName
as
String
var
fileBytes
=
part
.
streamProvider
().
readBytes
()
val
file
=
File
(
"uploads/$fileName"
)
if
(!
file
.
exists
())
{
call
.
respond
(
HttpStatusCode
.
NotFound
)
}
else
{
File
(
"uploads/$fileName"
).
writeBytes
(
fileBytes
)
}
}
}
}
call
.
respondText
(
"$fileDescription is uploaded to 'uploads/$fileName'"
)
}
}
registerUploadRoutes
()
}
src/ktor-server/src/test/Upload.http
0 → 100644
View file @
3649239c
POST http://0.0.0.0:8080/file
Content-Type: multipart/form-data;boundary=FileBoundary
--FileBoundary
Content-Disposition: form-data; name="description"
Content-Type: text/plain
docx Document
--FileBoundary
Content-Disposition: form-data; name="Dok"; filename="Dok.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
< ./Dok.docx
--FileBoundary--
###
\ No newline at end of file
src/ktor-server/src/test/kotlin/de/h_da/fbi/smebt/intentfinder/server/sources/UploadTest.kt
0 → 100644
View file @
3649239c
package
de.h_da.fbi.smebt.intentfinder.server.sources
import
de.h_da.fbi.smebt.intentfinder.server.module
import
io.ktor.http.*
import
io.ktor.server.testing.*
import
org.junit.Test
import
kotlin.test.assertEquals
class
UploadTest
{
@Test
fun
testUploadFile
()
{
withTestApplication
({
module
(
testing
=
true
)
})
{
handleRequest
(
HttpMethod
.
Post
,
"/file"
).
apply
{
assertEquals
(
HttpStatusCode
.
OK
,
response
.
status
())
}
}
}
}
\ No newline at end of file
src/ktor-server/uploads/Dok.docx
0 → 100644
View file @
3649239c
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment