upload.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @name Album Module
  3. * @description Takes care of every action an album can handle and execute.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. upload = {
  8. show: function(icon, text) {
  9. if (icon===undefined) icon = "upload";
  10. upload.close(true);
  11. $("body").append(build.uploadModal(icon, text));
  12. },
  13. setIcon: function(icon) {
  14. $(".upload_message a").remove();
  15. $(".upload_message").prepend("<a class='icon-" + icon + "'></a>");
  16. },
  17. setProgress: function(progress) {
  18. $(".progressbar div").css("width", progress + "%");
  19. },
  20. setText: function(text) {
  21. $(".progressbar").remove();
  22. $(".upload_message").append("<p>" + text + "</p>");
  23. },
  24. start: {
  25. local: function(files) {
  26. var pre_progress = 0,
  27. formData = new FormData(),
  28. xhr = new XMLHttpRequest(),
  29. albumID = album.getID(),
  30. popup,
  31. progress;
  32. if (files.length<=0) return false;
  33. if (albumID===false) albumID = 0;
  34. formData.append("function", "upload");
  35. formData.append("albumID", albumID);
  36. for (var i = 0; i < files.length; i++) {
  37. if (files[i].type!=="image/jpeg"&&files[i].type!=="image/jpg"&&files[i].type!=="image/png"&&files[i].type!=="image/gif"&&files[i].type!=="image/webp") {
  38. loadingBar.show("error", "The file format of " + files[i].name + " is not supported.");
  39. return false;
  40. } else {
  41. formData.append(i, files[i]);
  42. }
  43. }
  44. upload.show();
  45. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  46. xhr.open("POST", lychee.api_path);
  47. xhr.onload = function() {
  48. if (xhr.status===200) {
  49. upload.close();
  50. // WebKit Notification
  51. if (window.webkitNotifications&&BrowserDetect.browser==="Safari") {
  52. popup = window.webkitNotifications.createNotification("", "Upload complete", "You can now manage your new photo.");
  53. popup.show();
  54. }
  55. window.onbeforeunload = null;
  56. if (album.getID()===false) lychee.goto("0");
  57. else album.load(albumID);
  58. }
  59. };
  60. xhr.upload.onprogress = function(e) {
  61. if (e.lengthComputable) {
  62. progress = (e.loaded / e.total * 100 | 0);
  63. if (progress>pre_progress) {
  64. upload.setProgress(progress);
  65. pre_progress = progress;
  66. }
  67. if (progress>=100) {
  68. upload.setIcon("cog");
  69. upload.setText("Processing photos");
  70. }
  71. }
  72. };
  73. $("#upload_files").val("");
  74. xhr.send(formData);
  75. },
  76. url: function() {
  77. var albumID = album.getID(),
  78. params,
  79. extension,
  80. buttons;
  81. if (albumID===false) albumID = 0;
  82. buttons = [
  83. ["Import", function() {
  84. link = $(".message input.text").val();
  85. if (link&&link.length>3) {
  86. extension = link.split('.').pop();
  87. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  88. loadingBar.show("error", "The file format of this link is not supported.");
  89. return false;
  90. }
  91. modal.close();
  92. upload.show("cog", "Importing from URL");
  93. params = "importUrl&url=" + escape(encodeURI(link)) + "&albumID=" + albumID;
  94. lychee.api(params, function(data) {
  95. upload.close();
  96. if (album.getID()===false) lychee.goto("0");
  97. else album.load(albumID);
  98. if (data!==true) lychee.error(null, params, data);
  99. });
  100. } else loadingBar.show("error", "Link to short or too long. Please try another one!");
  101. }],
  102. ["Cancel", function() {}]
  103. ];
  104. modal.show("Import from Link", "Please enter the direct link to a photo to import it: <input class='text' type='text' placeholder='http://' value='http://'>", buttons);
  105. },
  106. server: function() {
  107. var albumID = album.getID(),
  108. params,
  109. buttons;
  110. if (albumID===false) albumID = 0;
  111. buttons = [
  112. ["Import", function() {
  113. modal.close();
  114. upload.show("cog", "Importing photos");
  115. params = "importServer&albumID=" + albumID;
  116. lychee.api(params, function(data) {
  117. upload.close();
  118. if (album.getID()===false) lychee.goto("0");
  119. else album.load(albumID);
  120. if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  121. else if (data!==true) lychee.error(null, params, data);
  122. });
  123. }],
  124. ["Cancel", function() {}]
  125. ];
  126. modal.show("Import from Server", "This action will import all photos which are located in <b>'uploads/import/'</b> of your Lychee installation.", buttons);
  127. },
  128. dropbox: function() {
  129. var albumID = album.getID(),
  130. params;
  131. if (albumID===false) albumID = 0;
  132. lychee.loadDropbox(function() {
  133. Dropbox.choose({
  134. linkType: "direct",
  135. multiselect: true,
  136. success: function(files) {
  137. if (files.length>1) {
  138. for (var i = 0; i < files.length; i++) files[i] = files[i].link;
  139. } else files = files[0].link;
  140. upload.show("cog", "Importing photos");
  141. params = "importUrl&url=" + escape(files) + "&albumID=" + albumID;
  142. lychee.api(params, function(data) {
  143. upload.close();
  144. if (album.getID()===false) lychee.goto("0");
  145. else album.load(albumID);
  146. if (data!==true) lychee.error(null, params, data);
  147. });
  148. }
  149. });
  150. });
  151. }
  152. },
  153. close: function(force) {
  154. if (force===true) {
  155. $(".upload_overlay").remove();
  156. } else {
  157. upload.setProgress(100);
  158. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  159. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  160. }
  161. }
  162. }