1  /**
     2   * com.sekati.net.File
     3   * @version 1.0.3
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8   
     9  import com.sekati.core.CoreObject;
    10  import com.sekati.events.Event;
    11  import com.sekati.events.Dispatcher;
    12  import com.sekati.utils.Delegate;
    13  
    14  import flash.net.FileReference;
    15  
    16  /**
    17   * File handler class for uploading and downloading files and FileReference Events.<br>
    18   * TODO Needs thorough testing to confirm event handling works properly.
    19   */
    20  class com.sekati.net.File extends CoreObject {
    21  
    22  	private var _this:File;
    23  	private var _ref:FileReference;
    24  	private var _listener:Object;
    25  	public var _imageType:Object = {description: "Image Files", extension: "*.jpg;*.gif;*.png"};
    26  	public var _textType:Object = {description:"Text Files", extension:"*.txt; *.rtf"};
    27  	public var _docTypes:Object = {description: "Documents", extension: "*.pdf; *.doc; *.xls"};
    28  	public var _webTypes:Object = {description: "Web Files", extension: "*.html; *.htm; *.xhtml; *.php; *.asp; *.aspx; *.cfm; *.xml; *.xsl; *.xslt; *.css; *.js; *.jsp"};
    29  	public var _flashTypes:Object = {description: "Flash Files", extension: "*.swf; *.fla; *.as; *.flp; *.flv"};
    30  	public var _audioTypes:Object = {description: "Audio Files", extension:"*.mp3, *.aiff, *.wav"};
    31  	public var _videoTypes:Object = {description: "Video Files", extension: "*.mpg; *.mpeg; *.mp4; *.mov; *.qt; *.avi; *.wmv; *.asf"};
    32  	public var _anyType:Object = {description: "All Files", extension:"*.*"};
    33  	public var _allTypes:Array = [ _imageType, _textType, _docTypes, _webTypes, _flashTypes, _audioTypes, _videoTypes, _anyType ];
    34  	// events to dispatch
    35  	public var onSelectEVENT:String = "FILE_onSelect";
    36  	public var onCancelEVENT:String = "FILE_onCancel";
    37  	public var onOpenEVENT:String = "FILE_onOpen";
    38  	public var onProgressEVENT:String = "FILE_onProgress";
    39  	public var onCompleteEVENT:String = "FILE_onComplete";
    40  	public var onHTTPErrorEVENT:String = "FILE_onHTTPError";
    41  	public var onIOErrorEVENT:String = "FILE_onIOError";
    42  	public var onSecurityErrorEVENT:String = "FILE_onSecurityError";
    43  
    44  	/**
    45  	 * Constructor
    46  	 */
    47  	public function File() {
    48  		super( );
    49  		_this = this;
    50  		_listener = new Object( );
    51  		_ref = new FileReference( );
    52  		_ref.addListener( _listener );
    53  		_listener.onSelect = Delegate.create( _this, onSelect );
    54  		_listener.onCancel = Delegate.create( _this, onCancel );
    55  		_listener.onOpen = Delegate.create( _this, onOpen );
    56  		_listener.onProgress = Delegate.create( _this, onProgress );
    57  		_listener.onComplete = Delegate.create( _this, onComplete );
    58  		_listener.onHTTPError = Delegate.create( _this, onHTTPError );
    59  		_listener.onIOError = Delegate.create( _this, onIOError );
    60  		_listener.onSecurityError = Delegate.create( _this, onSecurityError );
    61  	}
    62  
    63  	/**
    64  	 * prompt user to save a remote file
    65  	 * @param url (String) url of remote file
    66  	 * @param defaultName (String) optional default filename for remote file to be saved as.
    67  	 * @return Boolean - success status of download
    68  	 */
    69  	public function download(url:String, defaultFileName:String):Boolean {
    70  		return _ref.download( url, defaultFileName );
    71  	}
    72  
    73  	/**
    74  	 * Displays a file-browsing dialog box for the user to select a file to upload.
    75  	 * @param type (Array) optional array of allowed filetypes. If none is passed the _allTypes class array will be used.
    76  	 * @return Boolean - true if the dialogue box was successfully displayed.
    77  	 */
    78  	public function browse(type:Array):Boolean {
    79  		if (!type) type = _allTypes;
    80  		return _ref.browse( type );	
    81  	}
    82  
    83  	/**
    84  	 * Start a file upload (100mb max is supported by the FlashPlayer).
    85  	 * Note: On some browsers, URL strings are limited in length. 
    86  	 * Lengths greater than 256 characters may fail on some browsers or servers.
    87  	 * @param url (String) the server url to upload file to.
    88  	 * @param uploadDataField (String) optional field name that precedes the file data in the upload POST. The default is "Filedata".
    89  	 * @param testUpload (Boolean) optional 0byte upload test for windows Flashplayer only. The default is false.
    90  	 * @return Boolean - false if upload fails for any reason.
    91  	 */
    92  	public function upload(url:String, uploadDataFieldName:String, testUpload:Boolean):Boolean {
    93  		if (!uploadDataFieldName) uploadDataFieldName = "Filedata";
    94  		if (testUpload == undefined) testUpload = false;
    95  		return _ref.upload( url, uploadDataFieldName, testUpload );
    96  	}
    97  
    98  	// FileReference getters
    99  	
   100  	/**
   101  	 * file creation date
   102  	 * @return Date
   103  	 */
   104  	public function get creationDate():Date {
   105  		return _ref.creationDate;	
   106  	}
   107  
   108  	/**
   109  	 * macintosh creator type of the file
   110  	 * @return String
   111  	 */
   112  	public function get creator():String {
   113  		return _ref.creator;	
   114  	}
   115  
   116  	/**
   117  	 * date the file was last modified on local disk
   118  	 * @return Date
   119  	 */
   120  	public function get modificationDate():Date {
   121  		return _ref.modificationDate;	
   122  	}
   123  
   124  	/**
   125  	 * the file name on local disk
   126  	 * @return String
   127  	 */
   128  	public function get name():String {
   129  		return _ref.name;	
   130  	}
   131  
   132  	/**
   133  	 * the file size on local disk in bytes
   134  	 * @return Number
   135  	 */
   136  	public function get size():Number {
   137  		return _ref.size;	
   138  	}
   139  
   140  	/**
   141  	 * the file type
   142  	 * @return String
   143  	 */
   144  	public function get type():String {
   145  		return _ref.type;	
   146  	}
   147  
   148  	// FileReference Event handlers	
   149  	private function onSelect(file:FileReference):Void {
   150  		trace( "onSelect: " + file.name );
   151  		Dispatcher( new Event( onSelectEVENT, _this, {file: file} ) );
   152  		/*
   153  	    if(!file.upload("http://www.yourdomain.com/yourUploadHandlerScript.cfm")) {
   154  	        trace("Upload dialog failed to open.");
   155  	    }
   156  	    */
   157  	}
   158  
   159  	private function onCancel(file:FileReference):Void {
   160  		trace( "onCancel" );
   161  		Dispatcher( new Event( onCancelEVENT, _this, {file: file} ) );
   162  	}
   163  
   164  	private function onOpen(file:FileReference):Void {
   165  		trace( "onOpen: " + file.name );
   166  		Dispatcher( new Event( onOpenEVENT, _this, {name: file.name} ) );
   167  	}
   168  
   169  	private function onProgress(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
   170  		trace( "onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal );
   171  		Dispatcher( new Event( onProgressEVENT, _this, {bytesLoaded: bytesLoaded, bytesTotal: bytesTotal} ) );
   172  	}
   173  
   174  	private function onComplete(file:FileReference):Void {
   175  		trace( "onComplete: " + file.name );
   176  		Dispatcher( new Event( onCompleteEVENT, _this, {file:file} ) );
   177  	}
   178  
   179  	private function onHTTPError(file:FileReference):Void {
   180  		trace( "onHTTPError: " + file.name );
   181  		Dispatcher( new Event( onHTTPErrorEVENT, _this, {name: file.name} ) );
   182  	}
   183  
   184  	private function onIOError(file:FileReference):Void {
   185  		trace( "onIOError: " + file.name );
   186  		Dispatcher( new Event( onIOErrorEVENT, _this, {name: file.name} ) );
   187  	}	
   188  
   189  	private function onSecurityError(file:FileReference, errorString:String):Void {
   190  		trace( "onSecurityError: " + file.name + " errorString: " + errorString );
   191  		Dispatcher( new Event( onSecurityErrorEVENT, _this, {name: file.name, errorString: errorString} ) );
   192  	}
   193  }