upload.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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) 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. if (albumID===false) albumID = 0;
  87. buttons = [
  88. ["Import", function() {
  89. link = $(".message input.text").val();
  90. if (link&&link.length>3) {
  91. extension = link.split('.').pop();
  92. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  93. loadingBar.show("error", "The file format of this link is not supported.");
  94. return false;
  95. }
  96. modal.close();
  97. upload.show("cog", "Importing from URL");
  98. params = "importUrl&url=" + escape(encodeURI(link)) + "&albumID=" + albumID;
  99. lychee.api(params, function(data) {
  100. upload.close();
  101. upload.notify("Import complete");
  102. if (album.getID()===false) lychee.goto("0");
  103. else album.load(albumID);
  104. if (data!==true) lychee.error(null, params, data);
  105. });
  106. } else loadingBar.show("error", "Link to short or too long. Please try another one!");
  107. }],
  108. ["Cancel", function() {}]
  109. ];
  110. 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);
  111. },
  112. server: function() {
  113. var albumID = album.getID(),
  114. params,
  115. buttons;
  116. if (albumID===false) albumID = 0;
  117. buttons = [
  118. ["Import", function() {
  119. modal.close();
  120. upload.show("cog", "Importing photos");
  121. params = "importServer&albumID=" + albumID;
  122. lychee.api(params, function(data) {
  123. upload.close();
  124. upload.notify("Import complete");
  125. if (data==="Notice: Import only contains albums!") {
  126. if (visible.albums()) lychee.load();
  127. else lychee.goto("");
  128. }
  129. else if (album.getID()===false) lychee.goto("0");
  130. else album.load(albumID);
  131. if (data==="Notice: Import only contains albums!") return true;
  132. else if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  133. else if (data!==true) lychee.error(null, params, data);
  134. });
  135. }],
  136. ["Cancel", function() {}]
  137. ];
  138. 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);
  139. },
  140. dropbox: function() {
  141. var albumID = album.getID(),
  142. params;
  143. if (albumID===false) albumID = 0;
  144. lychee.loadDropbox(function() {
  145. Dropbox.choose({
  146. linkType: "direct",
  147. multiselect: true,
  148. success: function(files) {
  149. if (files.length>1) {
  150. for (var i = 0; i < files.length; i++) files[i] = files[i].link;
  151. } else files = files[0].link;
  152. upload.show("cog", "Importing photos");
  153. params = "importUrl&url=" + escape(files) + "&albumID=" + albumID;
  154. lychee.api(params, function(data) {
  155. upload.close();
  156. upload.notify("Import complete");
  157. if (album.getID()===false) lychee.goto("0");
  158. else album.load(albumID);
  159. if (data!==true) lychee.error(null, params, data);
  160. });
  161. }
  162. });
  163. });
  164. }
  165. },
  166. close: function(force) {
  167. if (force===true) {
  168. $(".upload_overlay").remove();
  169. } else {
  170. upload.setProgress(100);
  171. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  172. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  173. }
  174. }
  175. }