<commit_before>express = require 'express'
router = express.Router()
passport = require 'passport'
_ = require 'lodash'
multer = require 'multer'
upload = multer dest: 'temp/'
users = require '../users'
config = require '../config'
Slide = require '../slide'
fs = require 'fs'
bcrypt = require 'bcrypt-nodejs'

randomString = (len, charSet) ->
  charSet = charSet or 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  randomString = ''
  for i in [0..len]
    randomPoz = Math.floor Math.random() * charSet.length
    randomString += charSet.substring randomPoz,randomPoz+1
  return randomString

router.use (req, res, next) ->
  res.respond = (data) ->
    res.json data: data
  res.fail = (msg) ->
    res.json error: msg
    res.status 400
  next()


router.get "/getContent", (req, res) ->
  content = []
  for user in users.getUsers()
    for slide in user.data.slides
      if slide.data.hidden
        continue
      content.push
        url: "content/#{slide.data.name}"
        type: slide.data.type
        duration: slide.data.duration
  res.json content

router.get "/getConfig", (req, res) ->
  res.json config.getConfig()

router.post "/registerUser", (req, res) ->
  if !req.body.registerLink
    req.flash 'error', 'No register link ID!'
    return res.redirect '/login'

  if !req.body.username or !req.body.password
    req.flash 'error', 'No username or password!'
    return res.redirect '/login'

  for u in users.getUsers()
    if !u.data.registered and u.data.registerLink == req.body.registerLink
      u.data.username = req.body.username
      u.data.password = <PASSWORD> <PASSWORD>.body.password
      u.data.registered = true
      req.login u
      return res.redirect '/admin/index'
  req.flash 'error', 'Invalid register link ID!'
  res.redirect '/login'

router.use (req, res, next) ->
  if !req.user
    req.flash "error", "You need to be logged in to perform this action!"
    res.redirect "/admin/index"
  else
    next()

router.get "/getUser", (req, res) ->
  res.respond req.user

router.post '/setSlides', (req, res) ->
  req.user.slides = req.body.slides
  res.respond req.user.slides

router.post '/addSlide', upload.single('file'), (req, res) ->
  if !req.body.duration
    fs.unlink req.file.path
    req.flash 'error', 'No duration provided!'
    return res.redirect '/admin'
  user = req.user.data.username
  data =
    duration: parseInt req.body.duration<commit_msg>Fix error when no file is provided when adding a slide. Fixes #3<commit_after>express = require 'express'
router = express.Router()
passport = require 'passport'
_ = require 'lodash'
multer = require 'multer'
upload = multer dest: 'temp/'
users = require '../users'
config = require '../config'
Slide = require '../slide'
fs = require 'fs'
bcrypt = require 'bcrypt-nodejs'

randomString = (len, charSet) ->
  charSet = charSet or 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  randomString = ''
  for i in [0..len]
    randomPoz = Math.floor Math.random() * charSet.length
    randomString += charSet.substring randomPoz,randomPoz+1
  return randomString

router.use (req, res, next) ->
  res.respond = (data) ->
    res.json data: data
  res.fail = (msg) ->
    res.json error: msg
    res.status 400
  next()


router.get "/getContent", (req, res) ->
  content = []
  for user in users.getUsers()
    for slide in user.data.slides
      if slide.data.hidden
        continue
      content.push
        url: "content/#{slide.data.name}"
        type: slide.data.type
        duration: slide.data.duration
  res.json content

router.get "/getConfig", (req, res) ->
  res.json config.getConfig()

router.post "/registerUser", (req, res) ->
  if !req.body.registerLink
    req.flash 'error', 'No register link ID!'
    return res.redirect '/login'

  if !req.body.username or !req.body.password
    req.flash 'error', 'No username or password!'
    return res.redirect '/login'

  for u in users.getUsers()
    if !u.data.registered and u.data.registerLink == req.body.registerLink
      u.data.username = req.body.username
      u.data.password = <PASSWORD> <PASSWORD>
      u.data.registered = true
      req.login u
      return res.redirect '/admin/index'
  req.flash 'error', 'Invalid register link ID!'
  res.redirect '/login'

router.use (req, res, next) ->
  if !req.user
    req.flash "error", "You need to be logged in to perform this action!"
    res.redirect "/admin/index"
  else
    next()

router.get "/getUser", (req, res) ->
  res.respond req.user

router.post '/setSlides', (req, res) ->
  req.user.slides = req.body.slides
  res.respond req.user.slides

router.post '/addSlide', upload.single('file'), (req, res) ->
  if !req.body.duration
    fs.unlink req.file.path
    req.flash 'error', 'No duration provided!'
    return res.redirect '/admin'
  if !req.file
    req.flash 'error', 'No file uploaded!'
    return res.redirect '/admin'
  user = req.user.data.username
  data =
    duration: parseInt req.body.duration
{'fraction_non_alphanumeric': 0.136983553951063, 'fraction_numerical': 0.006217408744484557, 'mean_word_length': 3.094417077175698, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256845', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1635, 'n_words': 455}