Blame view

WebRoot/plugins/ueditor/third-party/swfupload/swfupload.proxy.js 3.13 KB
ad5081d3   孙向锦   初始化项目
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
  /*
  	Proxy Plug-in
  	
  	Features:
  		Times an upload to see if it appear suspiciously fast which might indicate a proxy server or anti-virus suite intercepting the upload. 
  		If the upload seems too fast an uploadError event is fired with PROXY error code after the final uploadProgress event.
  		
  		Adds a SWFUpload setting allowing you to tweak the bytes/s for triggering the error:
  			proxy_detect_threshold : 256000
  			
  		Adds an UPLOAD_ERROR entry called PROXY:
  			function uploadError(file, errorCode, message) {
  				if (errorCode === SWFUpload.UPLOAD_ERROR.PROXY) {
  					alert("You might have a proxy!");
  				}
  			}
  	*/
  
  var SWFUpload;
  if (typeof(SWFUpload) === "function") {
  	SWFUpload.proxyDetect = {};
  	SWFUpload.UPLOAD_ERROR.PROXY = -30300;
  	
  	SWFUpload.prototype.initSettings = (function (oldInitSettings) {
  		return function (userSettings) {
  			if (typeof(oldInitSettings) === "function") {
  				oldInitSettings.call(this, userSettings);
  			}
  			
  			this.ensureDefault = function (settingName, defaultValue) {
  				this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[settingName];
  			};
  
  			// List used to keep the speed stats for the files we are tracking
  			this.proxyDetectFileStartTimes = {};
  			this.proxyDetectSettings = {};
  
  			this.ensureDefault("proxy_detect_threshold", 256000); // Default is 250 KB per second
  			
  			this.proxyDetectSettings.user_upload_progress_handler = this.settings.upload_progress_handler;
  			this.proxyDetectSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
  			
  			this.settings.upload_progress_handler = SWFUpload.proxyDetect.uploadProgressHandler;
  			this.settings.upload_complete_handler = SWFUpload.proxyDetect.uploadCompleteHandler;
  			
  			
  			delete this.ensureDefault;
  		};
  	}(SWFUpload.prototype.initSettings));
  
  	SWFUpload.proxyDetect.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
  		var ex1 = null, time, differenceMS, bps;
  		try {
  			if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
  				this.proxyDetectSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
  			}
  		} catch (ex1) { }
  			
  		
  		if (bytesComplete === 0) {
  			this.proxyDetectFileStartTimes[file.ID] = new Date();
  
  		} else if (bytesComplete === bytesTotal) {
  			try {
  				// Calculate the Bps and decide if we should trigger the error
  				time = new Date();
  				differenceMS = time.getTime() - this.proxyDetectFileStartTimes[file.ID].getTime();
  				
  				if (differenceMS === 0) {
  					differenceMS = 1;
  				}
  				
  				bps = bytesTotal / (differenceMS * 1000);
  				if (bps > parseInt(this.settings.proxy_detect_threshold, 10)) {
  					this.queueEvent("upload_error_handler", [file, SWFUpload.UPLOAD_ERROR.PROXY, bps]);
  				}					
  			} catch (ex) {
  			}
  		}
  		
  		if (ex1 !== null) {
  			throw(ex1);
  		}
  	};
  	
  	SWFUpload.proxyDetect.uploadCompleteHandler = function (file) {
  		try {
  			delete this.proxyDetectFileStartTimes[file.ID];
  		} catch (ex) {
  		}
  		
  		if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
  			return this.proxyDetectSettings.user_upload_progress_handler.call(this, file);
  		}
  	};	
  }