upload.js 9.8 KB

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