upload.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. /*
  180. // Only check for file validity on PHP side
  181. let extension = data.link.split('.').pop()
  182. if (extension!=='jpeg' && extension!=='jpg' && extension!=='png' && extension!=='gif' && extension!=='webp') {
  183. loadingBar.show('error', 'File format of link not supported.')
  184. return false
  185. }
  186. */
  187. files[0] = {
  188. name : data.link,
  189. supported : true
  190. }
  191. upload.show('Importing URL', files, function() {
  192. $('.basicModal .rows .row .status').html('Importing')
  193. let params = {
  194. url: data.link,
  195. albumID
  196. }
  197. api.post('Import::url', params, function(data) {
  198. // Same code as in import.dropbox()
  199. if (data!==true) {
  200. $('.basicModal .rows .row p.notice')
  201. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  202. .show()
  203. $('.basicModal .rows .row .status')
  204. .html('Finished')
  205. .addClass('warning')
  206. // Show close button
  207. $('.basicModal #basicModal__action.hidden').show()
  208. // Log error
  209. lychee.error(null, params, data)
  210. } else {
  211. basicModal.close()
  212. }
  213. upload.notify('Import complete')
  214. albums.refresh()
  215. if (album.getID()===false) lychee.goto('0')
  216. else album.load(albumID)
  217. })
  218. })
  219. } else basicModal.error('link')
  220. }
  221. basicModal.show({
  222. 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>`,
  223. buttons: {
  224. action: {
  225. title: 'Import',
  226. fn: action
  227. },
  228. cancel: {
  229. title: 'Cancel',
  230. fn: basicModal.close
  231. }
  232. }
  233. })
  234. },
  235. server: function() {
  236. let albumID = album.getID()
  237. if (albumID===false) albumID = 0
  238. const action = function(data) {
  239. let files = []
  240. files[0] = {
  241. name : data.path,
  242. supported : true
  243. }
  244. upload.show('Importing from server', files, function() {
  245. $('.basicModal .rows .row .status').html('Importing')
  246. let params = {
  247. albumID,
  248. path: data.path
  249. }
  250. api.post('Import::server', params, function(data) {
  251. albums.refresh()
  252. upload.notify('Import complete')
  253. if (data==='Notice: Import only contained albums!') {
  254. // No error, but the folder only contained albums
  255. // Go back to the album overview to show the imported albums
  256. if (visible.albums()) lychee.load()
  257. else lychee.goto()
  258. basicModal.close()
  259. return true
  260. } else if (data==='Warning: Folder empty or no readable files to process!') {
  261. // Error because the import could not start
  262. $('.basicModal .rows .row p.notice')
  263. .html('Folder empty or no readable files to process. Please take a look at the log (Settings -> Show Log) for further details.')
  264. .show()
  265. $('.basicModal .rows .row .status')
  266. .html('Failed')
  267. .addClass('error')
  268. // Log error
  269. lychee.error('Could not start import because the folder was empty!', params, data)
  270. } else if (data!==true) {
  271. // Maybe an error, maybe just some skipped photos
  272. $('.basicModal .rows .row p.notice')
  273. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  274. .show()
  275. $('.basicModal .rows .row .status')
  276. .html('Finished')
  277. .addClass('warning')
  278. // Log error
  279. lychee.error(null, params, data)
  280. } else {
  281. // No error, everything worked fine
  282. basicModal.close()
  283. }
  284. if (album.getID()===false) lychee.goto('0')
  285. else album.load(albumID)
  286. // Show close button
  287. $('.basicModal #basicModal__action.hidden').show()
  288. })
  289. })
  290. }
  291. basicModal.show({
  292. 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>`,
  293. buttons: {
  294. action: {
  295. title: 'Import',
  296. fn: action
  297. },
  298. cancel: {
  299. title: 'Cancel',
  300. fn: basicModal.close
  301. }
  302. }
  303. })
  304. },
  305. dropbox: function() {
  306. let albumID = album.getID()
  307. if (albumID===false) albumID = 0
  308. const success = function(files) {
  309. let links = ''
  310. for (let i = 0; i < files.length; i++) {
  311. links += files[i].link + ','
  312. files[i] = {
  313. name : files[i].link,
  314. supported : true
  315. }
  316. }
  317. // Remove last comma
  318. links = links.substr(0, links.length - 1)
  319. upload.show('Importing from Dropbox', files, function() {
  320. $('.basicModal .rows .row .status').html('Importing')
  321. let params = {
  322. url: links,
  323. albumID
  324. }
  325. api.post('Import::url', params, function(data) {
  326. // Same code as in import.url()
  327. if (data!==true) {
  328. $('.basicModal .rows .row p.notice')
  329. .html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
  330. .show()
  331. $('.basicModal .rows .row .status')
  332. .html('Finished')
  333. .addClass('warning')
  334. // Show close button
  335. $('.basicModal #basicModal__action.hidden').show()
  336. // Log error
  337. lychee.error(null, params, data)
  338. } else {
  339. basicModal.close()
  340. }
  341. upload.notify('Import complete')
  342. albums.refresh()
  343. if (album.getID()===false) lychee.goto('0')
  344. else album.load(albumID)
  345. })
  346. })
  347. }
  348. lychee.loadDropbox(function() {
  349. Dropbox.choose({
  350. linkType: 'direct',
  351. multiselect: true,
  352. success
  353. })
  354. })
  355. }
  356. }