upload.js 9.1 KB

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