upload.js 11 KB

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