correct send of video files
This commit is contained in:
parent
c3cae930ce
commit
fd84ddb42f
@ -5,7 +5,7 @@ import com.sun.net.httpserver.HttpHandler
|
||||
import eu.heili.hometheater.basicutils.Log.Log.debug
|
||||
import eu.heili.hometheater.basicutils.Log.Log.warning
|
||||
import eu.heili.hometheater.website.datarequests.login.LoginState
|
||||
import java.io.IOException
|
||||
import java.io.*
|
||||
|
||||
class MainPage : HttpHandler {
|
||||
@Throws(IOException::class)
|
||||
@ -18,12 +18,16 @@ class MainPage : HttpHandler {
|
||||
if (path.contains(".html")) {
|
||||
if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") { //pass only register page
|
||||
sendPage(path, t)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
warning("user not logged in --> redirecting to login page")
|
||||
sendPage("/index.html", t)
|
||||
}
|
||||
} else { //only detect login state on html pages
|
||||
}
|
||||
else {
|
||||
//only detect login state on html pages
|
||||
sendPage(path, t)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,24 +37,71 @@ class MainPage : HttpHandler {
|
||||
if (fs == null && path.contains(".html")) {
|
||||
warning("wrong page sending 404")
|
||||
sendPage("/404Error.html", t)
|
||||
} else if (fs == null) {
|
||||
}
|
||||
else if (fs == null) {
|
||||
warning("requested resource doesnt exist --> $path")
|
||||
} else { // Object exists and is a file: accept with response code 200.
|
||||
}
|
||||
else {
|
||||
// Object exists and is a file: accept with response code 200.
|
||||
var mime = "text/html"
|
||||
val s = path.substring(path.length - 3)
|
||||
if (s == ".js") mime = "application/javascript"
|
||||
if (s == "css") mime = "text/css"
|
||||
if (s == "mp4") {
|
||||
mime = "text/css"
|
||||
sendVideo(fs, t)
|
||||
return
|
||||
}
|
||||
val h = t.responseHeaders
|
||||
h["Content-Type"] = mime
|
||||
|
||||
t.sendResponseHeaders(200, 0)
|
||||
val os = t.responseBody
|
||||
val buffer = ByteArray(0x10000)
|
||||
var count: Int
|
||||
|
||||
// todo check if output stream is even open
|
||||
// fs.skip(4096)
|
||||
while (fs.read(buffer).also { count = it } >= 0) {
|
||||
os.write(buffer, 0, count)
|
||||
}
|
||||
debug("left loop!")
|
||||
fs.close()
|
||||
os.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendVideo(fs: InputStream, t: HttpExchange) {
|
||||
var mime = "video/mp4"
|
||||
var offset = 0
|
||||
t.requestHeaders["Range"]?.get(0)?.apply {
|
||||
offset = Integer.valueOf(this.substring(this.indexOf("bytes=") + 6, this.indexOf("-")))
|
||||
}
|
||||
println(offset)
|
||||
val h = t.responseHeaders
|
||||
h["Content-Type"] = "video/mp4"
|
||||
h["Content-Length"] = "${145310237 - offset}"
|
||||
h["Accept-Ranges"] = "bytes"
|
||||
|
||||
if (offset != 0) {
|
||||
h["Content-Range"] = "bytes $offset-145310236/145310237"
|
||||
}
|
||||
|
||||
t.sendResponseHeaders(200, 0)
|
||||
val os = t.responseBody
|
||||
val buffer = ByteArray(1024)
|
||||
var count: Int
|
||||
|
||||
// todo check if output stream is even open
|
||||
|
||||
// fs.skip(offset.toLong())
|
||||
|
||||
while (fs.read(buffer).also { count = it } >= 0) {
|
||||
|
||||
os.write(buffer, 0, count)
|
||||
}
|
||||
|
||||
fs.close()
|
||||
os.close()
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ class Webserver {
|
||||
val server = HttpServer.create(InetSocketAddress(8080), 0)
|
||||
server.createContext("/", MainPage())
|
||||
// todo insert get and post request sites here!
|
||||
server.executor = null // creates a default executor
|
||||
server.executor = java.util.concurrent.Executors.newCachedThreadPool() // creates a default executor
|
||||
server.start()
|
||||
info("Server available at http://127.0.0.1:8080 now")
|
||||
} catch (e: BindException) {
|
||||
|
@ -17,7 +17,7 @@ public class LoginState {
|
||||
private String email;
|
||||
private int permission;
|
||||
|
||||
boolean loggedin = false;
|
||||
boolean loggedin = true;
|
||||
|
||||
public void logIn() {
|
||||
loggedin = true;
|
||||
|
BIN
src/resources/wwwroot/favicon.ico
Normal file
BIN
src/resources/wwwroot/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.4 KiB |
Loading…
Reference in New Issue
Block a user