uploader 0.2.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/Gemfile +13 -0
  2. data/README.rdoc +7 -2
  3. data/Rakefile +3 -3
  4. data/VERSION +1 -1
  5. data/app/controllers/uploader/uploads_controller.rb +2 -2
  6. data/app/helpers/uploader_helper.rb +51 -3
  7. data/app/views/uploads/_uploadify.html.erb +16 -0
  8. data/lib/active_record/acts/uploader_upload.rb +7 -6
  9. data/lib/daemons/amazonaws.rb +1 -1
  10. data/lib/uploader/middleware/flash_session_cookie_middleware.rb +4 -3
  11. data/lib/uploader/tasks.rb +1 -1
  12. data/locales/ar.yml +12 -12
  13. data/locales/bg.yml +13 -13
  14. data/locales/ca.yml +13 -13
  15. data/locales/cs.yml +13 -13
  16. data/locales/da.yml +8 -8
  17. data/locales/de.yml +12 -12
  18. data/locales/el.yml +16 -16
  19. data/locales/en.yml +2 -2
  20. data/locales/es.yml +12 -12
  21. data/locales/et.yml +14 -14
  22. data/locales/fa.yml +15 -15
  23. data/locales/fi.yml +14 -14
  24. data/locales/fr.yml +12 -12
  25. data/locales/gl.yml +15 -15
  26. data/locales/hi.yml +15 -15
  27. data/locales/hr.yml +13 -13
  28. data/locales/hu.yml +13 -13
  29. data/locales/id.yml +8 -8
  30. data/locales/it.yml +16 -16
  31. data/locales/iw.yml +12 -12
  32. data/locales/ja.yml +16 -16
  33. data/locales/ko.yml +13 -13
  34. data/locales/lt.yml +10 -10
  35. data/locales/lv.yml +11 -11
  36. data/locales/mt.yml +13 -13
  37. data/locales/nl.yml +8 -8
  38. data/locales/no.yml +8 -8
  39. data/locales/pl.yml +13 -13
  40. data/locales/pt-PT.yml +13 -13
  41. data/locales/ro.yml +13 -13
  42. data/locales/ru.yml +13 -13
  43. data/locales/sk.yml +14 -14
  44. data/locales/sl.yml +13 -13
  45. data/locales/sq.yml +13 -13
  46. data/locales/sr.yml +14 -14
  47. data/locales/sv.yml +6 -6
  48. data/locales/th.yml +18 -18
  49. data/locales/tl.yml +11 -11
  50. data/locales/tr.yml +15 -15
  51. data/locales/uk.yml +14 -14
  52. data/locales/vi.yml +12 -12
  53. data/locales/zh-CN.yml +11 -11
  54. data/locales/zh-TW.yml +11 -11
  55. data/locales/zh.yml +11 -11
  56. data/public/images/{SWFUploadButton.png → swfupload/SWFUploadButton.png} +0 -0
  57. data/public/images/{cancelbutton.gif → swfupload/cancelbutton.gif} +0 -0
  58. data/public/images/uploadify/cancel.png +0 -0
  59. data/public/javascripts/jquery/jquery.uploadify.min.js +26 -0
  60. data/public/javascripts/swf/swfobject.js +4 -0
  61. data/public/stylesheets/uploadify.css +53 -0
  62. data/public/swf/expressInstall.swf +0 -0
  63. data/public/swf/uploadify.swf +0 -0
  64. data/rails/init.rb +9 -1
  65. data/test/rails_root/app/controllers/application_controller.rb +4 -0
  66. data/test/rails_root/app/controllers/default_controller.rb +5 -0
  67. data/test/rails_root/app/models/upload.rb +26 -0
  68. data/test/rails_root/app/models/user.rb +11 -1
  69. data/test/rails_root/app/views/default/index.html.erb +6 -0
  70. data/test/rails_root/app/views/default/uploadify.html.erb +17 -0
  71. data/test/rails_root/config/environment.rb +1 -1
  72. data/test/rails_root/config/environments/test.rb +2 -8
  73. data/test/rails_root/config/routes.rb +1 -1
  74. data/test/rails_root/lib/daemons/amazonaws.rb +1 -1
  75. data/test/rails_root/public/images/cancelbutton.gif +0 -0
  76. data/test/rails_root/public/images/swfupload/SWFUploadButton.png +0 -0
  77. data/test/rails_root/public/images/swfupload/cancelbutton.gif +0 -0
  78. data/test/rails_root/public/images/uploadify/cancel.png +0 -0
  79. data/test/rails_root/public/javascripts/jquery/jquery-ui.js +763 -273
  80. data/test/rails_root/public/javascripts/jquery/jquery.js +150 -15
  81. data/test/rails_root/public/javascripts/jquery/jquery.uploadify.min.js +26 -0
  82. data/test/rails_root/public/javascripts/swf/swfobject.js +4 -0
  83. data/test/rails_root/public/javascripts/swfupload/swfupload.cookies.js +2 -2
  84. data/test/rails_root/public/javascripts/swfupload/swfupload.js +325 -136
  85. data/test/rails_root/public/javascripts/swfupload/swfupload.proxy.js +96 -0
  86. data/test/rails_root/public/javascripts/swfupload/swfupload.queue.js +83 -61
  87. data/test/rails_root/public/javascripts/swfupload/swfupload.speed.js +346 -0
  88. data/test/rails_root/public/javascripts/swfupload/swfupload.swfobject.js +4 -3
  89. data/test/rails_root/public/stylesheets/uploadify.css +53 -0
  90. data/test/rails_root/public/swf/expressInstall.swf +0 -0
  91. data/test/rails_root/public/swf/swfupload.swf +0 -0
  92. data/test/rails_root/public/swf/swfupload_fp9.swf +0 -0
  93. data/test/rails_root/public/swf/uploadify.swf +0 -0
  94. data/test/rails_root/test/factories.rb +1 -1
  95. data/test/rails_root/test/functional/default_controller_test.rb +20 -1
  96. data/test/rails_root/test/functional/uploads_controller_test.rb +21 -21
  97. data/test/rails_root/test/test_helper.rb +0 -1
  98. data/test/rails_root/test/unit/upload_test.rb +131 -32
  99. data/uploader.gemspec +28 -8
  100. metadata +31 -10
  101. data/locales/pt.yml +0 -17
