"use strict";(function(){"use strict";var e=require("util");var r=require("http");var n=require("url");var o=require("formidable");var i=require("fs");var t=require("events");var a=require("path");var l=require("./jsUtil");var u=a.resolve(l.getDataPath(),"./uploads");function f(){t.EventEmitter.call(this);if(false===this instanceof f){return new f}if(!l.fsExistsSync(u)){i.mkdirSync(u)}}e.inherits(f,t.EventEmitter);f.prototype.start=function(e){var t=this;function f(e,r){var n="";e.setEncoding("utf8");e.addListener("data",function(e){n+=e;console.log("Received POST data chunk '"+e+"'.")});e.addListener("end",function(){console.log(n);t.emit("inform_data",n);r.end("ok")})}function d(e,r){var n=new o.IncomingForm;n.uploadDir=u;n.parse(e,function(e,n,o){if(undefined==o.upload){t.emit("err","not upload");r.end("not upload ");return}i.rename(o.upload.path,a.resolve(u,"./upload_file"),function(e){if(e){if(l.fsExistsSync("./upload_file")){i.unlinkSync("./upload_file");i.renameSync(o.upload.path,"./upload_file")}else{console.log("file rename error");t.emit("err","file rename error");r.end("file rename error ");return}}i.readFile(a.resolve(u,"./upload_file"),"utf8",function(e,n){var o;if(e){o="err"}else{o=n}console.log(o);t.emit("file_data",o);r.end("ok")})})})}function s(e,r){var n=new o.IncomingForm;n.uploadDir=u;n.parse(e,function(e,n,o){console.log(n);console.log(o);if(undefined==o.upload){t.emit("err","not upload");r.end("not upload ");return}i.rename(o.upload.path,a.resolve(u,o.upload.name),function(e){if(e){t.emit("err","file rename error");r.end("file rename error ");return}t.emit("file",a.resolve(u,o.upload.name));r.end("rec_ok");return})})}function c(e,r){var o=n.parse(e.url).pathname;if(o=="/inform_data"){f(e,r)}else if(o=="/file_data"){d(e,r)}else if(o=="/file"){s(e,r)}else{r.end("404 Not Found");return}}r.createServer(c).listen(e);console.log("Server has started.");console.log(u)};module.exports=f})();