upload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. file.ready = true
  67. // Set status
  68. if (xhr.status===200 && xhr.responseText==='true') {
  69. // Success
  70. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  71. .html('Finished')
  72. .addClass('success')
  73. } else {
  74. if (xhr.responseText.substr(0, 6)==='Error:') {
  75. errorText = xhr.responseText.substr(6) + ' Please take a look at the console of your browser for further details.'
  76. error = true
  77. // Error Status
  78. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  79. .html('Failed')
  80. .addClass('error')
  81. } else if (xhr.responseText.substr(0, 8)==='Warning:') {
  82. errorText = xhr.responseText.substr(8)
  83. warning = true
  84. // Warning Status
  85. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  86. .html('Skipped')
  87. .addClass('warning')
  88. } else {
  89. errorText = 'Server returned an unknown response. Please take a look at the console of your browser for further details.'
  90. error = true
  91. // Error Status
  92. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') .status')
  93. .html('Failed')
  94. .addClass('error')
  95. }
  96. $('.basicModal .rows .row:nth-child(' + (file.num + 1) + ') p.notice')
  97. .html(errorText)
  98. .show()
  99. // Throw error
  100. if (error===true) 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 (let 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!==true) return false
  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 && next_file_started==false) {
  122. // Scroll to the uploading file
  123. let 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) {
  130. process(files, file.next)
  131. next_file_started = true
  132. }
  133. }
  134. }
  135. xhr.send(formData)
  136. }
  137. if (files.length<=0) return false
  138. if (albumID===false || visible.albums()===true) albumID = 0
  139. for (let i = 0; i < files.length; i++) {
  140. files[i].num = i
  141. files[i].ready = false
  142. if (i < files.length-1) files[i].next = files[i + 1]
  143. else files[i].next = null
  144. }
  145. window.onbeforeunload = function() { return 'Lychee is currently uploading!' }
  146. upload.show('Uploading', files, function() {
  147. // Upload first file
  148. process(files, files[0])
  149. })
  150. },
  151. url: function(url = '') {
  152. let albumID = album.getID()
  153. url = (typeof url === 'string' ? url : '')
  154. if (albumID===false) albumID = 0
  155. const action = function(data) {
  156. let files = []
  157. if (data.link && data.link.length>3) {
  158. basicModal.close()
  159. files[0] = {
  160. name : data.link
  161. }
  162. upload.show('Importing URL', files, function() {
  163. $('.basicModal .rows .row .status').html('Importing')
  164. let params = {
  165. url: data.link,
  166. albumID
  167. }
  168. api.post('Import::url', params, function(data) {
  169. // Same code as in import.dropbox()
  170. if (data!==true) {
  171. $('.basicModal .rows .row p.notice')
  172. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  173. .show()
  174. $('.basicModal .rows .row .status')
  175. .html('Finished')
  176. .addClass('warning')
  177. // Show close button
  178. $('.basicModal #basicModal__action.hidden').show()
  179. // Log error
  180. lychee.error(null, params, data)
  181. } else {
  182. basicModal.close()
  183. }
  184. upload.notify('Import complete')
  185. albums.refresh()
  186. if (album.getID()===false) lychee.goto('0')
  187. else album.load(albumID)
  188. })
  189. })
  190. } else basicModal.error('link')
  191. }
  192. basicModal.show({
  193. 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>`,
  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. let albumID = album.getID()
  208. if (albumID===false) albumID = 0
  209. const action = function(data) {
  210. let files = []
  211. files[0] = {
  212. name : data.path
  213. }
  214. upload.show('Importing from server', files, function() {
  215. $('.basicModal .rows .row .status').html('Importing')
  216. let params = {
  217. albumID,
  218. path: data.path
  219. }
  220. api.post('Import::server', params, function(data) {
  221. albums.refresh()
  222. upload.notify('Import complete')
  223. if (data==='Notice: Import only contained albums!') {
  224. // No error, but the folder only contained albums
  225. // Go back to the album overview to show the imported albums
  226. if (visible.albums()) lychee.load()
  227. else lychee.goto()
  228. basicModal.close()
  229. return true
  230. } else if (data==='Warning: Folder empty or no readable files to process!') {
  231. // Error because the import could not start
  232. $('.basicModal .rows .row p.notice')
  233. .html('Folder empty or no readable files to process. Please take a look at the log (Settings -> Show Log) for further details.')
  234. .show()
  235. $('.basicModal .rows .row .status')
  236. .html('Failed')
  237. .addClass('error')
  238. // Log error
  239. lychee.error('Could not start import because the folder was empty!', params, data)
  240. } else if (data!==true) {
  241. // Maybe an error, maybe just some skipped photos
  242. $('.basicModal .rows .row p.notice')
  243. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  244. .show()
  245. $('.basicModal .rows .row .status')
  246. .html('Finished')
  247. .addClass('warning')
  248. // Log error
  249. lychee.error(null, params, data)
  250. } else {
  251. // No error, everything worked fine
  252. basicModal.close()
  253. }
  254. if (album.getID()===false) lychee.goto('0')
  255. else album.load(albumID)
  256. // Show close button
  257. $('.basicModal #basicModal__action.hidden').show()
  258. })
  259. })
  260. }
  261. basicModal.show({
  262. 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>`,
  263. buttons: {
  264. action: {
  265. title: 'Import',
  266. fn: action
  267. },
  268. cancel: {
  269. title: 'Cancel',
  270. fn: basicModal.close
  271. }
  272. }
  273. })
  274. },
  275. dropbox: function() {
  276. let albumID = album.getID()
  277. if (albumID===false) albumID = 0
  278. const success = function(files) {
  279. let links = ''
  280. for (let i = 0; i < files.length; i++) {
  281. links += files[i].link + ','
  282. files[i] = {
  283. name : files[i].link
  284. }
  285. }
  286. // Remove last comma
  287. links = links.substr(0, links.length - 1)
  288. upload.show('Importing from Dropbox', files, function() {
  289. $('.basicModal .rows .row .status').html('Importing')
  290. let params = {
  291. url: links,
  292. albumID
  293. }
  294. api.post('Import::url', params, function(data) {
  295. // Same code as in import.url()
  296. if (data!==true) {
  297. $('.basicModal .rows .row p.notice')
  298. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  299. .show()
  300. $('.basicModal .rows .row .status')
  301. .html('Finished')
  302. .addClass('warning')
  303. // Show close button
  304. $('.basicModal #basicModal__action.hidden').show()
  305. // Log error
  306. lychee.error(null, params, data)
  307. } else {
  308. basicModal.close()
  309. }
  310. upload.notify('Import complete')
  311. albums.refresh()
  312. if (album.getID()===false) lychee.goto('0')
  313. else album.load(albumID)
  314. })
  315. })
  316. }
  317. lychee.loadDropbox(function() {
  318. Dropbox.choose({
  319. linkType: 'direct',
  320. multiselect: true,
  321. success
  322. })
  323. })
  324. }
  325. }