Skip to content

Instantly share code, notes, and snippets.

@kdarkhan
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdarkhan/a73b3111544e6e8f2071 to your computer and use it in GitHub Desktop.
Save kdarkhan/a73b3111544e6e8f2071 to your computer and use it in GitHub Desktop.
Compute sha1 checksum of file in Play framework 2.4.x during upload
val uploadDir = "/tmp/uploads/"
def uploadParted = Action.async(parse.multipartFormData(
handleFilePart {
case FileInfo(partName, fileName, contentType) =>
val tempFile = File.createTempFile("file_prefix", "")
val initialState = (MessageDigest.getInstance("SHA-1"), tempFile, new FileOutputStream(tempFile))
Iteratee.fold[Array[Byte],
(MessageDigest, File, FileOutputStream)](initialState) {
case ((md, file, os), data) =>
md.update(data)
os.write(data)
(md, file, os)
} map {
case (md, file, os) =>
os.close()
(md.digest.map("%02x".format(_)).mkString, file)
}
})
) { implicit request =>
Future {
request.body.file("video") map { x =>
x.ref match {
case (sha: String, file: File) =>
println("sha is " + sha)
Files.moveFile(file, new File(uploadDir + sha), true)
Ok("File uploaded")
}
} getOrElse BadRequest("No video provided")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment