albumsController.js 7.9 KB

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