upload.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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, html) {
  9. if (icon===undefined) icon = "upload";
  10. upload.close(true);
  11. $("body").append(build.uploadModal(icon, text, html));
  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 albumID = album.getID(),
  36. html = "";
  37. var process = function(files, file) {
  38. var formData = new FormData(),
  39. xhr = new XMLHttpRequest(),
  40. pre_progress = 0,
  41. progress;
  42. formData.append("function", "upload");
  43. formData.append("albumID", albumID);
  44. formData.append(0, file);
  45. xhr.open("POST", lychee.api_path);
  46. xhr.onload = function() {
  47. var wait;
  48. if (xhr.status===200) {
  49. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status")
  50. .html("Finished")
  51. .addClass("success");
  52. file.ready = true;
  53. wait = false;
  54. for (var i = 0; i < files.length; i++) {
  55. if (files[i].ready===false) {
  56. wait = true;
  57. break;
  58. }
  59. }
  60. if (wait===false) {
  61. $("#upload_files").val("");
  62. if (album.getID()===false) lychee.goto("0");
  63. else album.load(albumID);
  64. }
  65. }
  66. };
  67. xhr.upload.onprogress = function(e) {
  68. if (e.lengthComputable) {
  69. progress = (e.loaded / e.total * 100 | 0);
  70. if (progress>pre_progress) {
  71. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html(progress + "%");
  72. pre_progress = progress;
  73. }
  74. if (progress>=100) {
  75. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html("Processing");
  76. if (file.next!==null) process(files, file.next);
  77. }
  78. }
  79. };
  80. xhr.send(formData);
  81. }
  82. if (files.length<=0) return false;
  83. if (albumID===false) albumID = 0;
  84. html = "<div class='rows'>";
  85. for (var i = 0; i < files.length; i++) {
  86. files[i].num = i;
  87. files[i].ready = false;
  88. if (i < files.length-1) files[i].next = files[i+1];
  89. else files[i].next = null;
  90. if (files[i].type!=="image/jpeg"&&files[i].type!=="image/jpg"&&files[i].type!=="image/png"&&files[i].type!=="image/gif") {
  91. files[i].ready = true;
  92. // Generate html with error
  93. html += "<div class='row'><a class='name'>" + lychee.escapeHTML(files[i].name) + "</a><a class='status error'>Not supported</a></div>";
  94. } else {
  95. // Generate html
  96. html += "<div class='row'><a class='name'>" + lychee.escapeHTML(files[i].name) + "</a><a class='status'></a></div>";
  97. }
  98. }
  99. html += "</div>";
  100. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  101. window.onbeforeunload = null;
  102. upload.show(null, "Uploading", html);
  103. process(files, files[0]);
  104. },
  105. url: function() {
  106. var albumID = album.getID(),
  107. params,
  108. extension,
  109. buttons,
  110. link;
  111. if (albumID===false) albumID = 0;
  112. buttons = [
  113. ["Import", function() {
  114. link = $(".message input.text").val();
  115. if (link&&link.length>3) {
  116. extension = link.split('.').pop();
  117. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  118. loadingBar.show("error", "The file format of this link is not supported.");
  119. return false;
  120. }
  121. modal.close();
  122. upload.show("cog", "Importing from URL");
  123. params = "importUrl&url=" + escape(encodeURI(link)) + "&albumID=" + albumID;
  124. lychee.api(params, function(data) {
  125. upload.close();
  126. upload.notify("Import complete");
  127. if (album.getID()===false) lychee.goto("0");
  128. else album.load(albumID);
  129. if (data!==true) lychee.error(null, params, data);
  130. });
  131. } else loadingBar.show("error", "Link to short or too long. Please try another one!");
  132. }],
  133. ["Cancel", function() {}]
  134. ];
  135. 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);
  136. },
  137. server: function() {
  138. var albumID = album.getID(),
  139. params,
  140. buttons;
  141. if (albumID===false) albumID = 0;
  142. buttons = [
  143. ["Import", function() {
  144. modal.close();
  145. upload.show("cog", "Importing photos");
  146. params = "importServer&albumID=" + albumID;
  147. lychee.api(params, function(data) {
  148. upload.close();
  149. upload.notify("Import complete");
  150. if (data==="Notice: Import only contains albums!") {
  151. if (visible.albums()) lychee.load();
  152. else lychee.goto("");
  153. }
  154. else if (album.getID()===false) lychee.goto("0");
  155. else album.load(albumID);
  156. if (data==="Notice: Import only contains albums!") return true;
  157. else if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  158. else if (data!==true) lychee.error(null, params, data);
  159. });
  160. }],
  161. ["Cancel", function() {}]
  162. ];
  163. 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);
  164. },
  165. dropbox: function() {
  166. var albumID = album.getID(),
  167. params;
  168. if (albumID===false) albumID = 0;
  169. lychee.loadDropbox(function() {
  170. Dropbox.choose({
  171. linkType: "direct",
  172. multiselect: true,
  173. success: function(files) {
  174. if (files.length>1) {
  175. for (var i = 0; i < files.length; i++) files[i] = files[i].link;
  176. } else files = files[0].link;
  177. upload.show("cog", "Importing photos");
  178. params = "importUrl&url=" + escape(files) + "&albumID=" + albumID;
  179. lychee.api(params, function(data) {
  180. upload.close();
  181. upload.notify("Import complete");
  182. if (album.getID()===false) lychee.goto("0");
  183. else album.load(albumID);
  184. if (data!==true) lychee.error(null, params, data);
  185. });
  186. }
  187. });
  188. });
  189. }
  190. },
  191. close: function(force) {
  192. if (force===true) {
  193. $(".upload_overlay").remove();
  194. } else {
  195. upload.setProgress(100);
  196. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  197. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  198. }
  199. }
  200. };