upload.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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(title, files) {
  9. upload.close(true);
  10. $("body").append(build.uploadModal(title, files));
  11. },
  12. setIcon: function(icon) {
  13. $(".upload_message a").remove();
  14. $(".upload_message").prepend("<a class='icon-" + icon + "'></a>");
  15. },
  16. setProgress: function(progress) {
  17. $(".progressbar div").css("width", progress + "%");
  18. },
  19. setText: function(text) {
  20. $(".progressbar").remove();
  21. $(".upload_message").append("<p>" + text + "</p>");
  22. },
  23. notify: function(title) {
  24. var popup;
  25. if (!window.webkitNotifications) return false;
  26. if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
  27. if (window.webkitNotifications.checkPermission()===0&&title) {
  28. popup = window.webkitNotifications.createNotification("", title, "You can now manage your new photo(s).");
  29. popup.show();
  30. }
  31. },
  32. start: {
  33. local: function(files) {
  34. var albumID = album.getID(),
  35. process = function(files, file) {
  36. var formData = new FormData(),
  37. xhr = new XMLHttpRequest(),
  38. pre_progress = 0,
  39. progress;
  40. formData.append("function", "upload");
  41. formData.append("albumID", albumID);
  42. formData.append(0, file);
  43. xhr.open("POST", lychee.api_path);
  44. xhr.onload = function() {
  45. var wait;
  46. if (xhr.status===200) {
  47. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status")
  48. .html("Finished")
  49. .addClass("success");
  50. file.ready = true;
  51. wait = false;
  52. for (var i = 0; i < files.length; i++) {
  53. if (files[i].ready===false) {
  54. wait = true;
  55. break;
  56. }
  57. }
  58. if (wait===false) {
  59. window.onbeforeunload = null;
  60. $("#upload_files").val("");
  61. if (album.getID()===false) lychee.goto("0");
  62. else album.load(albumID);
  63. }
  64. }
  65. };
  66. xhr.upload.onprogress = function(e) {
  67. if (e.lengthComputable) {
  68. progress = (e.loaded / e.total * 100 | 0);
  69. if (progress>pre_progress) {
  70. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html(progress + "%");
  71. pre_progress = progress;
  72. }
  73. if (progress>=100) {
  74. /*$(".upload_message .rows").animate({
  75. scrollTop: $(".upload_message .rows .row:nth-child(" + (file.num+1) + ")").offset().top - 200
  76. }, 0);*/
  77. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html("Processing");
  78. if (file.next!==null) process(files, file.next);
  79. }
  80. }
  81. };
  82. xhr.send(formData);
  83. }
  84. if (files.length<=0) return false;
  85. if (albumID===false) albumID = 0;
  86. for (var i = 0; i < files.length; i++) {
  87. files[i].num = i;
  88. files[i].ready = false;
  89. files[i].supported = true;
  90. if (i < files.length-1) files[i].next = files[i+1];
  91. else files[i].next = null;
  92. if (files[i].type!=="image/jpeg"&&files[i].type!=="image/jpg"&&files[i].type!=="image/png"&&files[i].type!=="image/gif") {
  93. files[i].ready = true;
  94. files[i].supported = false;
  95. }
  96. }
  97. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  98. upload.show("Uploading", files);
  99. process(files, files[0]);
  100. },
  101. url: function() {
  102. var albumID = album.getID(),
  103. params,
  104. extension,
  105. buttons,
  106. link,
  107. files = [];
  108. if (albumID===false) albumID = 0;
  109. buttons = [
  110. ["Import", function() {
  111. link = $(".message input.text").val();
  112. if (link&&link.length>3) {
  113. extension = link.split('.').pop();
  114. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  115. loadingBar.show("error", "The file format of this link is not supported.");
  116. return false;
  117. }
  118. files[0] = {
  119. name: link,
  120. supported: true
  121. }
  122. upload.show("Importing URL", files);
  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. files = [];
  142. if (albumID===false) albumID = 0;
  143. buttons = [
  144. ["Import", function() {
  145. files[0] = {
  146. name: "uploads/import/",
  147. supported: true
  148. };
  149. upload.show("Importing from server", files);
  150. params = "importServer&albumID=" + albumID;
  151. lychee.api(params, function(data) {
  152. upload.close();
  153. upload.notify("Import complete");
  154. if (data==="Notice: Import only contains albums!") {
  155. if (visible.albums()) lychee.load();
  156. else lychee.goto("");
  157. }
  158. else if (album.getID()===false) lychee.goto("0");
  159. else album.load(albumID);
  160. if (data==="Notice: Import only contains albums!") return true;
  161. else if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  162. else if (data!==true) lychee.error(null, params, data);
  163. });
  164. }],
  165. ["Cancel", function() {}]
  166. ];
  167. 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);
  168. },
  169. dropbox: function() {
  170. var albumID = album.getID(),
  171. params,
  172. links = "";
  173. if (albumID===false) albumID = 0;
  174. lychee.loadDropbox(function() {
  175. Dropbox.choose({
  176. linkType: "direct",
  177. multiselect: true,
  178. success: function(files) {
  179. for (var i = 0; i < files.length; i++) {
  180. links += files[i].link + ",";
  181. files[i] = {
  182. name: files[i].link,
  183. supported: true
  184. };
  185. }
  186. // Remove last comma
  187. links = links.substr(0, links.length-1);
  188. upload.show("Importing from Dropbox", files);
  189. params = "importUrl&url=" + escape(links) + "&albumID=" + albumID;
  190. lychee.api(params, function(data) {
  191. upload.close();
  192. upload.notify("Import complete");
  193. if (album.getID()===false) lychee.goto("0");
  194. else album.load(albumID);
  195. if (data!==true) lychee.error(null, params, data);
  196. });
  197. }
  198. });
  199. });
  200. }
  201. },
  202. close: function(force) {
  203. if (force===true) {
  204. $(".upload_overlay").remove();
  205. } else {
  206. upload.setProgress(100);
  207. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  208. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  209. }
  210. }
  211. };