albumsController.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const config = require('../config.js')
  2. const db = require('knex')(config.database)
  3. const randomstring = require('randomstring')
  4. const utils = require('utilsController.js')
  5. let albumsController = {}
  6. albumsController.list = function(req, res, next) {
  7. let token = req.headers.token
  8. if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  9. db.table('users').where('token', token).then((user) => {
  10. if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
  11. let fields = ['id', 'name']
  12. if (req.params.sidebar === undefined) {
  13. fields.push('timestamp')
  14. fields.push('identifier')
  15. }
  16. db.table('albums').select(fields).where({ enabled: 1, userid: user[0].id }).then((albums) => {
  17. if (req.params.sidebar !== undefined)
  18. return res.json({ success: true, albums })
  19. let ids = []
  20. for (let album of albums) {
  21. album.date = new Date(album.timestamp * 1000)
  22. album.date = utils.getPrettyDate(album.date) // album.date.getFullYear() + '-' + (album.date.getMonth() + 1) + '-' + album.date.getDate() + ' ' + (album.date.getHours() < 10 ? '0' : '') + album.date.getHours() + ':' + (album.date.getMinutes() < 10 ? '0' : '') + album.date.getMinutes() + ':' + (album.date.getSeconds() < 10 ? '0' : '') + album.date.getSeconds()
  23. let basedomain = req.get('host')
  24. for (let domain of config.domains)
  25. if (domain.host === req.get('host'))
  26. if (domain.hasOwnProperty('resolve'))
  27. basedomain = domain.resolve
  28. album.identifier = basedomain + '/a/' + album.identifier
  29. ids.push(album.id)
  30. }
  31. db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
  32. let albumsCount = {}
  33. for (let id of ids) albumsCount[id] = 0
  34. for (let file of files) albumsCount[file.albumid] += 1
  35. for (let album of albums) album.files = albumsCount[album.id]
  36. return res.json({ success: true, albums })
  37. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  38. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  39. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  40. }
  41. albumsController.create = function(req, res, next) {
  42. let token = req.headers.token
  43. if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  44. db.table('users').where('token', token).then((user) => {
  45. if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token' })
  46. let name = req.body.name
  47. if (name === undefined || name === '')
  48. return res.json({ success: false, description: 'No album name specified' })
  49. db.table('albums').where({
  50. name: name,
  51. enabled: 1,
  52. userid: user[0].id
  53. }).then((album) => {
  54. if (album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
  55. db.table('albums').insert({
  56. name: name,
  57. enabled: 1,
  58. userid: user[0].id,
  59. identifier: randomstring.generate(8),
  60. timestamp: Math.floor(Date.now() / 1000)
  61. }).then(() => {
  62. return res.json({ success: true })
  63. })
  64. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  65. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  66. }
  67. albumsController.delete = function(req, res, next) {
  68. let token = req.headers.token
  69. if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  70. db.table('users').where('token', token).then((user) => {
  71. if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  72. let id = req.body.id
  73. if (id === undefined || id === ''){
  74. return res.json({ success: false, description: 'No album specified' })
  75. }
  76. db.table('albums').where({ id: id, userid: user[0].id }).update({ enabled: 0 }).then(() => {
  77. return res.json({ success: true })
  78. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  79. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  80. }
  81. albumsController.rename = function(req, res, next) {
  82. let token = req.headers.token
  83. if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  84. db.table('users').where('token', token).then((user) => {
  85. if (user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  86. let id = req.body.id
  87. if (id === undefined || id === '')
  88. return res.json({ success: false, description: 'No album specified' })
  89. let name = req.body.name
  90. if (name === undefined || name === '')
  91. return res.json({ success: false, description: 'No name specified' })
  92. db.table('albums').where({ name: name, userid: user[0].id }).then((results) => {
  93. if (results.length !== 0) return res.json({ success: false, description: 'Name already in use' })
  94. db.table('albums').where({ id: id, userid: user[0].id }).update({ name: name }).then(() => {
  95. return res.json({ success: true })
  96. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  97. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  98. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  99. }
  100. albumsController.get = function(req, res, next) {
  101. let identifier = req.params.identifier
  102. if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
  103. db.table('albums')
  104. .where('identifier', identifier)
  105. .then((albums) => {
  106. if (albums.length === 0) return res.json({ success: false, description: 'Album not found' })
  107. let title = albums[0].name
  108. db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
  109. let basedomain = req.get('host')
  110. for (let domain of config.domains)
  111. if (domain.host === req.get('host'))
  112. if (domain.hasOwnProperty('resolve'))
  113. basedomain = domain.resolve
  114. for (let file of files) {
  115. file.file = basedomain + '/' + file.name
  116. utils.generateThumbs(file)
  117. }
  118. return res.json({
  119. success: true,
  120. title: title,
  121. count: files.length,
  122. files
  123. })
  124. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  125. }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
  126. }
  127. module.exports = albumsController