package { import flash.display.MovieClip; import flash.events.MouseEvent; import flash.events.DataEvent; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.IOErrorEvent; import flash.events.TimerEvent; import flash.net.FileReference; import flash.net.FileReferenceList; import flash.net.FileFilter; import flash.net.URLRequest; import flash.utils.Timer; import flashx.textLayout.formats.Float; public class Uploader extends MovieClip { var fileList:FileReferenceList; var filefilters:Array; var req:URLRequest; var jsScope:String = ""; var fieldName:String = "asessionid=0"; var uploadUrl:String = "http://localhost:8080/upload"; var tm:Timer; var speed:Number = 0; var currbytes:Number = 0; var lastbytes:Number = 0; var allFiles:Array = new Array(); var uploadIndex:uint = 0; var currentUploadFile:FileReference; var cancelled:Boolean = false; var modifiedFiles:Array; public function Uploader() { // constructor code this.jsScope = stage.loaderInfo.parameters.jsScope; this.fieldName = stage.loaderInfo.parameters.fieldName; this.tm = new Timer( 1000 ); this.tm.addEventListener( TimerEvent.TIMER, updateSpeed ); this.fileList = new FileReferenceList(); this.fileList.addEventListener( Event.SELECT, handleSelect ); select_btn.addEventListener( MouseEvent.CLICK, handleClick ); select_btn.addEventListener( MouseEvent.MOUSE_OVER, handleMouseEvent ); select_btn.addEventListener( MouseEvent.MOUSE_OUT, handleMouseEvent ); select_btn.addEventListener( MouseEvent.MOUSE_DOWN, handleMouseEvent ); select_btn.addEventListener( MouseEvent.MOUSE_UP, handleMouseEvent ); } public function handleClick( e:MouseEvent ){ var archivesFilter:FileFilter = new FileFilter("Archives", "*.tar.gz;*.tar.bz2;*.tar.xz;*.tar.lzma"); filefilters = [ archivesFilter ]; fileList.browse( filefilters ); ExternalInterface.call(this.jsScope + ".onMouseEvent", "click"); } private function handleSelect( e:Event ){ var files:FileReferenceList = FileReferenceList(e.target); for (var i:uint = 0; i < files.fileList.length; i++) { this.allFiles.push(files.fileList[i]); } ExternalInterface.call(this.jsScope + ".flashLog", "handleSelect:"); ExternalInterface.call(this.jsScope + ".flashLog", this.allFiles); ExternalInterface.call(this.jsScope + ".onSelectFiles", files.fileList); } public function uploadFilesNow( modifiedFiles:Array ){ ExternalInterface.call(this.jsScope + ".flashLog", "--------------------------------------"); ExternalInterface.call(this.jsScope + ".flashLog", "uploadFilesNow -> "); ExternalInterface.call(this.jsScope + ".flashLog", "modifiedFiles: "); ExternalInterface.call(this.jsScope + ".flashLog", modifiedFiles); this.modifiedFiles = modifiedFiles; this.tm.start(); this.cancelled = false; this.uploadIndex = 0; uploadFile(); } private function uploadFile() { ExternalInterface.call(this.jsScope + ".flashLog", "--------------------------------------"); ExternalInterface.call(this.jsScope + ".flashLog", "uploadFile -> "); ExternalInterface.call(this.jsScope + ".flashLog", "allFiles.length: " + this.allFiles.length); ExternalInterface.call(this.jsScope + ".flashLog", "allFiles.uploadIndex: " + this.uploadIndex); ExternalInterface.call(this.jsScope + ".flashLog", "allFiles.cancelled: " + this.cancelled); if (this.uploadIndex == this.allFiles.length || this.cancelled == true) { this.tm.stop(); ExternalInterface.call(this.jsScope + ".onUploadSessionComplete"); return; } var fileObject:Object = this.allFiles[this.uploadIndex]; var found:Boolean = false; var alreadyDone:Boolean = false; for (var i:uint = 0; i < this.modifiedFiles.length; i++) { var modifiedFile:Array = this.modifiedFiles[i]; if (modifiedFile[0] == this.uploadIndex && modifiedFile[1] == fileObject.name) { found = true; } if (found == true && modifiedFile[6] == 100 && modifiedFile[9] == "local") { alreadyDone = true; } } ExternalInterface.call(this.jsScope + ".flashLog", "fileObject.name: " + fileObject.name); ExternalInterface.call(this.jsScope + ".flashLog", "found: " + found); ExternalInterface.call(this.jsScope + ".flashLog", "alreadyDone: " + alreadyDone); if (found == false || alreadyDone == true) { // file was deleted after file-browse or it was already uploaded this.uploadIndex++; return uploadFile(); } this.currentUploadFile = FileReference(fileObject); this.currentUploadFile.addEventListener( Event.CANCEL, handleCancel ); this.currentUploadFile.addEventListener(Event.COMPLETE, handleComplete); this.currentUploadFile.addEventListener(Event.COMPLETE, handleIoError); this.currentUploadFile.addEventListener( ProgressEvent.PROGRESS, handleProgress ); this.currentUploadFile.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, handleUploadComplete ); try { ExternalInterface.call(this.jsScope + ".onStart", this.currentUploadFile, this.uploadIndex); this.req = new URLRequest(); this.req.url = stage.loaderInfo.parameters.uploadUrl + "?uploadIndex=" + this.uploadIndex; this.currentUploadFile.upload(this.req, this.fieldName); } catch (e:Error) { ExternalInterface.call(this.jsScope + ".onError", e); } } private function finish( name:String ) { this.uploadIndex++; uploadFile(); } private function handleCancel( e:Event ){ trace( "canceled !" ); } private function handleComplete( e:Event ){ trace( "complete !" ); } private function handleIoError( e:IOErrorEvent ){ ExternalInterface.call(this.jsScope + ".onError", e); this.tm.stop(); } private function handleProgress( e:ProgressEvent ){ ExternalInterface.call(this.jsScope + ".onProgress", e, this.uploadIndex); this.currbytes = e.bytesLoaded; } private function updateSpeed( e:TimerEvent ){ this.speed = Math.round((this.currbytes - this.lastbytes) / 1024); this.lastbytes = this.currbytes; ExternalInterface.call(this.jsScope + ".onSpeed", this.speed); } private function handleUploadComplete( e:DataEvent ){ ExternalInterface.call(this.jsScope + ".onUploadComplete", e, this.uploadIndex); finish(e.target.name); } private function cancelUpload(){ this.currentUploadFile.cancel(); this.cancelled = true; } private function deleteFile(id, fileName) { var allFiles = new Array(); for (var i=0; i < this.allFiles.length; i++) { var fileObject = this.allFiles[i]; if (fileObject.name != fileName && i != id) { allFiles.push(fileObject); } } this.allFiles = allFiles; ExternalInterface.call(this.jsScope + ".flashLog", "--------------------------------------"); ExternalInterface.call(this.jsScope + ".flashLog", "deleteFile -> "); ExternalInterface.call(this.jsScope + ".flashLog", "id: " + id); ExternalInterface.call(this.jsScope + ".flashLog", "name: " + fileName); ExternalInterface.call(this.jsScope + ".flashLog", this.allFiles); } private function resetData(){ this.allFiles = new Array(); this.modifiedFiles = new Array(); } private function handleMouseEvent(e:MouseEvent):void { if (e.type == MouseEvent.MOUSE_OVER) { ExternalInterface.call(this.jsScope + ".onMouseEvent", "over"); } else if (e.type == MouseEvent.MOUSE_OUT) { ExternalInterface.call(this.jsScope + ".onMouseEvent", "out"); } else if (e.type == MouseEvent.MOUSE_DOWN) { ExternalInterface.call(this.jsScope + ".onMouseEvent", "down"); } else if (e.type == MouseEvent.MOUSE_UP) { ExternalInterface.call(this.jsScope + ".onMouseEvent", "up"); } } } }