db.js 1.6 KB

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