@@ -0,0 +1,96 @@
1
+ /*
2
+ Proxy Plug-in
3
+
4
+ Features:
5
+ Times an upload to see if it appear suspiciously fast which might indicate a proxy server or anti-virus suite intercepting the upload.
6
+ If the upload seems too fast an uploadError event is fired with PROXY error code after the final uploadProgress event.
7
+
8
+ Adds a SWFUpload setting allowing you to tweak the bytes/s for triggering the error:
9
+ proxy_detect_threshold : 256000
10
+
11
+ Adds an UPLOAD_ERROR entry called PROXY:
12
+ function uploadError(file, errorCode, message) {
13
+ if (errorCode === SWFUpload.UPLOAD_ERROR.PROXY) {
14
+ alert("You might have a proxy!");
15
+ }
16
+ }
17
+ */
18
+
19
+ var SWFUpload;
20
+ if (typeof(SWFUpload) === "function") {
21
+ SWFUpload.proxyDetect = {};
22
+ SWFUpload.UPLOAD_ERROR.PROXY = -30300;
23
+
24
+ SWFUpload.prototype.initSettings = (function (oldInitSettings) {
25
+ return function (userSettings) {
26
+ if (typeof(oldInitSettings) === "function") {
27
+ oldInitSettings.call(this, userSettings);
28
+ }
29
+
30
+ this.ensureDefault = function (settingName, defaultValue) {
31
+ this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[settingName];
32
+ };
33
+
34
+ // List used to keep the speed stats for the files we are tracking
35
+ this.proxyDetectFileStartTimes = {};
36
+ this.proxyDetectSettings = {};
37
+
38
+ this.ensureDefault("proxy_detect_threshold", 256000); // Default is 250 KB per second
39
+
40
+ this.proxyDetectSettings.user_upload_progress_handler = this.settings.upload_progress_handler;
41
+ this.proxyDetectSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
42
+
43
+ this.settings.upload_progress_handler = SWFUpload.proxyDetect.uploadProgressHandler;
44
+ this.settings.upload_complete_handler = SWFUpload.proxyDetect.uploadCompleteHandler;
45
+
46
+
47
+ delete this.ensureDefault;
48
+ };
49
+ }(SWFUpload.prototype.initSettings));
50
+
51
+ SWFUpload.proxyDetect.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
52
+ var ex1 = null, time, differenceMS, bps;
53
+ try {
54
+ if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
55
+ this.proxyDetectSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
56
+ }
57
+ } catch (ex1) { }
58
+
59
+
60
+ if (bytesComplete === 0) {
61
+ this.proxyDetectFileStartTimes[file.ID] = new Date();
62
+
63
+ } else if (bytesComplete === bytesTotal) {
64
+ try {
65
+ // Calculate the Bps and decide if we should trigger the error
66
+ time = new Date();
67
+ differenceMS = time.getTime() - this.proxyDetectFileStartTimes[file.ID].getTime();
68
+
69
+ if (differenceMS === 0) {
70
+ differenceMS = 1;
71
+ }
72
+
73
+ bps = bytesTotal / (differenceMS * 1000);
74
+ if (bps > parseInt(this.settings.proxy_detect_threshold, 10)) {
75
+ this.queueEvent("upload_error_handler", [file, SWFUpload.UPLOAD_ERROR.PROXY, bps]);
76
+ }
77
+ } catch (ex) {
78
+ }
79
+ }
80
+
81
+ if (ex1 !== null) {
82
+ throw(ex1);
83
+ }
84
+ };
85
+
86
+ SWFUpload.proxyDetect.uploadCompleteHandler = function (file) {
87
+ try {
88
+ delete this.proxyDetectFileStartTimes[file.ID];
89
+ } catch (ex) {
90
+ }
91
+
92
+ if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
93
+ return this.proxyDetectSettings.user_upload_progress_handler.call(this, file);
94
+ }
95
+ };
96
+ }
@@ -1,77 +1,99 @@
1
1
  /*
2
- Queue Plug-in
3
-
4
- Features:
5
- *Adds a cancelQueue() method for cancelling the entire queue.
6
- *All queued files are uploaded when startUpload() is called.
7
- *If false is returned from uploadComplete then the queue upload is stopped.
8
- If false is not returned (strict comparison) then the queue upload is continued.
9
- *Adds a QueueComplete event that is fired when all the queued files have finished uploading.
10
- Set the event handler with the queue_complete_handler setting.
11
-
12
- */
2
+ Queue Plug-in
3
+
4
+ Features:
5
+ *Adds a cancelQueue() method for cancelling the entire queue.
6
+ *All queued files are uploaded when startUpload() is called.
7
+ *If false is returned from uploadComplete then the queue upload is stopped.
8
+ If false is not returned (strict comparison) then the queue upload is continued.
9
+ *Adds a QueueComplete event that is fired when all the queued files have finished uploading.
10
+ Set the event handler with the queue_complete_handler setting.
11
+
12
+ */
13
13
 
