upload.js 7.2 KB

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