upload.js 12 KB

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