db.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. let init = function(db, config){
  2. // Create the tables we need to store galleries and files
  3. db.schema.createTableIfNotExists('albums', function (table) {
  4. table.increments()
  5. table.string('name')
  6. table.integer('enabled')
  7. table.integer('timestamp')
  8. }).then(() => {})
  9. db.schema.createTableIfNotExists('files', function (table) {
  10. table.increments()
  11. table.string('name')
  12. table.string('original')
  13. table.string('type')
  14. table.string('size')
  15. table.string('ip')
  16. table.integer('albumid')
  17. table.integer('timestamp')
  18. }).then(() => {})
  19. db.schema.createTableIfNotExists('tokens', function (table) {
  20. table.string('name')
  21. table.string('value')
  22. table.integer('timestamp')
  23. }).then(() => {
  24. // == Generate a 1 time token == //
  25. db.table('tokens').then((tokens) => {
  26. if(tokens.length !== 0) return printAndSave(config, tokens[0].value, tokens[1].value)
  27. // This is the first launch of the app
  28. let clientToken = require('randomstring').generate()
  29. let adminToken = require('randomstring').generate()
  30. let now = Math.floor(Date.now() / 1000)
  31. db.table('tokens').insert(
  32. [
  33. {
  34. name: 'client',
  35. value: clientToken,
  36. timestamp: now
  37. },
  38. {
  39. name: 'admin',
  40. value: adminToken,
  41. timestamp: now
  42. }
  43. ]
  44. ).then(() => {
  45. printAndSave(config, clientToken, adminToken)
  46. })
  47. })
  48. })
  49. }
  50. function printAndSave(config, clientToken, adminToken){
  51. console.log('Your client token is: ' + clientToken)
  52. console.log('Your admin token is: ' + adminToken)
  53. config.clientToken = clientToken
  54. config.adminToken = adminToken
  55. }
  56. module.exports = init