upload.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. notify: function(title, text) {
  14. var popup;
  15. if (!text||text==="") text = "You can now manage your new photo(s).";
  16. if (!window.webkitNotifications) return false;
  17. if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
  18. if (window.webkitNotifications.checkPermission()===0&&title) {
  19. popup = window.webkitNotifications.createNotification("", title, text);
  20. popup.show();
  21. }
  22. },
  23. start: {
  24. local: function(files) {
  25. var albumID = album.getID(),
  26. error = false,
  27. process = function(files, file) {
  28. var formData = new FormData(),
  29. xhr = new XMLHttpRequest(),
  30. pre_progress = 0,
  31. progress,
  32. finish = function() {
  33. window.onbeforeunload = null;
  34. $("#upload_files").val("");
  35. if (error===false) {
  36. // Success
  37. upload.close();
  38. upload.notify("Upload complete");
  39. } else {
  40. // Error
  41. $(".upload_message a.close").show();
  42. upload.notify("Upload complete", "Failed to upload one or more photos.");
  43. }
  44. if (album.getID()===false) lychee.goto("0");
  45. else album.load(albumID);
  46. };
  47. // Check if file is supported
  48. if (file.supported===false) {
  49. // Skip file
  50. if (file.next!==null) process(files, file.next);
  51. else {
  52. // Look for supported files
  53. // If zero files are supported, hide the upload after a delay
  54. var hasSupportedFiles = false;
  55. for (var i = 0; i < files.length; i++) {
  56. if (files[i].supported===true) {
  57. hasSupportedFiles = true;
  58. break;
  59. }
  60. }
  61. if (hasSupportedFiles===false) setTimeout(finish, 2000);
  62. }
  63. return false;
  64. }
  65. formData.append("function", "upload");
  66. formData.append("albumID", albumID);
  67. formData.append(0, file);
  68. xhr.open("POST", lychee.api_path);
  69. xhr.onload = function() {
  70. var wait = false;
  71. file.ready = true;
  72. // Set status
  73. if (xhr.status===200&&xhr.responseText==="1") {
  74. // Success
  75. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status")
  76. .html("Finished")
  77. .addClass("success");
  78. } else {
  79. // Error
  80. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status")
  81. .html("Error")
  82. .addClass("error");
  83. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") p.notice")
  84. .html("Server returned an unknown response. Please take a look at the console of your browser for further details.")
  85. .show();
  86. // Set global error
  87. error = true;
  88. // Throw error
  89. lychee.error("Upload failed. Server returned the status code " + xhr.status + "!", xhr, xhr.responseText);
  90. }
  91. // Check if there are file which are not finished
  92. for (var i = 0; i < files.length; i++) {
  93. if (files[i].ready===false) {
  94. wait = true;
  95. break;
  96. }
  97. }
  98. // Finish upload when all files are finished
  99. if (wait===false) finish();
  100. };
  101. xhr.upload.onprogress = function(e) {
  102. if (e.lengthComputable) {
  103. // Calculate progress
  104. progress = (e.loaded / e.total * 100 | 0);
  105. // Set progress when progress has changed
  106. if (progress>pre_progress) {
  107. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html(progress + "%");
  108. pre_progress = progress;
  109. }
  110. if (progress>=100) {
  111. // Scroll to the uploading file
  112. var scrollPos = 0;
  113. if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
  114. $(".upload_message .rows").scrollTop(scrollPos);
  115. // Set status to processing
  116. $(".upload_message .rows .row:nth-child(" + (file.num+1) + ") .status").html("Processing");
  117. // Upload next file
  118. if (file.next!==null) process(files, file.next);
  119. }
  120. }
  121. };
  122. xhr.send(formData);
  123. };
  124. if (files.length<=0) return false;
  125. if (albumID===false||visible.albums()===true) albumID = 0;
  126. for (var i = 0; i < files.length; i++) {
  127. files[i].num = i;
  128. files[i].ready = false;
  129. files[i].supported = true;
  130. if (i < files.length-1) files[i].next = files[i+1];
  131. else files[i].next = null;
  132. // Check if file is supported
  133. if (files[i].type!=="image/jpeg"&&files[i].type!=="image/jpg"&&files[i].type!=="image/png"&&files[i].type!=="image/gif") {
  134. files[i].ready = true;
  135. files[i].supported = false;
  136. }
  137. }
  138. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  139. upload.show("Uploading", files);
  140. // Upload first file
  141. process(files, files[0]);
  142. },
  143. url: function() {
  144. var albumID = album.getID(),
  145. params,
  146. extension,
  147. buttons,
  148. link,
  149. files = [];
  150. if (albumID===false) albumID = 0;
  151. buttons = [
  152. ["Import", function() {
  153. link = $(".message input.text").val();
  154. if (link&&link.length>3) {
  155. extension = link.split('.').pop();
  156. if (extension!=="jpeg"&&extension!=="jpg"&&extension!=="png"&&extension!=="gif"&&extension!=="webp") {
  157. loadingBar.show("error", "The file format of this link is not supported.");
  158. return false;
  159. }
  160. files[0] = {
  161. name: link,
  162. supported: true
  163. }
  164. upload.show("Importing URL", files, function() {
  165. $(".upload_message .rows .row .status").html("Importing");
  166. });
  167. params = "importUrl&url=" + escape(encodeURI(link)) + "&albumID=" + albumID;
  168. lychee.api(params, function(data) {
  169. upload.close();
  170. upload.notify("Import complete");
  171. if (album.getID()===false) lychee.goto("0");
  172. else album.load(albumID);
  173. if (data!==true) lychee.error(null, params, data);
  174. });
  175. } else loadingBar.show("error", "Link to short or too long. Please try another one!");
  176. }],
  177. ["Cancel", function() {}]
  178. ];
  179. 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);
  180. },
  181. server: function() {
  182. var albumID = album.getID(),
  183. params,
  184. buttons,
  185. files = [],
  186. path;
  187. if (albumID===false) albumID = 0;
  188. buttons = [
  189. ["Import", function() {
  190. path = $(".message input.text").val();
  191. files[0] = {
  192. name: path,
  193. supported: true
  194. };
  195. upload.show("Importing from server", files, function() {
  196. $(".upload_message .rows .row .status").html("Importing");
  197. });
  198. params = "importServer&albumID=" + albumID + "&path=" + escape(encodeURI(path));
  199. lychee.api(params, function(data) {
  200. upload.close();
  201. upload.notify("Import complete");
  202. if (data==="Notice: Import only contains albums!") {
  203. if (visible.albums()) lychee.load();
  204. else lychee.goto("");
  205. }
  206. else if (album.getID()===false) lychee.goto("0");
  207. else album.load(albumID);
  208. if (data==="Notice: Import only contains albums!") return true;
  209. else if (data==="Warning: Folder empty!") lychee.error("Folder empty. No photos imported!", params, data);
  210. else if (data!==true) lychee.error(null, params, data);
  211. });
  212. }],
  213. ["Cancel", function() {}]
  214. ];
  215. modal.show("Import from Server", "This action will import all photos, folders and sub-folders which are located in the following directory. The <b>original files will be deleted</b> after the import when possible. <input class='text' type='text' maxlength='100' placeholder='Absolute path to directory' value='" + lychee.location + "uploads/import/'>", buttons);
  216. },
  217. dropbox: function() {
  218. var albumID = album.getID(),
  219. params,
  220. links = "";
  221. if (albumID===false) albumID = 0;
  222. lychee.loadDropbox(function() {
  223. Dropbox.choose({
  224. linkType: "direct",
  225. multiselect: true,
  226. success: function(files) {
  227. for (var i = 0; i < files.length; i++) {
  228. links += files[i].link + ",";
  229. files[i] = {
  230. name: files[i].link,
  231. supported: true
  232. };
  233. }
  234. // Remove last comma
  235. links = links.substr(0, links.length-1);
  236. upload.show("Importing from Dropbox", files, function() {
  237. $(".upload_message .rows .row .status").html("Importing");
  238. });
  239. params = "importUrl&url=" + escape(links) + "&albumID=" + albumID;
  240. lychee.api(params, function(data) {
  241. upload.close();
  242. upload.notify("Import complete");
  243. if (album.getID()===false) lychee.goto("0");
  244. else album.load(albumID);
  245. if (data!==true) lychee.error(null, params, data);
  246. });
  247. }
  248. });
  249. });
  250. }
  251. },
  252. close: function(force) {
  253. if (force===true) {
  254. $(".upload_overlay").remove();
  255. } else {
  256. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  257. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  258. }
  259. }
  260. };