swfupload.proxy.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if (bytesComplete === 0) {
  46. this.proxyDetectFileStartTimes[file.ID] = new Date();
  47. } else if (bytesComplete === bytesTotal) {
  48. try {
  49. // Calculate the Bps and decide if we should trigger the error
  50. time = new Date();
  51. differenceMS = time.getTime() - this.proxyDetectFileStartTimes[file.ID].getTime();
  52. if (differenceMS === 0) {
  53. differenceMS = 1;
  54. }
  55. bps = bytesTotal / (differenceMS * 1000);
  56. if (bps > parseInt(this.settings.proxy_detect_threshold, 10)) {
  57. this.queueEvent("upload_error_handler", [file, SWFUpload.UPLOAD_ERROR.PROXY, bps]);
  58. }
  59. } catch (ex) {
  60. }
  61. }
  62. if (ex1 !== null) {
  63. throw(ex1);
  64. }
  65. };
  66. SWFUpload.proxyDetect.uploadCompleteHandler = function (file) {
  67. try {
  68. delete this.proxyDetectFileStartTimes[file.ID];
  69. } catch (ex) {
  70. }
  71. if (typeof this.proxyDetectSettings.user_upload_progress_handler === "function") {
  72. return this.proxyDetectSettings.user_upload_progress_handler.call(this, file);
  73. }
  74. };
  75. }