<commit_before>#!/usr/bin/python2
#!/home/jason/local/bin/python


# Outline of code:
# Read the SVG
# Read the type to convert to
# Read the options (width, height, dpi, quality)  (TODO)
# Generate a hash for caching and check to see if we've already done this (TODO)
# Convert with scripts off
# Check the output file size.
# Send the result back

import cgi
#import cgitb; cgitb.enable() # Show errors to browser.
import sys
import os
import time
import md5
sys.stderr = sys.stdout

mediatypes={
  'pdf':'application/pdf',
  'ps':'application/postscript',
  'jpg':'image/jpeg',
  'png':'image/png',
  'svg':'image/svg+xml'
}

debug = False
redirect = True

if debug:
    print 'Content-type: text/plain\n\n'

form = cgi.FieldStorage()
time.sleep(0.1) # Throttle requests
if debug:
    print 'Debug mode of convert_svg.py\n'
    print 'form.keys(): ' + form.keys().__str__()+'\n'
    for key in form.keys():
        print 'form['+key+'] = '+form[key].value+'\n'
source = form['source'].value
type = form['type'].value
mime = mediatypes[type]

md5hex = md5.new(source).hexdigest()
svgname = 'files/'+md5hex+'.svg'
outname = 'files/'+md5hex+'.'+type

if not os.path.isfile(outname) or source!=open(svgname, 'r' ).read():<commit_msg>Added important security cgi.maxlen feature.<commit_after>#!/usr/bin/python2
#!/home/jason/local/bin/python


# Outline of code:
# Read the SVG
# Read the type to convert to
# Read the options (width, height, dpi, quality)  (TODO)
# Generate a hash for caching and check to see if we've already done this (TODO)
# Convert with scripts off
# Check the output file size.
# Send the result back

import cgi
#import cgitb; cgitb.enable() # Show errors to browser.
import sys
import os
import time
import md5
sys.stderr = sys.stdout
cgi.maxlen = 100*1024

mediatypes={
  'pdf':'application/pdf',
  'ps':'application/postscript',
  'jpg':'image/jpeg',
  'png':'image/png',
  'svg':'image/svg+xml'
}

debug = False
redirect = True

if debug:
    print 'Content-type: text/plain\n\n'

form = cgi.FieldStorage()
time.sleep(0.1) # Throttle requests
if debug:
    print 'Debug mode of convert_svg.py\n'
    print 'form.keys(): ' + form.keys().__str__()+'\n'
    for key in form.keys():
        print 'form['+key+'] = '+form[key].value+'\n'
source = form['source'].value
type = form['type'].value
mime = mediatypes[type]

md5hex = md5.new(source).hexdigest()
svgname = 'files/'+md5hex+'.svg'
outname = 'files/'+md5hex+'.'+type

if not os.path.isfile(outname) or source!=open(svgname, 'r' ).read():
{'fraction_non_alphanumeric': 0.1617004370282082, 'fraction_numerical': 0.009137862534763607, 'mean_word_length': 4.191752577319588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4189237', 'n_tokens_mistral': 927, 'n_tokens_neox': 899, 'n_words': 255}