upload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. if (text==null||text==='') text = 'You can now manage your new photo(s).'
  21. if (!window.webkitNotifications) return false
  22. if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission()
  23. if (window.webkitNotifications.checkPermission()===0 && title) {
  24. let popup = window.webkitNotifications.createNotification('', title, text)
  25. popup.show()
  26. }
  27. }
  28. upload.start = {
  29. local: function(files) {
  30. let albumID = album.getID()
  31. let error = false
  32. let warning = false
  33. const process = function(files, file) {
  34. let formData = new FormData()
  35. let xhr = new XMLHttpRequest()
  36. let pre_progress = 0
  37. let progress = 0
  38. let next_file_started = false
  39. const 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. formData.append('function', 'Photo::add')
  60. formData.append('albumID', albumID)
  61. formData.append(0, file)
  62. xhr.open('POST', api.path)
  63. xhr.onload = function() {
  64. let wait = false
  65. let errorText = ''
  66. const isNumber = (n) => (!isNaN(parseFloat(n)) && isFinite(n))
  67. file.ready = true
  68. // Set status
  69. if (xhr.status===200 && isNumber(xhr.responseText)) {
  70. // Success
  71. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  72. .html('Finished')
  73. .addClass('success')
  74. } else {
  75. if (xhr.responseText.substr(0, 6)==='Error:') {
  76. errorText = xhr.responseText.substr(6) + ' Please take a look at the console of your browser for further details.'
  77. error = true
  78. // Error Status
  79. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  80. .html('Failed')
  81. .addClass('error')
  82. } else if (xhr.responseText.substr(0, 8)==='Warning:') {
  83. errorText = xhr.responseText.substr(8)
  84. warning = true
  85. // Warning Status
  86. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  87. .html('Skipped')
  88. .addClass('warning')
  89. } else {
  90. errorText = 'Server returned an unknown response. Please take a look at the console of your browser for further details.'
  91. error = true
  92. // Error Status
  93. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  94. .html('Failed')
  95. .addClass('error')
  96. }
  97. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') p.notice')
  98. .html(errorText)
  99. .show()
  100. // Throw error
  101. if (error===true) lychee.error('Upload failed. Server returned the status code ' + xhr.status + '!', xhr, xhr.responseText)
  102. }
  103. // Check if there are file which are not finished
  104. for (let i = 0; i < files.length; i++) {
  105. if (files[i].ready===false) {
  106. wait = true
  107. break
  108. }
  109. }
  110. // Finish upload when all files are finished
  111. if (wait===false) finish()
  112. }
  113. xhr.upload.onprogress = function(e) {
  114. if (e.lengthComputable!==true) return false
  115. // Calculate progress
  116. progress = (e.loaded / e.total * 100 | 0)
  117. // Set progress when progress has changed
  118. if (progress>pre_progress) {
  119. $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html(progress + '%')
  120. pre_progress = progress
  121. }
  122. if (progress>=100 && next_file_started===false) {
  123. // Scroll to the uploading file
  124. let scrollPos = 0
  125. if ((file.num + 1)>4) scrollPos = (file.num + 1 - 4) * 40
  126. $('.basicModal .rows').scrollTop(scrollPos)
  127. // Set status to processing
  128. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status').html('Processing')
  129. // Upload next file
  130. if (file.next!=null) {
  131. process(files, file.next)
  132. next_file_started = true
  133. }
  134. }
  135. }
  136. xhr.send(formData)
  137. }
  138. if (files.length<=0) return false
  139. if (albumID===false || visible.albums()===true) albumID = 0
  140. for (let i = 0; i < files.length; i++) {
  141. files[i].num = i
  142. files[i].ready = false
  143. if (i < files.length-1) files[i].next = files[i + 1]
  144. else files[i].next = null
  145. }
  146. window.onbeforeunload = function() { return 'Lychee is currently uploading!' }
  147. upload.show('Uploading', files, function() {
  148. // Upload first file
  149. process(files, files[0])
  150. })
  151. },
  152. url: function(url = '') {
  153. let albumID = album.getID()
  154. url = (typeof url === 'string' ? url : '')
  155. if (albumID===false) albumID = 0
  156. const action = function(data) {
  157. let files = []
  158. if (data.link && data.link.length>3) {
  159. basicModal.close()
  160. files[0] = {
  161. name : data.link
  162. }
  163. upload.show('Importing URL', files, function() {
  164. $('.basicModal .rows .row .status').html('Importing')
  165. let params = {
  166. url: data.link,
  167. albumID
  168. }
  169. api.post('Import::url', params, function(data) {
  170. // Same code as in import.dropbox()
  171. if (data!==true) {
  172. $('.basicModal .rows .row p.notice')
  173. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  174. .show()
  175. $('.basicModal .rows .row .status')
  176. .html('Finished')
  177. .addClass('warning')
  178. // Show close button
  179. $('.basicModal #basicModal__action.hidden').show()
  180. // Log error
  181. lychee.error(null, params, data)
  182. } else {
  183. basicModal.close()
  184. }
  185. upload.notify('Import complete')
  186. albums.refresh()
  187. if (album.getID()===false) lychee.goto('0')
  188. else album.load(albumID)
  189. })
  190. })
  191. } else basicModal.error('link')
  192. }
  193. basicModal.show({
  194. body: lychee.html`<p>Please enter the direct link to a photo to import it: <input class='text' name='link' type='text' placeholder='http://' value='$${ url }'></p>`,
  195. buttons: {
  196. action: {
  197. title: 'Import',
  198. fn: action
  199. },
  200. cancel: {
  201. title: 'Cancel',
  202. fn: basicModal.close
  203. }
  204. }
  205. })
  206. },
  207. server: function() {
  208. let albumID = album.getID()
  209. if (albumID===false) albumID = 0
  210. const action = function(data) {
  211. let files = []
  212. files[0] = {
  213. name : data.path
  214. }
  215. upload.show('Importing from server', files, function() {
  216. $('.basicModal .rows .row .status').html('Importing')
  217. let params = {
  218. albumID,
  219. path: data.path
  220. }
  221. api.post('Import::server', params, function(data) {
  222. albums.refresh()
  223. upload.notify('Import complete')
  224. if (data==='Notice: Import only contained albums!') {
  225. // No error, but the folder only contained albums
  226. // Go back to the album overview to show the imported albums
  227. if (visible.albums()) lychee.load()
  228. else lychee.goto()
  229. basicModal.close()
  230. return true
  231. } else if (data==='Warning: Folder empty or no readable files to process!') {
  232. // Error because the import could not start
  233. $('.basicModal .rows .row p.notice')
  234. .html('Folder empty or no readable files to process. Please take a look at the log (Settings -> Show Log) for further details.')
  235. .show()
  236. $('.basicModal .rows .row .status')
  237. .html('Failed')
  238. .addClass('error')
  239. // Log error
  240. lychee.error('Could not start import because the folder was empty!', params, data)
  241. } else if (data!==true) {
  242. // Maybe an error, maybe just some skipped photos
  243. $('.basicModal .rows .row p.notice')
  244. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  245. .show()
  246. $('.basicModal .rows .row .status')
  247. .html('Finished')
  248. .addClass('warning')
  249. // Log error
  250. lychee.error(null, params, data)
  251. } else {
  252. // No error, everything worked fine
  253. basicModal.close()
  254. }
  255. if (album.getID()===false) lychee.goto('0')
  256. else album.load(albumID)
  257. // Show close button
  258. $('.basicModal #basicModal__action.hidden').show()
  259. })
  260. })
  261. }
  262. basicModal.show({
  263. body: lychee.html`<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' name='path' type='text' maxlength='100' placeholder='Absolute path to directory' value='$${ lychee.location }uploads/import/'></p>`,
  264. buttons: {
  265. action: {
  266. title: 'Import',
  267. fn: action
  268. },
  269. cancel: {
  270. title: 'Cancel',
  271. fn: basicModal.close
  272. }
  273. }
  274. })
  275. },
  276. dropbox: function() {
  277. let albumID = album.getID()
  278. if (albumID===false) albumID = 0
  279. const success = function(files) {
  280. let links = ''
  281. for (let i = 0; i < files.length; i++) {
  282. links += files[i].link + ','
  283. files[i] = {
  284. name : files[i].link
  285. }
  286. }
  287. // Remove last comma
  288. links = links.substr(0, links.length - 1)
  289. upload.show('Importing from Dropbox', files, function() {
  290. $('.basicModal .rows .row .status').html('Importing')
  291. let params = {
  292. url: links,
  293. albumID
  294. }
  295. api.post('Import::url', params, function(data) {
  296. // Same code as in import.url()
  297. if (data!==true) {
  298. $('.basicModal .rows .row p.notice')
  299. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  300. .show()
  301. $('.basicModal .rows .row .status')
  302. .html('Finished')
  303. .addClass('warning')
  304. // Show close button
  305. $('.basicModal #basicModal__action.hidden').show()
  306. // Log error
  307. lychee.error(null, params, data)
  308. } else {
  309. basicModal.close()
  310. }
  311. upload.notify('Import complete')
  312. albums.refresh()
  313. if (album.getID()===false) lychee.goto('0')
  314. else album.load(albumID)
  315. })
  316. })
  317. }
  318. lychee.loadDropbox(function() {
  319. Dropbox.choose({
  320. linkType: 'direct',
  321. multiselect: true,
  322. success
  323. })
  324. })
  325. }
  326. }