upload.js 12 KB

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