s3multipart.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. ckan.module('s3multipart', function ($) {
  3. return {
  4. initialize: function () {
  5. var s3_enabled = false;
  6. var isInChoose = false;
  7. var accessKeyId = this.options.accesskeyid;
  8. var secretAccessKey = this.options.secretaccesskey;
  9. var sessionToken = this.options.sessiontoken;
  10. var region = this.options.region;
  11. var bucket = this.options.bucket;
  12. var prefix = this.options.prefix;
  13. $.proxyAll(this, /_on/);
  14. // https://jsfiddle.net/embkay06/kjjcv42L/32/
  15. $(window).on("focus", function (temp) {
  16. if (isInChoose) {
  17. isInChoose = false;
  18. setTimeout(function () {
  19. temp = $("#field-image-upload").val();
  20. if (temp) {
  21. return;
  22. }
  23. s3_enabled = false;
  24. }, 500);
  25. }
  26. });
  27. $('#field-image-upload').change(function (evt) {
  28. var files = evt.target.files;
  29. if (files.length == 0) {
  30. s3_enabled = false;
  31. }
  32. if (s3_enabled) {
  33. var s3 = new AWS.S3({
  34. accessKeyId: accessKeyId,
  35. secretAccessKey: secretAccessKey,
  36. sessionToken: sessionToken,
  37. region: region
  38. });
  39. // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html
  40. // default = 4 concurrent uploads of 5mb parts
  41. var managedUpload = s3.upload({
  42. ACL: 'public-read',
  43. Body: files[0],
  44. Bucket: bucket,
  45. Key: prefix + files[0].name
  46. }, function (error, data) {
  47. // upload finished
  48. if (error) {
  49. // upload failed
  50. $('.meter').hide();
  51. $('#upload-progress').html('Upload failed due to error:' + data);
  52. console.log(data);
  53. }
  54. else if (data) {
  55. // upload succeeded
  56. $('.meter').hide();
  57. $('#upload-progress').html('Upload completed, file available at <a href="' + data['Location']
  58. + '">' + data['Location'] + '</a>');
  59. $('#field-image-url').attr('value', data['Location']).attr('placeholder', data['Location']);
  60. $('#field-image-url')[0].value = data['Location'];
  61. }
  62. });
  63. managedUpload.on('httpUploadProgress', function (progress) {
  64. // progress event handler
  65. // progress.loaded = number of bytes uploaded so far
  66. $('.meter').show().first().children().width(Math.round(progress.loaded / progress.total * 100) + '%');
  67. $('#upload-progress').html(
  68. Math.round(progress.loaded / progress.total * 100)
  69. + '% uploaded (' + progress.loaded + ' of ' + progress.total + ' bytes)');
  70. });
  71. $('.btn-remove-url').on('click', function () {
  72. s3_enabled = false;
  73. managedUpload.abort();
  74. });
  75. $(evt.target).val('');
  76. }
  77. ;
  78. });
  79. $("#advanced-upload").show().on('click', function () {
  80. s3_enabled = true;
  81. isInChoose = true;
  82. $('#field-image-upload').click();
  83. })
  84. //$("#field-image-upload").hide();
  85. }
  86. }
  87. });