upload.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. notify: function(title) {
  25. var popup;
  26. if (!window.webkitNotifications) return false;
  27. if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
  28. if (window.webkitNotifications.checkPermission()===0&&title) {
  29. popup = window.webkitNotifications.createNotification("", title, "You can now manage your new photo(s).");
  30. popup.show();
  31. }
  32. },
  33. start: {
  34. local: function(files) {
  35. var pre_progress = 0,
  36. formData = new FormData(),
  37. xhr = new XMLHttpRequest(),
  38. albumID = album.getID(),
  39. popup,
  40. progress;
  41. if (files.length<=0) return false;
  42. if (albumID===false||visible.albums()===true) albumID = 0;
  43. formData.append("function", "upload");
  44. formData.append("albumID", albumID);
  45. for (var i = 0; i < files.length; i++) {
  46. 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") {
  47. loadingBar.show("error", "The file format of " + files[i].name + " is not supported.");
  48. return false;
  49. } else {
  50. formData.append(i, files[i]);
  51. }
  52. }
  53. upload.show();
  54. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  55. xhr.open("POST", lychee.api_path);
  56. xhr.onload = function() {
  57. if (xhr.status===200) {
  58. upload.close();
  59. upload.notify("Upload complete");
  60. window.onbeforeunload = null;
  61. if (album.getID()===false) lychee.goto("0");
  62. else album.load(albumID);
  63. }
  64. };
  65. xhr.upload.onprogress = function(e) {
  66. if (e.lengthComputable) {
  67. progress = (e.loaded / e.total * 100 | 0);
  68. if (progress>pre_progress) {
  69. upload.setProgress(progress);
  70. pre_progress = progress;
  71. }
  72. if (progress>=100) {
  73. upload.setIcon("cog");
  74. upload.setText("Processing photos");
  75. }
  76. }
  77. };
  78. $("#upload_files").val("");
  79. xhr.send(formData);
  80. },
  81. url: function() {
  82. var albumID = album.getID(),
  83. params,
  84. extension,
  85. buttons,
  86. link;
  87. if (albumID===false) albumID = 0;
  88. buttons = [
  89. ["Import", function() {
  90. link = $(".message input.text").val();
  91. if (link&&link.length>3) {
  92. extension = link.split('.').pop();
  93. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  94. loadingBar.show("error", "The file format of this link is not supported.");
  95. return false;
  96. }
  97. modal.close();
  98. upload.show("cog", "Importing from URL");
  99. params = "importUrl&url=" + escape(encodeURI(link)) + "&albumID=" + albumID;
  100. lychee.api(params, function(data) {
  101. upload.close();
  102. upload.notify("Import complete");
  103. if (album.getID()===false) lychee.goto("0");
  104. else album.load(albumID);
  105. if (data!==true) lychee.error(null, params, data);
  106. });
  107. } else loadingBar.show("error", "Link to short or too long. Please try another one!");
  108. }],
  109. ["Cancel", function() {}]
  110. ];
  111. 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);
  112. },
  113. server: function() {
  114. var albumID = album.getID(),
  115. params,
  116. buttons;
  117. if (albumID===false) albumID = 0;
  118. buttons = [
  119. ["Import", function() {
  120. modal.close();
  121. upload.show("cog", "Importing photos");
  122. params = "importServer&albumID=" + albumID;
  123. lychee.api(params, function(data) {
  124. upload.close();
  125. upload.notify("Import complete");
  126. if (data==="Notice: Import only contains albums!") {
  127. if (visible.albums()) lychee.load();
  128. else lychee.goto("");
  129. }
  130. else if (album.getID()===false) lychee.goto("0");
  131. else album.load(albumID);
  132. if (data==="Notice: Import only contains albums!") return true;
  133. else if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  134. else if (data!==true) lychee.error(null, params, data);
  135. });
  136. }],
  137. ["Cancel", function() {}]
  138. ];
  139. modal.show("Import from Server", "This action will import all photos and albums which are located in <b>'uploads/import/'</b> of your Lychee installation.", buttons);
  140. },
  141. dropbox: function() {
  142. var albumID = album.getID(),
  143. params;
  144. if (albumID===false) albumID = 0;
  145. lychee.loadDropbox(function() {
  146. Dropbox.choose({
  147. linkType: "direct",
  148. multiselect: true,
  149. success: function(files) {
  150. if (files.length>1) {
  151. for (var i = 0; i < files.length; i++) files[i] = files[i].link;
  152. } else files = files[0].link;
  153. upload.show("cog", "Importing photos");
  154. params = "importUrl&url=" + escape(files) + "&albumID=" + albumID;
  155. lychee.api(params, function(data) {
  156. upload.close();
  157. upload.notify("Import complete");
  158. if (album.getID()===false) lychee.goto("0");
  159. else album.load(albumID);
  160. if (data!==true) lychee.error(null, params, data);
  161. });
  162. }
  163. });
  164. });
  165. }
  166. },
  167. close: function(force) {
  168. if (force===true) {
  169. $(".upload_overlay").remove();
  170. } else {
  171. upload.setProgress(100);
  172. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  173. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  174. }
  175. }
  176. };