summaryrefslogtreecommitdiffstats
path: root/debian/missing-sources/swfupload/Flash/FileItem.as
blob: 9eb39525101d47baaf3edec5010773cf38946365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package {
	import flash.net.FileReference;

	internal class FileItem
	{
		private static var file_id_sequence:Number = 0;		// tracks the file id sequence

		private var postObject:Object;
		public var file_reference:FileReference;
		public var id:String;
		public var index:Number = -1;
		public var file_status:int = 0;
		private var js_object:Object;
		
		public static var FILE_STATUS_QUEUED:int		= -1;
		public static var FILE_STATUS_IN_PROGRESS:int	= -2;
		public static var FILE_STATUS_ERROR:int			= -3;
		public static var FILE_STATUS_SUCCESS:int		= -4;
		public static var FILE_STATUS_CANCELLED:int		= -5;
		public static var FILE_STATUS_NEW:int			= -6;	// This file status should never be sent to JavaScript
		
		public function FileItem(file_reference:FileReference, control_id:String, index:Number)
		{
			this.postObject = {};
			this.file_reference = file_reference;
			this.id = control_id + "_" + (FileItem.file_id_sequence++);
			this.file_status = FileItem.FILE_STATUS_NEW;
			this.index = index;
			
			this.js_object = {
				id: this.id,
				index: this.index,
				post: this.GetPostObject()
			};
			
			// Cleanly attempt to retrieve the FileReference info
			// this can fail and so is wrapped in try..catch
			try {
				this.js_object.name = this.file_reference.name;
				this.js_object.size = this.file_reference.size;
				this.js_object.type = this.file_reference.type || "";
				this.js_object.creationdate = this.file_reference.creationDate || new Date(0);
				this.js_object.modificationdate = this.file_reference.modificationDate || new Date(0);
			} catch (ex:Error) {
				this.file_status = FileItem.FILE_STATUS_ERROR;
			}
			
			this.js_object.filestatus = this.file_status;
		}
		
		public function AddParam(name:String, value:String):void {
			this.postObject[name] = value;
		}
		
		public function RemoveParam(name:String):void {
			delete this.postObject[name];
		}
		
		public function GetPostObject(escape:Boolean = false):Object {
			if (escape) {
				var escapedPostObject:Object = { };
				for (var k:String in this.postObject) {
					if (this.postObject.hasOwnProperty(k)) {
						var escapedName:String = FileItem.EscapeParamName(k);
						escapedPostObject[escapedName] = this.postObject[k];
					}
				}
				return escapedPostObject;
			} else {
				return this.postObject;
			}
		}
		
		// Create the simply file object that is passed to the browser
		public function ToJavaScriptObject():Object {
			this.js_object.filestatus = this.file_status;
			this.js_object.post = this.GetPostObject(true);
		
			return this.js_object;
		}
		
		public function toString():String {
			return "FileItem - ID: " + this.id;
		}
		
		/*
		// The purpose of this function is to escape the property names so when Flash
		// passes them back to javascript they can be interpretted correctly.
		// ***They have to be unescaped again by JavaScript.**
		//
		// This works around a bug where Flash sends objects this way:
		//		object.parametername = "value";
		// instead of
		//		object["parametername"] = "value";
		// This can be a problem if the parameter name has characters that are not
		// allowed in JavaScript identifiers:
		// 		object.parameter.name! = "value";
		// does not work but,
		//		object["parameter.name!"] = "value";
		// would have worked.
		*/
		public static function EscapeParamName(name:String):String {
			name = name.replace(/[^a-z0-9_]/gi, FileItem.EscapeCharacter);
			name = name.replace(/^[0-9]/, FileItem.EscapeCharacter);
			return name;
		}
		public static function EscapeCharacter():String {
			return "$" + ("0000" + arguments[0].charCodeAt(0).toString(16)).substr(-4, 4);
		}
		
	}
}