Created
April 8, 2011 19:30
-
-
Save cr0t/910552 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Upload action (for partial - AJAX uploads) | |
* Works with chunks of uploading files, save them to HDFS directly | |
*/ | |
def ajax() = { | |
response.setHeader("Access-Control-Allow-Origin", "*") | |
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS") | |
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,X-File-Name,Content-Type") | |
response.setHeader("Access-Control-Max-Age", "1728000") | |
if (request.method == "POST") { | |
try { | |
var origin = request.remoteAddress | |
// check that hio app runned as nginx backend or not | |
if (request.headers.get("xrealip") != null) { | |
origin = request.headers.get("xrealip").value | |
} | |
val user_id = request.params.get("user_id").toInt | |
var filename = "" | |
var input_stream = request.body | |
if (request.contentType == "multipart/form-data") { | |
// this part is used by IE browsers (and maybe others) | |
val parser = new play.data.parsing.ApacheMultipartParser | |
val parsed = parser.parse(request.body) | |
val uploads = request.args.get("__UPLOADS").asInstanceOf[java.util.ArrayList[play.data.FileUpload]] | |
filename = uploads.get(0).getFileName | |
input_stream = uploads.get(0).asStream | |
} | |
else if (request.contentType == "application/octet-stream") { | |
filename = request.headers.get("x-file-name").value() | |
} | |
else { | |
throw new Exception("Requested Content-Type do not support") | |
} | |
val upload = HadoopIO.putFile(filename, input_stream, origin, user_id) | |
// IE browsers do not "like" Json("{success:true}") answers, so we give them text/html | |
"{success:true,hash:'" + upload.filehash + "'}" | |
} | |
catch { | |
case e: Exception => | |
Logger.error(e, "AJAX Upload goes wrong") | |
"{success:false}" | |
} | |
} | |
else { | |
"{success:true}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment