Compute sha1 checksum of file in Play framework 2.4.x during upload
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
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