upload.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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', lychee.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 = 'importUrl&url=' + escape(encodeURI(data.link)) + '&albumID=' + albumID;
  177. lychee.api(params, function(data) {
  178. basicModal.close();
  179. upload.notify('Import complete');
  180. albums.refresh();
  181. if (album.getID()===false) lychee.goto('0');
  182. else album.load(albumID);
  183. if (data!==true) lychee.error(null, params, data);
  184. });
  185. });
  186. } else basicModal.error('link');
  187. }
  188. basicModal.show({
  189. 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>",
  190. buttons: {
  191. action: {
  192. title: 'Import',
  193. fn: action
  194. },
  195. cancel: {
  196. title: 'Cancel',
  197. fn: basicModal.close
  198. }
  199. }
  200. });
  201. },
  202. server: function() {
  203. var albumID = album.getID(),
  204. action;
  205. if (albumID===false) albumID = 0;
  206. action = function(data) {
  207. var files = [];
  208. files[0] = {
  209. name: data.path,
  210. supported: true
  211. };
  212. upload.show('Importing from server', files, function() {
  213. var params;
  214. $('.basicModal .rows .row .status').html('Importing');
  215. params = 'importServer&albumID=' + albumID + '&path=' + escape(encodeURI(data.path));
  216. lychee.api(params, function(data) {
  217. basicModal.close();
  218. upload.notify('Import complete');
  219. albums.refresh();
  220. if (data==='Notice: Import only contains albums!') {
  221. if (visible.albums()) lychee.load();
  222. else lychee.goto('');
  223. }
  224. else if (album.getID()===false) lychee.goto('0');
  225. else album.load(albumID);
  226. if (data==='Notice: Import only contains albums!') return true;
  227. else if (data==='Warning: Folder empty!') lychee.error('Folder empty. No photos imported!', params, data);
  228. else if (data!==true) lychee.error(null, params, data);
  229. });
  230. });
  231. }
  232. basicModal.show({
  233. 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>",
  234. buttons: {
  235. action: {
  236. title: 'Import',
  237. fn: action
  238. },
  239. cancel: {
  240. title: 'Cancel',
  241. fn: basicModal.close
  242. }
  243. }
  244. });
  245. },
  246. dropbox: function() {
  247. var albumID = album.getID(),
  248. links = '',
  249. success;
  250. if (albumID===false) albumID = 0;
  251. success = function(files) {
  252. for (var i = 0; i < files.length; i++) {
  253. links += files[i].link + ',';
  254. files[i] = {
  255. name: files[i].link,
  256. supported: true
  257. };
  258. }
  259. // Remove last comma
  260. links = links.substr(0, links.length-1);
  261. upload.show('Importing from Dropbox', files, function() {
  262. var params;
  263. $('.basicModal .rows .row .status').html('Importing');
  264. params = 'importUrl&url=' + escape(links) + '&albumID=' + albumID;
  265. lychee.api(params, function(data) {
  266. basicModal.close();
  267. upload.notify('Import complete');
  268. albums.refresh();
  269. if (album.getID()===false) lychee.goto('0');
  270. else album.load(albumID);
  271. if (data!==true) lychee.error(null, params, data);
  272. });
  273. });
  274. }
  275. lychee.loadDropbox(function() {
  276. Dropbox.choose({
  277. linkType: 'direct',
  278. multiselect: true,
  279. success
  280. });
  281. });
  282. }
  283. }