diff --git a/web/templates/uuid.html b/web/templates/uuid.html index a23eb5c..bf3ecee 100644 --- a/web/templates/uuid.html +++ b/web/templates/uuid.html @@ -41,8 +41,12 @@

Nansen Legacy UUID Generator

${str(uuid.uuid1())}

${str(uuid.uuid1())}

${str(uuid.uuid1())}

-
-

+
+

+

OR

+

Create a .txt file with any number of UUIDs

+Enter number of UUIDs to generate:

+

diff --git a/web/uuid.cgi b/web/uuid.cgi index c359aa1..ecb54f1 100755 --- a/web/uuid.cgi +++ b/web/uuid.cgi @@ -20,12 +20,24 @@ import codecs import uuid import shutil import textwrap +import tempfile __updated__ = '2018-06-29' -# cgitb.enable() +cgitb.enable() +def uuids_many(path, number=100): + ''' + Creates a file with n uuids, one per line + ''' + f = open(path,"w") + for n in range(number): + f.write(str(uuid.uuid1())+'\n') + +def warn(message, color='red'): + message = bytes(message, "utf-8") + sys.stdout.buffer.write(b'

'+message+b'

') method = os.environ.get("REQUEST_METHOD", "GET") @@ -35,10 +47,6 @@ from mako.lookup import TemplateLookup templates = TemplateLookup( directories=['templates'], output_encoding='utf-8') - -# method = 'POST' -# method = 'Test' - if method == "GET": # This is for getting the page # Using sys, as print doesn't work for cgi in python3 @@ -50,9 +58,39 @@ if method == "GET": # This is for getting the page template.render(uuid=uuid)) elif method == "POST": - template = templates.get_template("uuid.html") - - sys.stdout.flush() - sys.stdout.buffer.write(b"Content-Type: text/html\n\n") - sys.stdout.buffer.write( - template.render(uuid=uuid)) + + form = cgi.FieldStorage() + + if "file" in form: + tmpfile = '/tmp/tmpfile.txt' + try: + number = int(form["number"].value) + except: + number = 0 + + if number < 1: + error = "Please enter an integer greater than 0" + sys.stdout.flush() + sys.stdout.buffer.write(b"Content-Type: text/html\n\n") + sys.stdout.buffer.write(b"\n\n ") + warn(error) + sys.stdout.buffer.write(b'') + else: + + filename='uuids.txt' + print("Content-Type: text/plain") + print("Content-Disposition: attachment; filename="+filename+"\n") + path = "/tmp/" + next(tempfile._get_candidate_names()) + ".txt" + + uuids_many(path, number) + + with open(path, "rb") as f: + sys.stdout.flush() + shutil.copyfileobj(f, sys.stdout.buffer) + else: + template = templates.get_template("uuid.html") + + sys.stdout.flush() + sys.stdout.buffer.write(b"Content-Type: text/html\n\n") + sys.stdout.buffer.write( + template.render(uuid=uuid))