14
14
  var SWFUpload;
15
15
  if (typeof(SWFUpload) === "function") {
16
16
  SWFUpload.queue = {};
17
-
17
+
18
18
  SWFUpload.prototype.initSettings = (function (oldInitSettings) {
19
- return function () {
19
+ return function (userSettings) {
20
+ userSettings = this.tmp_settings; // This fixes a bug where userSettings is undefined
20
21
  if (typeof(oldInitSettings) === "function") {
21
- oldInitSettings.call(this);
22
+ oldInitSettings.call(this, userSettings);
22
23
  }
23
-
24
- this.customSettings.queue_cancelled_flag = false;
25
- this.customSettings.queue_upload_count = 0;
26
-
27
- this.settings.user_upload_complete_handler = this.settings.upload_complete_handler;
24
+
25
+ this.queueSettings = {};
26
+
27
+ this.queueSettings.queue_cancelled_flag = false;
28
+ this.queueSettings.queue_upload_count = 0;
29
+
30
+ this.queueSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
31
+ this.queueSettings.user_upload_start_handler = this.settings.upload_start_handler;
28
32
  this.settings.upload_complete_handler = SWFUpload.queue.uploadCompleteHandler;
29
-
30
- this.settings.queue_complete_handler = this.settings.queue_complete_handler || null;
33
+ this.settings.upload_start_handler = SWFUpload.queue.uploadStartHandler;
34
+
35
+ this.settings.queue_complete_handler = userSettings.queue_complete_handler || null;
31
36
  };
32
- })(SWFUpload.prototype.initSettings);
37
+ })(SWFUpload.prototype.initSettings);
33
38
 
