<commit_before>fs = require 'fs'
path = require 'path'
debug = require('debug') 'tastetyper'
express = require 'express'
morgan = require 'morgan'
serveStatic = require 'serve-static'
rawBody = require 'raw-body'
mediaTyper = require 'media-typer'
cookieParser = require 'cookie-parser'

module.exports = exports = (config = {}) ->
  defaultTheme = config.theme

  config.hljsStylesHtml = ['\n']
  for hljsStyle in config.hljsStyles
    selected = ''
    selected = ' selected'  if hljsStyle is config.hljsStyle
    config.hljsStylesHtml.push "<option#{selected} value=\"#{hljsStyle}\">#{hljsStyle}</option>\n"
  config.hljsStylesHtml = config.hljsStylesHtml.join ''
  config.hljsStylesHtml = ''  if config.hljsStyles.length <= 1

  app = express.Router {strict: true}
  {saveFile} = exports

  app.use morgan config.morgan.format

  app.get '/', cookieParser(), (req, res, next) ->
    config.theme = defaultTheme
    config.theme = req.cookies.theme  if req.cookies?.theme in config.themes

    tpl = "#{__dirname}/static/#{config.theme}.mustache"
    unless fs.existsSync tpl
      res.clearCookie 'theme'
      config.theme = defaultTheme
      tpl = "#{__dirname}/static/#{config.theme}.mustache"

    config.themesHtml = ['\n']
    for theme in config.themes
      selected = ''
      selected = ' selected'  if theme is config.theme
      config.themesHtml.push "<option#{selected} value=\"#{theme}\">#{theme}</option>\n"
    config.themesHtml = config.themesHtml.join ''
    config.themesHtml = ''  if config.themes.length <= 1

    res.render tpl, {config}

  app.put '/tastes/:filename', (req, res, next) ->
    if req.params.filename.length > config.maxFilenameLength
      return res.status(414).send()
    if "/#{req.params.filename}" isnt path.resolve '/', req.params.filename
      return res.status(400).send()
    relPath = "tastes/#{req.params.filename}"
    saveFile relPath, config, req, res, (err) ->
      return next err  if err?
      res.status(204).send()

  app.use '/tastes', serveStatic config.tastesDir
  app.use serveStatic 'static'
  app


exports.saveFile = (relPath, config, req, res, next) ->
  contentType = req.headers['content-type']
  encoding = 'utf-8'
  encoding = mediaTyper.parse(contentType).parameters.charset  if contentType?
  rawBody req, {
    length: req.headers['content-length']
    limit: config.maxSize
    encoding
  }, (err, data) ->
    return next err  if err?
    fs.writeFile relPath, data, {encoding}, next
<commit_msg>allow nested files<commit_after>fs = require 'fs'
path = require 'path'
debug = require('debug') 'tastetyper'
express = require 'express'
morgan = require 'morgan'
serveStatic = require 'serve-static'
rawBody = require 'raw-body'
mediaTyper = require 'media-typer'
cookieParser = require 'cookie-parser'

module.exports = exports = (config = {}) ->
  defaultTheme = config.theme

  config.hljsStylesHtml = ['\n']
  for hljsStyle in config.hljsStyles
    selected = ''
    selected = ' selected'  if hljsStyle is config.hljsStyle
    config.hljsStylesHtml.push "<option#{selected} value=\"#{hljsStyle}\">#{hljsStyle}</option>\n"
  config.hljsStylesHtml = config.hljsStylesHtml.join ''
  config.hljsStylesHtml = ''  if config.hljsStyles.length <= 1

  app = express.Router {strict: true}
  {saveFile} = exports

  app.use morgan config.morgan.format

  app.get '/', cookieParser(), (req, res, next) ->
    config.theme = defaultTheme
    config.theme = req.cookies.theme  if req.cookies?.theme in config.themes

    tpl = "#{__dirname}/static/#{config.theme}.mustache"
    unless fs.existsSync tpl
      res.clearCookie 'theme'
      config.theme = defaultTheme
      tpl = "#{__dirname}/static/#{config.theme}.mustache"

    config.themesHtml = ['\n']
    for theme in config.themes
      selected = ''
      selected = ' selected'  if theme is config.theme
      config.themesHtml.push "<option#{selected} value=\"#{theme}\">#{theme}</option>\n"
    config.themesHtml = config.themesHtml.join ''
    config.themesHtml = ''  if config.themes.length <= 1

    res.render tpl, {config}

  app.put /^\/tastes\/(.+)/, (req, res, next) ->
    filename = req.params[0]
    if filename.length > config.maxFilenameLength
      return res.status(414).send()
    if "/#{filename}" isnt path.resolve '/', filename
      return res.status(400).send()
    relPath = "tastes/#{filename}"
    saveFile relPath, config, req, res, (err) ->
      return next err  if err?
      res.status(204).send()

  app.use '/tastes', serveStatic config.tastesDir
  app.use serveStatic 'static'
  app


exports.saveFile = (relPath, config, req, res, next) ->
  contentType = req.headers['content-type']
  encoding = 'utf-8'
  encoding = mediaTyper.parse(contentType).parameters.charset  if contentType?
  rawBody req, {
    length: req.headers['content-length']
    limit: config.maxSize
    encoding
  }, (err, data) ->
    return next err  if err?
    fs.writeFile relPath, data, {encoding}, next

{'fraction_non_alphanumeric': 0.14510913500404204, 'fraction_numerical': 0.0050525464834276475, 'mean_word_length': 3.482789855072464, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, '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': '1941628', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1573, 'n_words': 372}