<commit_before>fs = require 'fs'
path = require 'path'
multer = require 'multer'
bodyParser = require 'body-parser'
express = require 'express'
cors = require 'cors'
app = express()
router = express.Router()
logger = require './lib/logger'
validator = require './lib/validator'
configParser = require './lib/configParser'
database = require './lib/database'

if !configParser.parsed
  if !configParser.parse()
    process.exit 1
port = configParser.port
uploadPath = configParser.uploadPath
maxFileSize = configParser.maxFileSize
skipAuth = configParser.skipAuth
debug = configParser.debug

app.use bodyParser.urlencoded(extended: true, limit: maxFileSize)
app.use bodyParser.json()

storage = multer.diskStorage(
  destination: (req, file, cb) ->
    cb null, uploadPath
  filename: (req, file, cb) ->
    cb null, Date.now() + '-' +
      file.originalname.replace(new RegExp('[^0-9A-Za-z\\.]+', 'g'), '_').replace(new RegExp('\\.\\.+', 'g'), '_')
)

upload = multer(
  storage: storage
  limits:
    fileSize: maxFileSize).single('file')

router.all '/*', (req, res, next) ->
  res.setHeader 'X-Powered-By', 'Dumpster'
  next()

if configParser.enableWebUI
  router.get '/', (req, res) ->
    res.render 'index',
      title: 'Dumpster WebUI'
      skipAuth: skipAuth

router.post '/api/upload', validator.auth, validator.date, ((req, res, next) ->
  req.socket.setTimeout 60 * 60 * 1000  # 1h
  upload req, res, (err) ->
    if err
      logger.error err
      res.status 400
      res.send 'Upload failed, file too big?\n'
    else
      next()
), validator.md5, validator.scheduleDeletion, validator.sendLink

router.get '/uploads/*', (req, res) ->
  try
    if fs.statSync(__dirname + req.url).isFile()
      res.status 200
      res.setHeader('Content-Type', 'application/force-download')
      res.setHeader('Content-Disposition', 'attachment')
      res.sendFile __dirname + req.url
    else
      res.status 404
      res.end 'Nothing here.\n'
  catch error<commit_msg>Fix for connection reset while uploading large files<commit_after>fs = require 'fs'
path = require 'path'
multer = require 'multer'
bodyParser = require 'body-parser'
express = require 'express'
cors = require 'cors'
app = express()
router = express.Router()
logger = require './lib/logger'
validator = require './lib/validator'
configParser = require './lib/configParser'
database = require './lib/database'

if !configParser.parsed
  if !configParser.parse()
    process.exit 1
port = configParser.port
uploadPath = configParser.uploadPath
maxFileSize = configParser.maxFileSize
skipAuth = configParser.skipAuth
debug = configParser.debug

app.use bodyParser.urlencoded(extended: true, limit: maxFileSize)
app.use bodyParser.json()

storage = multer.diskStorage(
  destination: (req, file, cb) ->
    cb null, uploadPath
  filename: (req, file, cb) ->
    cb null, Date.now() + '-' +
      file.originalname.replace(new RegExp('[^0-9A-Za-z\\.]+', 'g'), '_').replace(new RegExp('\\.\\.+', 'g'), '_')
)

upload = multer(
  storage: storage
  limits:
    fileSize: maxFileSize).single('file')

router.all '/*', (req, res, next) ->
  res.setHeader 'X-Powered-By', 'Dumpster'
  next()

if configParser.enableWebUI
  router.get '/', (req, res) ->
    res.render 'index',
      title: 'Dumpster WebUI'
      skipAuth: skipAuth

router.post '/api/upload', validator.auth, validator.date, ((req, res, next) ->
  req.socket.setTimeout 12 * 60 * 60 * 1000  # 12h
  upload req, res, (err) ->
    if err
      logger.error err
      res.status 400
      res.send 'Upload failed, file too big?\n'
    else
      next()
), validator.md5, validator.scheduleDeletion, validator.sendLink

router.get '/uploads/*', (req, res) ->
  try
    if fs.statSync(__dirname + req.url).isFile()
      res.status 200
      res.setHeader('Content-Type', 'application/force-download')
      res.setHeader('Content-Disposition', 'attachment')
      res.sendFile __dirname + req.url
    else
      res.status 404
      res.end 'Nothing here.\n'
  catch error
{'fraction_non_alphanumeric': 0.15006242197253433, 'fraction_numerical': 0.011735330836454432, 'mean_word_length': 3.470982142857143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4758263', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1307, 'n_words': 335}