34
- SWFUpload.prototype.startUpload = function (fileID) {
35
- this.customSettings.queue_cancelled_flag = false;
36
- this.callFlash("StartUpload", false, [fileID]);
37
- };
39
+ SWFUpload.prototype.startUpload = function (fileID) {
40
+ this.queueSettings.queue_cancelled_flag = false;
41
+ this.callFlash("StartUpload", [fileID]);
42
+ };
38
43
 
39
- SWFUpload.prototype.cancelQueue = function () {
40
- this.customSettings.queue_cancelled_flag = true;
41
- this.stopUpload();
44
+ SWFUpload.prototype.cancelQueue = function () {
45
+ this.queueSettings.queue_cancelled_flag = true;
46
+ this.stopUpload();
47
+
48
+ var stats = this.getStats();
49
+ while (stats.files_queued > 0) {
50
+ this.cancelUpload();
51
+ stats = this.getStats();
52
+ }
53
+ };
54
+
55
+ SWFUpload.queue.uploadStartHandler = function (file) {
56
+ var returnValue;
57
+ if (typeof(this.queueSettings.user_upload_start_handler) === "function") {
58
+ returnValue = this.queueSettings.user_upload_start_handler.call(this, file);
59
+ }
60
+
61
+ // To prevent upload a real "FALSE" value must be returned, otherwise default to a real "TRUE" value.
62
+ returnValue = (returnValue === false) ? false : true;
63
+
64
+ this.queueSettings.queue_cancelled_flag = !returnValue;
42
65
 
43
- var stats = this.getStats();
44
- while (stats.files_queued > 0) {
45
- this.cancelUpload();
46
- stats = this.getStats();
47
- }
48
- };
49
-
50
- SWFUpload.queue.uploadCompleteHandler = function (file) {
51
- var user_upload_complete_handler = this.settings.user_upload_complete_handler;
52
- var continueUpload;
53
-
54
- if (file.file_status === SWFUpload.FILE_STATUS.COMPLETE) {
55
- this.customSettings.queue_upload_count++;
56
- }
66
+ return returnValue;
67
+ };
68
+
69
+ SWFUpload.queue.uploadCompleteHandler = function (file) {
70
+ var user_upload_complete_handler = this.queueSettings.user_upload_complete_handler;
71
+ var continueUpload;
72
+
73
+ if (file.filestatus === SWFUpload.FILE_STATUS.COMPLETE) {
74
+ this.queueSettings.queue_upload_count++;
75
+ }
57
76
 
58
- if (typeof(user_upload_complete_handler) === "function") {
59
- continueUpload = (user_upload_complete_handler.call(this, file) === false) ? false : true;
77
+ if (typeof(user_upload_complete_handler) === "function") {
78
+ continueUpload = (user_upload_complete_handler.call(this, file) === false) ? false : true;
79
+ } else if (file.filestatus === SWFUpload.FILE_STATUS.QUEUED) {
80
+ // If the file was stopped and re-queued don't restart the upload
81
+ continueUpload = false;
82
+ } else {
83
+ continueUpload = true;
84
+ }
85
+
86
+ if (continueUpload) {
87
+ var stats = this.getStats();
88
+ if (stats.files_queued > 0 && this.queueSettings.queue_cancelled_flag === false) {
89
+ this.startUpload();
90
+ } else if (this.queueSettings.queue_cancelled_flag === false) {
91
+ this.queueEvent("queue_complete_handler", [this.queueSettings.queue_upload_count]);
92
+ this.queueSettings.queue_upload_count = 0;
60
93
  } else {
61
- continueUpload = true;
62
- }
63
-
64
- if (continueUpload) {
65
- var stats = this.getStats();
66
- if (stats.files_queued > 0 && this.customSettings.queue_cancelled_flag === false) {
67
- this.startUpload();
68
- } else if (this.customSettings.queue_cancelled_flag === false) {
69
- this.queueEvent("queue_complete_handler", [this.customSettings.queue_upload_count]);
70
- this.customSettings.queue_upload_count = 0;
71
- } else {
72
- this.customSettings.queue_cancelled_flag = false;
73
- this.customSettings.queue_upload_count = 0;
74
- }
94
+ this.queueSettings.queue_cancelled_flag = false;
95
+ this.queueSettings.queue_upload_count = 0;
75
96
  }
76
- };
77
- }
97
+ }
98
+ };
99
+ }
@@ -0,0 +1,346 @@
1
+ /*
2
+ Speed Plug-in
3
+
4
+ Features:
5
+ *Adds several properties to the 'file' object indicated upload speed, time left, upload time, etc.
6
+ - currentSpeed -- String indicating the upload speed, bits per second
7
+ - averageSpeed -- Overall average upload speed, bits per second
8
+ - movingAverageSpeed -- Speed over averaged over the last several measurements, bits per second
9
+ - timeRemaining -- Estimated remaining upload time in seconds
10
+ - timeElapsed -- Number of seconds passed for this upload
11
+ - percentUploaded -- Percentage of the file uploaded (0 to 100)
12
+ - sizeUploaded -- Formatted size uploaded so far, bytes
13
+
14
+ *Adds setting 'moving_average_history_size' for defining the window size used to calculate the moving average speed.
15
+
16
+ *Adds several Formatting functions for formatting that values provided on the file object.
17
+ - SWFUpload.speed.formatBPS(bps) -- outputs string formatted in the best units (Gbps, Mbps, Kbps, bps)
18
+ - SWFUpload.speed.formatTime(seconds) -- outputs string formatted in the best units (x Hr y M z S)
19
+ - SWFUpload.speed.formatSize(bytes) -- outputs string formatted in the best units (w GB x MB y KB z B )
20
+ - SWFUpload.speed.formatPercent(percent) -- outputs string formatted with a percent sign (x.xx %)
21
+ - SWFUpload.speed.formatUnits(baseNumber, divisionArray, unitLabelArray, fractionalBoolean)
22
+ - Formats a number using the division array to determine how to apply the labels in the Label Array
23
+ - factionalBoolean indicates whether the number should be returned as a single fractional number with a unit (speed)
24
+ or as several numbers labeled with units (time)
25
+ */
26
+
27
+ var SWFUpload;
28
+ if (typeof(SWFUpload) === "function") {
29
+ SWFUpload.speed = {};
30
+
31
+ SWFUpload.prototype.initSettings = (function (oldInitSettings) {
32
+ return function (userSettings) {
33
+ if (typeof(oldInitSettings) === "function") {
34
+ oldInitSettings.call(this, userSettings);
35
+ }
36
+
37
+ this.ensureDefault = function (settingName, defaultValue) {
38
+ this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[settingName];
39
+ };
40
+
41
+ // List used to keep the speed stats for the files we are tracking
42
+ this.fileSpeedStats = {};
43
+ this.speedSettings = {};
44
+
45
+ this.ensureDefault("moving_average_history_size", "10");
46
+
47
+ this.speedSettings.user_file_queued_handler = this.settings.file_queued_handler;
48
+ this.speedSettings.user_file_queue_error_handler = this.settings.file_queue_error_handler;
49
+ this.speedSettings.user_upload_start_handler = this.settings.upload_start_handler;
50
+ this.speedSettings.user_upload_error_handler = this.settings.upload_error_handler;
51
+ this.speedSettings.user_upload_progress_handler = this.settings.upload_progress_handler;
52
+ this.speedSettings.user_upload_success_handler = this.settings.upload_success_handler;
53
+ this.speedSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
54
+
55
+ this.settings.file_queued_handler = SWFUpload.speed.fileQueuedHandler;
56
+ this.settings.file_queue_error_handler = SWFUpload.speed.fileQueueErrorHandler;
57
+ this.settings.upload_start_handler = SWFUpload.speed.uploadStartHandler;
58
+ this.settings.upload_error_handler = SWFUpload.speed.uploadErrorHandler;
59
+ this.settings.upload_progress_handler = SWFUpload.speed.uploadProgressHandler;
60
+ this.settings.upload_success_handler = SWFUpload.speed.uploadSuccessHandler;
61
+ this.settings.upload_complete_handler = SWFUpload.speed.uploadCompleteHandler;
62
+
63
+ delete this.ensureDefault;
64
+ };
65
+ })(SWFUpload.prototype.initSettings);
66
+
67
+
68
+ SWFUpload.speed.fileQueuedHandler = function (file) {
69
+ if (typeof this.speedSettings.user_file_queued_handler === "function") {
70
+ file = SWFUpload.speed.extendFile(file);
71
+
72
+ return this.speedSettings.user_file_queued_handler.call(this, file);
73
+ }
74
+ };
75
+
76
+ SWFUpload.speed.fileQueueErrorHandler = function (file, errorCode, message) {
77
+ if (typeof this.speedSettings.user_file_queue_error_handler === "function") {
78
+ file = SWFUpload.speed.extendFile(file);
79
+
80
+ return this.speedSettings.user_file_queue_error_handler.call(this, file, errorCode, message);
81
+ }
82
+ };
83
+
84
+ SWFUpload.speed.uploadStartHandler = function (file) {
85
+ if (typeof this.speedSettings.user_upload_start_handler === "function") {
86
+ file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
87
+ return this.speedSettings.user_upload_start_handler.call(this, file);
88
+ }
89
+ };
90
+
91
+ SWFUpload.speed.uploadErrorHandler = function (file, errorCode, message) {
92
+ file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
93
+ SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
94
+
95
+ if (typeof this.speedSettings.user_upload_error_handler === "function") {
96
+ return this.speedSettings.user_upload_error_handler.call(this, file, errorCode, message);
97
+ }
98
+ };
99
+ SWFUpload.speed.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
100
+ this.updateTracking(file, bytesComplete);
101
+ file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
102
+
103
+ if (typeof this.speedSettings.user_upload_progress_handler === "function") {
104
+ return this.speedSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
105
+ }
106
+ };
107
+
108
+ SWFUpload.speed.uploadSuccessHandler = function (file, serverData) {
109
+ if (typeof this.speedSettings.user_upload_success_handler === "function") {
110
+ file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
111
+ return this.speedSettings.user_upload_success_handler.call(this, file, serverData);
112
+ }
113
+ };
114
+ SWFUpload.speed.uploadCompleteHandler = function (file) {
115
+ file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
116
+ SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
117
+
118
+ if (typeof this.speedSettings.user_upload_complete_handler === "function") {
119
+ return this.speedSettings.user_upload_complete_handler.call(this, file);
120
+ }
121
+ };
122
+
123
+ // Private: extends the file object with the speed plugin values
124
+ SWFUpload.speed.extendFile = function (file, trackingList) {
125
+ var tracking;
126
+
127
+ if (!file) {
128
+ return file;
129
+ }
130
+
131
+ if (trackingList) {
132
+ tracking = trackingList[file.id];
133
+ }
134
+
135
+ if (tracking) {
136
+ file.currentSpeed = tracking.currentSpeed;
137
+ file.averageSpeed = tracking.averageSpeed;
138
+ file.movingAverageSpeed = tracking.movingAverageSpeed;
139
+ file.timeRemaining = tracking.timeRemaining;
140
+ file.timeElapsed = tracking.timeElapsed;
141
+ file.percentUploaded = tracking.percentUploaded;
142
+ file.sizeUploaded = tracking.bytesUploaded;
143
+
144
+ } else {
145
+ file.currentSpeed = 0;
146
+ file.averageSpeed = 0;
147
+ file.movingAverageSpeed = 0;
148
+ file.timeRemaining = 0;
149
+ file.timeElapsed = 0;
150
+ file.percentUploaded = 0;
151
+ file.sizeUploaded = 0;
152
+ }
153
+
154
+ return file;
155
+ };
156
+
157
+ // Private: Updates the speed tracking object, or creates it if necessary
158
+ SWFUpload.prototype.updateTracking = function (file, bytesUploaded) {
159
+ var tracking = this.fileSpeedStats[file.id];
160
+ if (!tracking) {
161
+ this.fileSpeedStats[file.id] = tracking = {};
162
+ }
163
+
164
+ // Sanity check inputs
165
+ bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
166
+ if (bytesUploaded < 0) {
167
+ bytesUploaded = 0;
168
+ }
169
+ if (bytesUploaded > file.size) {
170
+ bytesUploaded = file.size;
171
+ }
172
+
173
+ var tickTime = (new Date()).getTime();
174
+ if (!tracking.startTime) {
175
+ tracking.startTime = (new Date()).getTime();
176
+ tracking.lastTime = tracking.startTime;
177
+ tracking.currentSpeed = 0;
178
+ tracking.averageSpeed = 0;
179
+ tracking.movingAverageSpeed = 0;
180
+ tracking.movingAverageHistory = [];
181
+ tracking.timeRemaining = 0;
182
+ tracking.timeElapsed = 0;
183
+ tracking.percentUploaded = bytesUploaded / file.size;
184
+ tracking.bytesUploaded = bytesUploaded;
185
+ } else if (tracking.startTime > tickTime) {
186
+ this.debug("When backwards in time");
187
+ } else {
188
+ // Get time and deltas
189
+ var now = (new Date()).getTime();
190
+ var lastTime = tracking.lastTime;
191
+ var deltaTime = now - lastTime;
192
+ var deltaBytes = bytesUploaded - tracking.bytesUploaded;
193
+
194
+ if (deltaBytes === 0 || deltaTime === 0) {
195
+ return tracking;
196
+ }
197
+
198
+ // Update tracking object
199
+ tracking.lastTime = now;
200
+ tracking.bytesUploaded = bytesUploaded;
201
+
202
+ // Calculate speeds
203
+ tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
204
+ tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
205
+
206
+ // Calculate moving average
207
+ tracking.movingAverageHistory.push(tracking.currentSpeed);
208
+ if (tracking.movingAverageHistory.length > this.settings.moving_average_history_size) {
209
+ tracking.movingAverageHistory.shift();
210
+ }
211
+
212
+ tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
213
+
214
+ // Update times
215
+ tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
216
+ tracking.timeElapsed = (now - tracking.startTime) / 1000;
217
+
218
+ // Update percent
219
+ tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
220
+ }
221
+
222
+ return tracking;
223
+ };
224
+ SWFUpload.speed.removeTracking = function (file, trackingList) {
225
+ try {
226
+ trackingList[file.id] = null;
227
+ delete trackingList[file.id];
228
+ } catch (ex) {
229
+ }
230
+ };
231
+
232
+ SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
233
+ var i, unit, unitDivisor, unitLabel;
234
+
235
+ if (baseNumber === 0) {
236
+ return "0 " + unitLabels[unitLabels.length - 1];
237
+ }
238
+
239
+ if (singleFractional) {
240
+ unit = baseNumber;
241
+ unitLabel = unitLabels.length >= unitDivisors.length ? unitLabels[unitDivisors.length - 1] : "";
242
+ for (i = 0; i < unitDivisors.length; i++) {
243
+ if (baseNumber >= unitDivisors[i]) {
244
+ unit = (baseNumber / unitDivisors[i]).toFixed(2);
245
+ unitLabel = unitLabels.length >= i ? " " + unitLabels[i] : "";
246
+ break;
247
+ }
248
+ }
249
+
250
+ return unit + unitLabel;
251
+ } else {
252
+ var formattedStrings = [];
253
+ var remainder = baseNumber;
254
+
255
+ for (i = 0; i < unitDivisors.length; i++) {
256
+ unitDivisor = unitDivisors[i];
257
+ unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
258
+
259
+ unit = remainder / unitDivisor;
260
+ if (i < unitDivisors.length -1) {
261
+ unit = Math.floor(unit);
262
+ } else {
263
+ unit = unit.toFixed(2);
264
+ }
265
+ if (unit > 0) {
266
+ remainder = remainder % unitDivisor;
267
+
268
+ formattedStrings.push(unit + unitLabel);
269
+ }
270
+ }
271
+
272
+ return formattedStrings.join(" ");
273
+ }
274
+ };
275
+
276
+ SWFUpload.speed.formatBPS = function (baseNumber) {
277
+ var bpsUnits = [1073741824, 1048576, 1024, 1], bpsUnitLabels = ["Gbps", "Mbps", "Kbps", "bps"];
278
+ return SWFUpload.speed.formatUnits(baseNumber, bpsUnits, bpsUnitLabels, true);
279
+
280
+ };
281
+ SWFUpload.speed.formatTime = function (baseNumber) {
282
+ var timeUnits = [86400, 3600, 60, 1], timeUnitLabels = ["d", "h", "m", "s"];
283
+ return SWFUpload.speed.formatUnits(baseNumber, timeUnits, timeUnitLabels, false);
284
+
285
+ };
286
+ SWFUpload.speed.formatBytes = function (baseNumber) {
287
+ var sizeUnits = [1073741824, 1048576, 1024, 1], sizeUnitLabels = ["GB", "MB", "KB", "bytes"];
288
+ return SWFUpload.speed.formatUnits(baseNumber, sizeUnits, sizeUnitLabels, true);
289
+
290
+ };
291
+ SWFUpload.speed.formatPercent = function (baseNumber) {
292
+ return baseNumber.toFixed(2) + " %";
293
+ };
294
+
295
+ SWFUpload.speed.calculateMovingAverage = function (history) {
296
+ var vals = [], size, sum = 0.0, mean = 0.0, varianceTemp = 0.0, variance = 0.0, standardDev = 0.0;
297
+ var i;
298
+ var mSum = 0, mCount = 0;
299
+
300
+ size = history.length;
301
+
302
+ // Check for sufficient data
303
+ if (size >= 8) {
304
+ // Clone the array and Calculate sum of the values
305
+ for (i = 0; i < size; i++) {
306
+ vals[i] = history[i];
307
+ sum += vals[i];
308
+ }
309
+
310
+ mean = sum / size;
311
+
312
+ // Calculate variance for the set
313
+ for (i = 0; i < size; i++) {
314
+ varianceTemp += Math.pow((vals[i] - mean), 2);
315
+ }
316
+
317
+ variance = varianceTemp / size;
318
+ standardDev = Math.sqrt(variance);
319
+
320
+ //Standardize the Data
321
+ for (i = 0; i < size; i++) {
322
+ vals[i] = (vals[i] - mean) / standardDev;
323
+ }
324
+
325
+ // Calculate the average excluding outliers
326
+ var deviationRange = 2.0;
327
+ for (i = 0; i < size; i++) {
328
+
329
+ if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
330
+ mCount++;
331
+ mSum += history[i];
332
+ }
333
+ }
334
+
335
+ } else {
336
+ // Calculate the average (not enough data points to remove outliers)
337
+ mCount = size;
338
+ for (i = 0; i < size; i++) {
339
+ mSum += history[i];
340
+ }
341
+ }
342
+
343
+ return mSum / mCount;
344
+ };
345
+
346
+ }