drop.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var g = {}
  2. $(function () {
  3. var pastearea = $('#pastearea')
  4. var uploadprogress = $('#uploadprogress')
  5. var filepicker = $('#filepicker')
  6. var progress = $('#progressamount')
  7. var progressbg = $('#progressamountbg')
  8. function progressdo(e) {
  9. var percent = (e.loaded / e.total) * 100
  10. progressbg.css('width', percent + '%');
  11. progress.text(Math.floor(percent) + '%')
  12. }
  13. function encrypted(data) {
  14. var formdata = new FormData()
  15. formdata.append('privkey', 'c61540b5ceecd05092799f936e27755f')
  16. formdata.append('ident', data.ident)
  17. formdata.append('file', data.encrypted)
  18. $.ajax({
  19. url: 'https://e.3d3.ca/up',
  20. data: formdata,
  21. cache: false,
  22. processData: false,
  23. contentType: false,
  24. xhr: function () {
  25. var xhr = new XMLHttpRequest()
  26. xhr.upload.addEventListener('progress', progressdo, false)
  27. return xhr
  28. },
  29. type: 'POST'
  30. }).done(function () {
  31. progress.text('Encrypting')
  32. progressbg.css('width', 0);
  33. pastearea.removeClass('hidden')
  34. uploadprogress.addClass('hidden')
  35. window.location = '#' + data.seed
  36. })
  37. }
  38. function doupload(file) {
  39. pastearea.addClass('hidden')
  40. uploadprogress.removeClass('hidden')
  41. crypt.encrypt(file).done(encrypted)
  42. }
  43. pastearea.on('drop', function (e) {
  44. e.preventDefault()
  45. pastearea.removeClass('dragover')
  46. doupload(e.originalEvent.dataTransfer.files[0])
  47. })
  48. pastearea.on('dragleave', function (e) {
  49. e.preventDefault()
  50. e.stopPropagation()
  51. pastearea.removeClass('dragover')
  52. })
  53. pastearea.on('dragover', function (e) {
  54. e.preventDefault()
  55. e.stopPropagation()
  56. pastearea.addClass('dragover')
  57. })
  58. pastearea.click(function () {
  59. $('#filepicker').click()
  60. })
  61. function dataURItoBlob(dataURI) {
  62. // convert base64 to raw binary data held in a string
  63. // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
  64. var byteString = atob(dataURI.split(',')[1]);
  65. // separate out the mime component
  66. var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
  67. // write the bytes of the string to an ArrayBuffer
  68. var ab = new ArrayBuffer(byteString.length);
  69. var ia = new Uint8Array(ab);
  70. for (var i = 0; i < byteString.length; i++) {
  71. ia[i] = byteString.charCodeAt(i);
  72. }
  73. // write the ArrayBuffer to a blob, and you're done
  74. return new Blob([ab], { type: mimeString });
  75. }
  76. // Hack to make firefox catch pastes
  77. var pasteCatcher = $('<pre>').prop('id', 'pastecatcher')
  78. pasteCatcher.prop('contenteditable', true)
  79. $('body').append(pasteCatcher)
  80. g.focusPaste = function () {
  81. setTimeout(function () {
  82. pasteCatcher.focus()
  83. }, 100);
  84. }
  85. $(document).on('click', function (e) {
  86. if (e.target == document.body && pastearea.is(':visible')) {
  87. e.preventDefault()
  88. g.focusPaste();
  89. }
  90. })
  91. $(document).on('paste', function (e) {
  92. if (!pastearea.is(':visible')) {
  93. e.preventDefault()
  94. return
  95. }
  96. var items = e.originalEvent.clipboardData.items
  97. var text = e.originalEvent.clipboardData.getData('text/plain')
  98. if (text) {
  99. doupload(new Blob([text], { type: 'text/plain' }))
  100. } else if (typeof items == 'undefined') {
  101. setTimeout(function () {
  102. if (pasteCatcher.find('img').length) {
  103. var src = pasteCatcher.find('img').prop('src')
  104. if (src.startsWith('data:')) {
  105. doupload(dataURItoBlob(src))
  106. } else {
  107. alert("Firefox (I assume) basically pasted that as a direct embed to the image, we could download then upload it maybe like imgur does")
  108. }
  109. }
  110. }, 0)
  111. } else if (items.length >= 1) {
  112. e.preventDefault()
  113. for (var i = 0; i < items.length; i++) {
  114. var blob = items[i].getAsFile()
  115. if (blob) {
  116. doupload(blob)
  117. break;
  118. }
  119. }
  120. }
  121. })
  122. filepicker.on('change', function (e) {
  123. if (e.target.files.length > 0) {
  124. doupload(e.target.files[0])
  125. }
  126. })
  127. });