upload.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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', 'Photo::add');
  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(url = '') {
  156. var albumID = album.getID(),
  157. url = (typeof url === 'string' ? url : ''),
  158. action;
  159. if (albumID===false) albumID = 0;
  160. action = function(data) {
  161. var extension,
  162. files = [];
  163. if (data.link&&data.link.length>3) {
  164. basicModal.close();
  165. extension = data.link.split('.').pop();
  166. if (extension!=='jpeg'&&extension!=='jpg'&&extension!=='png'&&extension!=='gif'&&extension!=='webp') {
  167. loadingBar.show('error', 'The file format of this link is not supported.');
  168. return false;
  169. }
  170. files[0] = {
  171. name: data.link,
  172. supported: true
  173. }
  174. upload.show('Importing URL', files, function() {
  175. var params;
  176. $('.basicModal .rows .row .status').html('Importing');
  177. params = {
  178. url: data.link,
  179. albumID
  180. }
  181. api.post('Import::url', params, function(data) {
  182. basicModal.close();
  183. upload.notify('Import complete');
  184. albums.refresh();
  185. if (album.getID()===false) lychee.goto('0');
  186. else album.load(albumID);
  187. if (data!==true) lychee.error(null, params, data);
  188. });
  189. });
  190. } else basicModal.error('link');
  191. }
  192. basicModal.show({
  193. 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>",
  194. buttons: {
  195. action: {
  196. title: 'Import',
  197. fn: action
  198. },
  199. cancel: {
  200. title: 'Cancel',
  201. fn: basicModal.close
  202. }
  203. }
  204. });
  205. },
  206. server: function() {
  207. var albumID = album.getID(),
  208. action;
  209. if (albumID===false) albumID = 0;
  210. action = function(data) {
  211. var files = [];
  212. files[0] = {
  213. name: data.path,
  214. supported: true
  215. };
  216. upload.show('Importing from server', files, function() {
  217. var params;
  218. $('.basicModal .rows .row .status').html('Importing');
  219. params = {
  220. albumID,
  221. path: data.path
  222. }
  223. api.post('Import::server', params, function(data) {
  224. basicModal.close();
  225. upload.notify('Import complete');
  226. albums.refresh();
  227. if (data==='Notice: Import only contains albums!') {
  228. if (visible.albums()) lychee.load();
  229. else lychee.goto('');
  230. }
  231. else if (album.getID()===false) lychee.goto('0');
  232. else album.load(albumID);
  233. if (data==='Notice: Import only contains albums!') return true;
  234. else if (data==='Warning: Folder empty!') lychee.error('Folder empty. No photos imported!', params, data);
  235. else if (data!==true) lychee.error(null, params, data);
  236. });
  237. });
  238. }
  239. basicModal.show({
  240. 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>",
  241. buttons: {
  242. action: {
  243. title: 'Import',
  244. fn: action
  245. },
  246. cancel: {
  247. title: 'Cancel',
  248. fn: basicModal.close
  249. }
  250. }
  251. });
  252. },
  253. dropbox: function() {
  254. var albumID = album.getID(),
  255. links = '',
  256. success;
  257. if (albumID===false) albumID = 0;
  258. success = function(files) {
  259. for (var i = 0; i < files.length; i++) {
  260. links += files[i].link + ',';
  261. files[i] = {
  262. name: files[i].link,
  263. supported: true
  264. };
  265. }
  266. // Remove last comma
  267. links = links.substr(0, links.length-1);
  268. upload.show('Importing from Dropbox', files, function() {
  269. var params;
  270. $('.basicModal .rows .row .status').html('Importing');
  271. params = {
  272. url: links,
  273. albumID
  274. }
  275. api.post('Import::url', params, function(data) {
  276. basicModal.close();
  277. upload.notify('Import complete');
  278. albums.refresh();
  279. if (album.getID()===false) lychee.goto('0');
  280. else album.load(albumID);
  281. if (data!==true) lychee.error(null, params, data);
  282. });
  283. });
  284. }
  285. lychee.loadDropbox(function() {
  286. Dropbox.choose({
  287. linkType: 'direct',
  288. multiselect: true,
  289. success
  290. });
  291. });
  292. }
  293. }