swfupload.proxy.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Proxy Plug-in
  3. Features:
  4. Times an upload to see if it appear suspiciously fast which might indicate a proxy server or anti-virus suite intercepting the upload.
  5. If the upload seems too fast an uploadError event is fired with PROXY error code after the final uploadProgress event.
  6. Adds a SWFUpload setting allowing you to tweak the bytes/s for triggering the error:
  7. proxy_detect_threshold : 256000
  8. Adds an UPLOAD_ERROR entry called PROXY:
  9. function uploadError(file, errorCode, message) {
  10. if (errorCode === SWFUpload.UPLOAD_ERROR.PROXY) {
  11. alert("You might have a proxy!");
  12. }
  13. }
  14. */
  15. var SWFUpload;
  16. if (typeof(SWFUpload) === "function") {
  17. SWFUpload.proxyDetect = {};
  18. SWFUpload.UPLOAD_ERROR.PROXY = -30300;
  19. SWFUpload.prototype.initSettings = (function (oldInitSettings) {
  20. return function (userSettings) {
  21. if (typeof(oldInitSettings) === "function") {
  22. oldInitSettings.call(this, userSettings);
  23. }
  24. this.ensureDefault = function (settingName, defaultValue) {
  25. this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[settingName];
  26. };
  27. // List used to keep the speed stats for the files we are tracking
  28. this.proxyDetectFileStartTimes = {};
  29. this.proxyDetectSettings = {};
  30. this.ensureDefault("proxy_detect_threshold", 256000); // Default is 250 KB per second
  31. this.proxyDetectSettings.user_upload_progress_handler = this.settings.upload_progress_handler;
  32. this.proxyDetectSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
  33. this.settings.upload_progress_handler = SWFUpload.proxyDetect.uploadProgressHandler;
  34. this.settings.upload_complete_handler = SWFUpload.proxyDetect.uploadCompleteHandler;
  35. delete this.ensureDefault;
  36. };
  37. }(SWFUpload.prototype.initSettings));
  38. SWFUpload.proxyDetect.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
  39. var ex1 = null, time, differenceMS, bps;
  40. try {
  41. if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
  42. this.proxyDetectSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
  43. }
  44. } catch (ex1) {
  45. }
  46. if (bytesComplete === 0) {
  47. this.proxyDetectFileStartTimes[file.ID] = new Date();
  48. } else if (bytesComplete === bytesTotal) {
  49. try {
  50. // Calculate the Bps and decide if we should trigger the error
  51. time = new Date();
  52. differenceMS = time.getTime() - this.proxyDetectFileStartTimes[file.ID].getTime();
  53. if (differenceMS === 0) {
  54. differenceMS = 1;
  55. }
  56. bps = bytesTotal / (differenceMS * 1000);
  57. if (bps > parseInt(this.settings.proxy_detect_threshold, 10)) {
  58. this.queueEvent("upload_error_handler", [file, SWFUpload.UPLOAD_ERROR.PROXY, bps]);
  59. }
  60. } catch (ex) {
  61. }
  62. }
  63. if (ex1 !== null) {
  64. throw(ex1);
  65. }
  66. };
  67. SWFUpload.proxyDetect.uploadCompleteHandler = function (file) {
  68. try {
  69. delete this.proxyDetectFileStartTimes[file.ID];
  70. } catch (ex) {
  71. }
  72. if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
  73. return this.proxyDetectSettings.user_upload_progress_handler.call(this, file);
  74. }
  75. };
  76. }