correct send of video files

This commit is contained in:
lukas-heiligenbrunner 2020-02-29 15:37:54 +01:00
parent c3cae930ce
commit fd84ddb42f
4 changed files with 58 additions and 7 deletions

View File

@ -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.debug
import eu.heili.hometheater.basicutils.Log.Log.warning import eu.heili.hometheater.basicutils.Log.Log.warning
import eu.heili.hometheater.website.datarequests.login.LoginState import eu.heili.hometheater.website.datarequests.login.LoginState
import java.io.IOException import java.io.*
class MainPage : HttpHandler { class MainPage : HttpHandler {
@Throws(IOException::class) @Throws(IOException::class)
@ -18,12 +18,16 @@ class MainPage : HttpHandler {
if (path.contains(".html")) { if (path.contains(".html")) {
if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") { //pass only register page if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") { //pass only register page
sendPage(path, t) sendPage(path, t)
} else { }
else {
warning("user not logged in --> redirecting to login page") warning("user not logged in --> redirecting to login page")
sendPage("/index.html", t) sendPage("/index.html", t)
} }
} else { //only detect login state on html pages }
else {
//only detect login state on html pages
sendPage(path, t) sendPage(path, t)
} }
} }
@ -33,24 +37,71 @@ class MainPage : HttpHandler {
if (fs == null && path.contains(".html")) { if (fs == null && path.contains(".html")) {
warning("wrong page sending 404") warning("wrong page sending 404")
sendPage("/404Error.html", t) sendPage("/404Error.html", t)
} else if (fs == null) { }
else if (fs == null) {
warning("requested resource doesnt exist --> $path") 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" var mime = "text/html"
val s = path.substring(path.length - 3) val s = path.substring(path.length - 3)
if (s == ".js") mime = "application/javascript" if (s == ".js") mime = "application/javascript"
if (s == "css") mime = "text/css" if (s == "css") mime = "text/css"
if (s == "mp4") {
mime = "text/css"
sendVideo(fs, t)
return
}
val h = t.responseHeaders val h = t.responseHeaders
h["Content-Type"] = mime h["Content-Type"] = mime
t.sendResponseHeaders(200, 0) t.sendResponseHeaders(200, 0)
val os = t.responseBody val os = t.responseBody
val buffer = ByteArray(0x10000) val buffer = ByteArray(0x10000)
var count: Int var count: Int
// todo check if output stream is even open
// fs.skip(4096)
while (fs.read(buffer).also { count = it } >= 0) { while (fs.read(buffer).also { count = it } >= 0) {
os.write(buffer, 0, count) os.write(buffer, 0, count)
} }
debug("left loop!")
fs.close() fs.close()
os.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()
}
} }

View File

@ -14,7 +14,7 @@ class Webserver {
val server = HttpServer.create(InetSocketAddress(8080), 0) val server = HttpServer.create(InetSocketAddress(8080), 0)
server.createContext("/", MainPage()) server.createContext("/", MainPage())
// todo insert get and post request sites here! // 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() server.start()
info("Server available at http://127.0.0.1:8080 now") info("Server available at http://127.0.0.1:8080 now")
} catch (e: BindException) { } catch (e: BindException) {

View File

@ -17,7 +17,7 @@ public class LoginState {
private String email; private String email;
private int permission; private int permission;
boolean loggedin = false; boolean loggedin = true;
public void logIn() { public void logIn() {
loggedin = true; loggedin = true;

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB