upload.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. upload.close();
  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. var scrollPos = 0;
  76. if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
  77. $(".upload_message .rows").scrollTop(scrollPos);
  78. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html("Processing");
  79. if (file.next!==null) process(files, file.next);
  80. }
  81. }
  82. };
  83. xhr.send(formData);
  84. }
  85. if (files.length<=0) return false;
  86. if (albumID===false||visible.albums()===true) albumID = 0;
  87. for (var i = 0; i < files.length; i++) {
  88. files[i].num = i;
  89. files[i].ready = false;
  90. files[i].supported = true;
  91. if (i < files.length-1) files[i].next = files[i+1];
  92. else files[i].next = null;
  93. if (files[i].type!=="image/jpeg"&&files[i].type!=="image/jpg"&&files[i].type!=="image/png"&&files[i].type!=="image/gif") {
  94. files[i].ready = true;
  95. files[i].supported = false;
  96. }
  97. }
  98. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  99. upload.show("Uploading", files);
  100. process(files, files[0]);
  101. },
  102. url: function() {
  103. var albumID = album.getID(),
  104. params,
  105. extension,
  106. buttons,
  107. link,
  108. files = [];
  109. if (albumID===false) albumID = 0;
  110. buttons = [
  111. ["Import", function() {
  112. link = $(".message input.text").val();
  113. if (link&&link.length>3) {
  114. extension = link.split('.').pop();
  115. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  116. loadingBar.show("error", "The file format of this link is not supported.");
  117. return false;
  118. }
  119. files[0] = {
  120. name: link,
  121. supported: true
  122. }
  123. upload.show("Importing URL", files);
  124. //$(".upload_message .rows .row:nth-child(1) .status").html("Importing");
  125. params = "importUrl&url=" + escape(encodeURI(link)) + "&albumID=" + albumID;
  126. lychee.api(params, function(data) {
  127. upload.close();
  128. upload.notify("Import complete");
  129. if (album.getID()===false) lychee.goto("0");
  130. else album.load(albumID);
  131. if (data!==true) lychee.error(null, params, data);
  132. });
  133. } else loadingBar.show("error", "Link to short or too long. Please try another one!");
  134. }],
  135. ["Cancel", function() {}]
  136. ];
  137. 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);
  138. },
  139. server: function() {
  140. var albumID = album.getID(),
  141. params,
  142. buttons,
  143. files = [];
  144. if (albumID===false) albumID = 0;
  145. buttons = [
  146. ["Import", function() {
  147. files[0] = {
  148. name: "uploads/import/",
  149. supported: true
  150. };
  151. upload.show("Importing from server", files);
  152. params = "importServer&albumID=" + albumID;
  153. lychee.api(params, function(data) {
  154. upload.close();
  155. upload.notify("Import complete");
  156. if (data==="Notice: Import only contains albums!") {
  157. if (visible.albums()) lychee.load();
  158. else lychee.goto("");
  159. }
  160. else if (album.getID()===false) lychee.goto("0");
  161. else album.load(albumID);
  162. if (data==="Notice: Import only contains albums!") return true;
  163. else if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  164. else if (data!==true) lychee.error(null, params, data);
  165. });
  166. }],
  167. ["Cancel", function() {}]
  168. ];
  169. 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);
  170. },
  171. dropbox: function() {
  172. var albumID = album.getID(),
  173. params,
  174. links = "";
  175. if (albumID===false) albumID = 0;
  176. lychee.loadDropbox(function() {
  177. Dropbox.choose({
  178. linkType: "direct",
  179. multiselect: true,
  180. success: function(files) {
  181. for (var i = 0; i < files.length; i++) {
  182. links += files[i].link + ",";
  183. files[i] = {
  184. name: files[i].link,
  185. supported: true
  186. };
  187. }
  188. // Remove last comma
  189. links = links.substr(0, links.length-1);
  190. upload.show("Importing from Dropbox", files);
  191. params = "importUrl&url=" + escape(links) + "&albumID=" + albumID;
  192. lychee.api(params, function(data) {
  193. upload.close();
  194. upload.notify("Import complete");
  195. if (album.getID()===false) lychee.goto("0");
  196. else album.load(albumID);
  197. if (data!==true) lychee.error(null, params, data);
  198. });
  199. }
  200. });
  201. });
  202. }
  203. },
  204. close: function(force) {
  205. if (force===true) {
  206. $(".upload_overlay").remove();
  207. } else {
  208. upload.setProgress(100);
  209. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  210. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  211. }
  212. }
  213. };