Created
February 1, 2013 11:30
-
-
Save ivansjg/4690785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import java.io.File | |
import java.io.FileInputStream | |
import play.api.libs.iteratee.Enumerator | |
object VideoStream extends Controller { | |
def stream = Action { implicit request => | |
val file = new File("D:/workspace/portal/public/videos/test.mp4") | |
request.headers.get(RANGE) match { | |
case Some(value) => { | |
val range: (Long, Long) = value.substring("bytes=".length).split("-") match { | |
case x if x.length == 1 => (x.head.toLong, file.length() - 1) | |
case x => (x(0).toLong, x(1).toLong) | |
} | |
range match { | |
case (start, end) => | |
val stream = new FileInputStream(file) | |
stream.skip(start) | |
SimpleResult( | |
header = ResponseHeader(PARTIAL_CONTENT, | |
Map( | |
CONNECTION -> "keep-alive", | |
ACCEPT_RANGES -> "bytes", | |
CONTENT_RANGE -> "bytes %d-%d/%d".format(start, end, file.length()), | |
CONTENT_LENGTH -> (end - start + 1).toString, | |
CONTENT_TYPE -> play.api.libs.MimeTypes.forExtension("mp4").get)), | |
body = Enumerator.fromStream(stream)) | |
} | |
} | |
case None => Ok.sendFile(file) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment