<commit_before>fs = require 'fs'
bencode = require '../../lib/bencode'
crypto = require 'crypto'

Torrent = require('../models/torrents').Torrent


exports.list = (req, res) -> 
  Torrent.find {}, (err, docs) ->
    res.render 'torrents/list', {'title' : 'Listing torrents', 'torrents' : docs}

exports.upload = (req, res) ->
  res.render 'torrents/upload', {'title' : 'Upload a torrent'}

exports.upload_post = (req, res) ->
  if req.form
    req.form.complete (err, fields, files) ->
      if !files.torrent
        req.flash 'error', "There was an error with the upload form"
        res.redirect '/upload'
      fs.readFile files.torrent.path, (err, data) ->
        # This needs full error checking to check .torrent is valid
        torrentInfo = bencode.bdecode data
        hasher = crypto.createHash 'sha1'
        hasher.update bencode.bencode(torrentInfo.info)
        infohash = hasher.digest 'hex'

        if fields.title
          title = fields.title
        else
          title = torrentInfo.info.name.toString 'utf8'

        torrentFiles = []
        if torrentInfo.info.files
          size = 0
          for file in torrentInfo.info.files
            torrentFiles.push {
              'path' : (file.path.toString 'utf8'),
              'size' : file.length
            }
            size += file.length
        else
          size = torrentInfo.info.length
          torrentFiles.push {
            'path' : (torrentInfo.info.name.toString 'utf8'),
            'size' : torrentInfo.info.length
          }

        torrent = new Torrent {
          'infohash' : infohash,
          'size'     : size,
          'title'    : title,
          'files'    : torrentFiles,
          'description' : fields.description
        }
        torrent.generatePermalink (err) ->
          torrent.save (err) ->
            fs.writeFile (__dirname+'/../../torrents/' + infohash + '.torrent'), data, (err) ->
              fs.unlink files.torrent.path, (err) ->
                res.redirect '/'
  else
    req.flash 'error', "There was an error with the upload form"
    res.redirect '/upload'

exports.download = (req, res) ->
  Torrent.findOne {'permalink' : req.params.permalink}, (err, doc) ->
    if doc
      infohash = doc.infohash
      fs.readFile (__dirname+'/../../torrents/' + infohash + '.torrent'), (err, data) ->
        if err
          console.log err
          res.send 'There was an error', 500
        else
          torrentInfo = bencode.bdecode data
          if torrentInfo['announce-list']
            delete torrentInfo['announce-list']
          torrentInfo.announce = 'mahtracker'
          
          output = bencode.bencode torrentInfo
          res.contentType 'application/x-bittorrent'

          res.header 'Content-Length', output.length<commit_msg>Add duplicate torrent upload message<commit_after>fs = require 'fs'
bencode = require '../../lib/bencode'
crypto = require 'crypto'

Torrent = require('../models/torrents').Torrent


exports.list = (req, res) -> 
  Torrent.find {}, (err, docs) ->
    res.render 'torrents/list', {'title' : 'Listing torrents', 'torrents' : docs}

exports.upload = (req, res) ->
  res.render 'torrents/upload', {'title' : 'Upload a torrent'}

exports.upload_post = (req, res) ->
  if req.form
    req.form.complete (err, fields, files) ->
      if !files.torrent
        req.flash 'error', "There was an error with the upload form"
        res.redirect '/upload'
      fs.readFile files.torrent.path, (err, data) ->
        # This needs full error checking to check .torrent is valid
        torrentInfo = bencode.bdecode data
        hasher = crypto.createHash 'sha1'
        hasher.update bencode.bencode(torrentInfo.info)
        infohash = hasher.digest 'hex'

        Torrent.findOne {'infohash' : infohash}, (err, doc) ->
          if doc
            req.flash 'error', 'This torrent is already uploaded'
            res.redirect ('/torrent/' + doc.permalink)
          else
            if fields.title
              title = fields.title
            else
              title = torrentInfo.info.name.toString 'utf8'
    
            torrentFiles = []
            if torrentInfo.info.files
              size = 0
              for file in torrentInfo.info.files
                torrentFiles.push {
                  'path' : (file.path.toString 'utf8'),
                  'size' : file.length
                }
                size += file.length
            else
              size = torrentInfo.info.length
              torrentFiles.push {
                'path' : (torrentInfo.info.name.toString 'utf8'),
                'size' : torrentInfo.info.length
              }
    
            torrent = new Torrent {
              'infohash' : infohash,
              'size'     : size,
              'title'    : title,
              'files'    : torrentFiles,
              'description' : fields.description
            }
            torrent.generatePermalink (err) ->
              torrent.save (err) ->
                fs.writeFile (__dirname+'/../../torrents/' + infohash + '.torrent'), data, (err) ->
                  fs.unlink files.torrent.path, (err) ->
                    res.redirect ('/torrent/' + torrent.permalink)
  else
    req.flash 'error', "There was an error with the upload form"
    res.redirect '/upload'

exports.download = (req, res) ->
  Torrent.findOne {'permalink' : req.params.permalink}, (err, doc) ->
    if doc
      infohash = doc.infohash
      fs.readFile (__dirname+'/../../torrents/' + infohash + '.torrent'), (err, data) ->
        if err
          console.log err
          res.send 'There was an error', 500
        else
          torrentInfo = bencode.bdecode data
          if torrentInfo['announce-list']
            delete torrentInfo['announce-list']
          torrentInfo.announce = 'mahtracker'
          
          output = bencode.bencode torrentInfo
          res.contentType 'application/x-bittorrent'

          res.header 'Content-Length', output.length
{'fraction_non_alphanumeric': 0.1239766081871345, 'fraction_numerical': 0.0026733500417710945, 'mean_word_length': 1.8037470725995317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '948776', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1734, 'n_words': 425}