albumsController.js 6.8 KB

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