upload.js 11 KB

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