albumsController.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const config = require('../config.js')
  2. const db = require('knex')(config.database)
  3. const randomstring = require('randomstring')
  4. const path = require('path')
  5. const fs = require('fs')
  6. const ffmpeg = require('fluent-ffmpeg')
  7. const gm = require('gm')
  8. let albumsController = {}
  9. albumsController.list = function(req, res, next){
  10. let token = req.headers.token
  11. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  12. db.table('users').where('token', token).then((user) => {
  13. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  14. let fields = ['id', 'name']
  15. if(req.params.sidebar === undefined)
  16. fields.push('timestamp')
  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 = 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. ids.push(album.id)
  25. }
  26. db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
  27. let albumsCount = {}
  28. for(let id of ids) albumsCount[id] = 0
  29. for(let file of files) albumsCount[file.albumid] += 1
  30. for(let album of albums) album.files = albumsCount[album.id]
  31. return res.json({ success: true, albums })
  32. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  33. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  34. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  35. }
  36. albumsController.create = function(req, res, next){
  37. let token = req.headers.token
  38. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  39. db.table('users').where('token', token).then((user) => {
  40. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  41. let name = req.body.name
  42. if(name === undefined || name === '')
  43. return res.json({ success: false, description: 'No album name specified' })
  44. db.table('albums').where({
  45. name: name,
  46. enabled: 1,
  47. userid: user[0].id
  48. }).then((album) => {
  49. if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
  50. db.table('albums').insert({
  51. name: name,
  52. enabled: 1,
  53. userid: user[0].id,
  54. identifier: randomstring.generate(8),
  55. timestamp: Math.floor(Date.now() / 1000)
  56. }).then(() => {
  57. return res.json({ success: true })
  58. })
  59. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  60. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  61. }
  62. albumsController.delete = function(req, res, next){
  63. let token = req.headers.token
  64. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  65. db.table('users').where('token', token).then((user) => {
  66. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  67. let id = req.body.id
  68. if(id === undefined || id === '')
  69. return res.json({ success: false, description: 'No album specified' })
  70. db.table('albums').where({id: id, userid: user[0].id}).update({ enabled: 0 }).then(() => {
  71. return res.json({ success: true })
  72. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  73. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  74. }
  75. albumsController.rename = function(req, res, next){
  76. let token = req.headers.token
  77. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  78. db.table('users').where('token', token).then((user) => {
  79. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  80. let id = req.body.id
  81. if(id === undefined || id === '')
  82. return res.json({ success: false, description: 'No album specified' })
  83. let name = req.body.name
  84. if(name === undefined || name === '')
  85. return res.json({ success: false, description: 'No name specified' })
  86. db.table('albums').where({name: name, userid: user[0].id}).then((results) => {
  87. if(results.length !== 0) return res.json({ success: false, description: 'Name already in use' })
  88. db.table('albums').where({id: id, userid: user[0].id}).update({ name: name }).then(() => {
  89. return res.json({ success: true })
  90. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  91. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  92. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  93. }
  94. albumsController.get = function(req, res, next){
  95. let identifier = req.params.identifier
  96. if(identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' })
  97. db.table('albums')
  98. .where('identifier', identifier)
  99. .then((albums) => {
  100. if(albums.length === 0) return res.json({ success: false, description: 'Album not found' })
  101. let title = albums[0].name
  102. db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
  103. let basedomain = req.get('host')
  104. for(let domain of config.domains)
  105. if(domain.host === req.get('host'))
  106. if(domain.hasOwnProperty('resolve'))
  107. basedomain = domain.resolve
  108. for(let file of files){
  109. file.file = basedomain + '/' + file.name
  110. if(config.uploads.generateThumbnails === true){
  111. let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4']
  112. for(let ext of extensions){
  113. if(path.extname(file.name) === ext){
  114. file.thumb = basedomain + '/thumbs/' + file.name.slice(0, -ext.length) + '.png'
  115. let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs') + '/' + file.name.slice(0, -ext.length) + '.png'
  116. fs.access(thumbname, function(err) {
  117. if (err && err.code === 'ENOENT') {
  118. // File doesnt exist
  119. if (ext === '.webm' || ext === '.mp4') {
  120. ffmpeg('./' + config.uploads.folder + '/' + file.name)
  121. .thumbnail({
  122. timestamps: [0],
  123. filename: '%b.png',
  124. folder: './' + config.uploads.folder + '/thumbs',
  125. size: '200x?'
  126. })
  127. .on('error', function(error) {
  128. console.log('Error - ', error.message)
  129. })
  130. }
  131. else {
  132. let size = {
  133. width: 200,
  134. height: 200
  135. }
  136. gm('./' + config.uploads.folder + '/' + file.name)
  137. .resize(size.width, size.height + '>')
  138. .gravity('Center')
  139. .extent(size.width, size.height)
  140. .background('transparent')
  141. .write(thumbname, function (error) {
  142. if (error) console.log('Error - ', error)
  143. })
  144. }
  145. }
  146. })
  147. }
  148. }
  149. }
  150. }
  151. return res.json({
  152. success: true,
  153. title: title,
  154. files
  155. })
  156. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  157. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  158. }
  159. module.exports = albumsController