From 1f8d83c5381275ae92ed2c3415832dea4c5f2ce6 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 12:47:00 +0300 Subject: [PATCH 01/15] Refactor new_paste() and add new file A new file called `logic.py` has been created which will gradually start to have more and more of the application logic there. --- logic.py | 45 +++++++++++++++++++++++++++++++++++++ torpaste.py | 64 +++++++++++++---------------------------------------- 2 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 logic.py diff --git a/logic.py b/logic.py new file mode 100644 index 0000000..b4d8a43 --- /dev/null +++ b/logic.py @@ -0,0 +1,45 @@ +#!bin/python + +import time +from hashlib import sha256 + +def format_size(size): + scales = ["bytes", "kB", "MB", "GB", "TB", "PB"] + count = 0 + while (1 == 1): + if (size > 1024.0): + size /= 1024.0 + count += 1 + else: + break + return str(round(size, 1)) + " " + scales[count] + +def create_new_paste(content, config): + try: + paste_id = str(sha256(content.encode('utf-8')).hexdigest()) + except: + return "ERROR", "An issue occured while handling the paste. \ + Please try again later. If the problem persists, try \ + notifying a system administrator." + + if (len(content.encode('utf-8')) > config['MAX_PASTE_SIZE']): + return "ERROR", "The paste sent is too large. This TorPaste \ + instance has a maximum allowed paste size of " + \ + format_size(config['MAX_PASTE_SIZE']) + "." + + try: + config['b'].new_paste(paste_id, content) + except config['b'].e.ErrorException as errmsg: + return "ERROR", errmsg + + try: + config['b'].update_paste_metadata( + paste_id, + { + "date": str(int(time.time())) + } + ) + except config['b'].e.ErrorException as errmsg: + return "ERROR", errmsg + + return "OK", paste_id diff --git a/torpaste.py b/torpaste.py index 2dbe7f1..3c95dd3 100755 --- a/torpaste.py +++ b/torpaste.py @@ -7,6 +7,7 @@ from hashlib import sha256 from os import getenv import sys +import logic from flask import * @@ -35,57 +36,22 @@ def new_paste(): page = "new" ) else: - if request.form['content']: - try: - paste_id = str(sha256(request.form['content'].encode('utf-8')).hexdigest()) - except: - return render_template( - "index.html", - config = config, - version = VERSION, - page = "new", - error = "An issue occurred while handling the paste. Please try again later. If the problem persists,\ - try notifying a system administrator." - ) - - if len(request.form['content'].encode('utf-8')) > config['MAX_PASTE_SIZE']: - return render_template( - "index.html", - config = config, - version = VERSION, - page = "new", - error = "The paste sent is too large. This TorPaste instance has a maximum allowed paste size of " - + format_size(config['MAX_PASTE_SIZE']) + "." - ) - - try: - b.new_paste(paste_id, request.form['content']) - except b.e.ErrorException as errmsg: - return render_template( - "index.html", - config = config, - version = VERSION, - page = "new", - error = errmsg - ) - - try: - b.update_paste_metadata( - paste_id, - { - "date": str(int(time.time())) - } - ) - except b.e.ErrorException as errmsg: - return render_template( - "index.html", - config = config, - version = VERSION, - page = "new", - error = errmsg + if (request.form['content']): + status, message = logic.create_new_paste(request.form['content'], config) + if (status == "ERROR"): + return Response( + render_template( + "index.html", + config = config, + version = VERSION, + page = "new", + error = message + ), + 400 ) - return redirect("/view/" + paste_id) + if (status == "OK"): + return redirect("/view/" + message) else: return Response( render_template( From 88b8af61d33ed7fc96d57c15d35ed7b22824ab83 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 13:17:14 +0300 Subject: [PATCH 02/15] Convert view_paste() to the new format (w/ logic.py) --- logic.py | 28 ++++++++++++++++++ torpaste.py | 82 +++++++++++++---------------------------------------- 2 files changed, 47 insertions(+), 63 deletions(-) diff --git a/logic.py b/logic.py index b4d8a43..3c71a63 100644 --- a/logic.py +++ b/logic.py @@ -43,3 +43,31 @@ def create_new_paste(content, config): return "ERROR", errmsg return "OK", paste_id + +def view_existing_paste(paste_id, config): + if (not paste_id.isalnum()): + return "ERROR", "Invalid Paste ID. Please check the link \ + you used or use the Pastes button above.", 400 + + if (len(paste_id) != 64): + return "ERROR", "Paste ID has invalid length. Paste IDs \ + are 64 characters long. Please make sure the link you \ + clicked is correct or use the Pastes button above.", 400 + + if (not config['b'].does_paste_exist(paste_id)): + return "ERROR", "A paste with this Paste ID could not be \ + found. Sorry.", 404 + + try: + paste_content = config['b'].get_paste_contents(paste_id) + except config['b'].e.ErrorException as errmsg: + return "ERROR", errmsg, 500 + + try: + paste_date = config['b'].get_paste_metadata_value(paste_id, "date") + except config['b'].e.ErrorException as errmsg: + return "ERROR", errmsg, 500 + except config['b'].e.WarningException as errmsg: + return "WARNING", errmsg, 500 + + return "OK", (paste_content, paste_date), 200 diff --git a/torpaste.py b/torpaste.py index 3c95dd3..006e9dd 100755 --- a/torpaste.py +++ b/torpaste.py @@ -67,84 +67,40 @@ def new_paste(): @app.route("/view/") def view_paste(pasteid): - if not pasteid.isalnum(): - return Response( - render_template( - "index.html", - config = config, - version = VERSION, - error = "Invalid Paste ID. Please check the link you used or use Pastes button above.", - page = "new" - ), - 400 - ) - if len(pasteid) < 6: + status, data, code = logic.view_existing_paste(pasteid, config) + + if (status == "ERROR"): return Response( render_template( "index.html", config = config, version = VERSION, - error = "Paste ID too short. Usually Paste IDs are longer than 6 characters. Please make sure the link \ - you clicked is correct or use the Pastes button above.", + error = data, page = "new" ), - 400 + code ) - if not b.does_paste_exist(pasteid): + + if (status == "OK"): + paste_date = datetime.fromtimestamp(int(data[1]) + time.altzone + 3600)\ + .strftime("%H:%M:%S %d/%m/%Y") + + paste_size = logic.format_size(len(data[0].encode('utf-8'))) + return Response( render_template( - "index.html", + "view.html", + content = data[0], + date = paste_date, + size = paste_size, + pid = pasteid, config = config, version = VERSION, - error = "A paste with this ID could not be found. Sorry.", - page = "new" + page = "view" ), - 404 + 200 ) - try: - paste_content = b.get_paste_contents(pasteid) - except b.e.ErrorException as errmsg: - return render_template( - "index.html", - config = config, - version = VERSION, - error = errmsg, - page = "new" - ) - - try: - paste_date = b.get_paste_metadata_value(pasteid, "date") - except b.e.ErrorException as errmsg: - return render_template( - "index.html", - config = config, - version = VERSION, - error = errmsg, - page = "new" - ) - except b.e.WarningException as errmsg: - return render_template( - "index.html", - config = config, - version = VERSION, - warning = errmsg, - page = "new" - ) - - paste_date = datetime.fromtimestamp(int(paste_date) + time.altzone + 3600).strftime("%H:%M:%S %d/%m/%Y") - paste_size = format_size(len(paste_content.encode('utf-8'))) - return render_template( - "view.html", - content = paste_content, - date = paste_date, - size = paste_size, - pid = pasteid, - config = config, - version = VERSION, - page = "view" - ) - @app.route("/raw/") def raw_paste(pasteid): From cfbcda1fda7a37c4d1f2192e12195f88f6517e9e Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 13:29:12 +0300 Subject: [PATCH 03/15] Refactor raw_paste() to make use of `logic.py` --- torpaste.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/torpaste.py b/torpaste.py index 006e9dd..d24a3bd 100755 --- a/torpaste.py +++ b/torpaste.py @@ -104,23 +104,13 @@ def view_paste(pasteid): @app.route("/raw/") def raw_paste(pasteid): - if not pasteid.isalnum(): - return "No such paste", 404 - if len(pasteid) < 6: - return "No such paste", 404 - if not b.does_paste_exist(pasteid): - return "No such paste", 404 - try: - paste_content = b.get_paste_contents(pasteid) - except b.e.ErrorException as errmsg: - return Response( - errmsg, - 500 - ) - return Response( - paste_content, - mimetype = "text/plain" - ) + status, data, code = logic.view_existing_paste(pasteid, config) + + if (status == "ERROR" and code >= 500): + return Response(data, code, mimetype="text/plain") + if (status == "ERROR"): + return Response("No such paste", code, mimetype="text/plain") + return Response(data[0], mimetype="text/plain") @app.route("/list") From 97a1f1bbc937c738771c17c0f6c06441f532d273 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 13:37:25 +0300 Subject: [PATCH 04/15] Fix some line width issues w/ Python strings --- logic.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/logic.py b/logic.py index 3c71a63..63b6af0 100644 --- a/logic.py +++ b/logic.py @@ -18,13 +18,13 @@ def create_new_paste(content, config): try: paste_id = str(sha256(content.encode('utf-8')).hexdigest()) except: - return "ERROR", "An issue occured while handling the paste. \ - Please try again later. If the problem persists, try \ - notifying a system administrator." + return "ERROR", "An issue occured while handling the paste. "+\ + "Please try again later. If the problem persists, try "+\ + "notifying a system administrator." if (len(content.encode('utf-8')) > config['MAX_PASTE_SIZE']): - return "ERROR", "The paste sent is too large. This TorPaste \ - instance has a maximum allowed paste size of " + \ + return "ERROR", "The paste sent is too large. This TorPaste "+\ + "instance has a maximum allowed paste size of "+\ format_size(config['MAX_PASTE_SIZE']) + "." try: @@ -46,17 +46,17 @@ def create_new_paste(content, config): def view_existing_paste(paste_id, config): if (not paste_id.isalnum()): - return "ERROR", "Invalid Paste ID. Please check the link \ - you used or use the Pastes button above.", 400 + return "ERROR", "Invalid Paste ID. Please check the link "+\ + "you used or use the Pastes button above.", 400 if (len(paste_id) != 64): - return "ERROR", "Paste ID has invalid length. Paste IDs \ - are 64 characters long. Please make sure the link you \ - clicked is correct or use the Pastes button above.", 400 + return "ERROR", "Paste ID has invalid length. Paste IDs "+\ + "are 64 characters long. Please make sure the link you "+\ + "clicked is correct or use the Pastes button above.", 400 if (not config['b'].does_paste_exist(paste_id)): - return "ERROR", "A paste with this Paste ID could not be \ - found. Sorry.", 404 + return "ERROR", "A paste with this Paste ID could not be "+\ + "found. Sorry.", 404 try: paste_content = config['b'].get_paste_contents(paste_id) From 6841a1c32a2db490f878190d39b1e28c2a15e0a7 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 13:39:47 +0300 Subject: [PATCH 05/15] Keep `torpaste.py` in under 80 characters per line --- torpaste.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/torpaste.py b/torpaste.py index d24a3bd..18040d6 100755 --- a/torpaste.py +++ b/torpaste.py @@ -37,7 +37,10 @@ def new_paste(): ) else: if (request.form['content']): - status, message = logic.create_new_paste(request.form['content'], config) + status, message = logic.create_new_paste( + request.form['content'], + config + ) if (status == "ERROR"): return Response( render_template( @@ -192,7 +195,8 @@ def load_config(): if BACKEND in COMPATIBLE_BACKENDS: b = importlib.import_module('backends.'+BACKEND) else: - print("Configured backend (" + BACKEND + ") is not compatible with current version.") + print("Configured backend (" + BACKEND + ") is not compatible with "+\ + "current version.") exit(1) # Maximum Paste Size From ec3875683151a004bdaf103077ef2166d53145bd Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 13:47:48 +0300 Subject: [PATCH 06/15] Keep backends under 80 characters per line. --- backends/example.py | 3 ++- backends/filesystem.py | 56 ++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/backends/example.py b/backends/example.py index 911c388..0915342 100644 --- a/backends/example.py +++ b/backends/example.py @@ -146,7 +146,8 @@ def get_paste_metadata_value(paste_id, key): passed are typically ASCII. :param paste_id: ASCII string which represents the ID of the paste :param key: key of the metadata - :return: value of the metadata key provided for the given ID, None if the key wasn't set + :return: value of the metadata key provided for the given ID, None if + the key wasn't set """ if get_paste_metadata(paste_id)[key]: diff --git a/backends/filesystem.py b/backends/filesystem.py index 5cd0f12..7d19acf 100644 --- a/backends/filesystem.py +++ b/backends/filesystem.py @@ -48,8 +48,9 @@ def new_paste(paste_id, paste_content): fd.write(paste_content) except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again later. If the problem persists, \ - try notifying a system administrator.") + "An issue occurred with the local filesystem. Please try again "+\ + "later. If the problem persists, try notifying a system "+\ + "administrator.") return @@ -84,17 +85,23 @@ def update_paste_metadata(paste_id, metadata): os.remove("pastes/" + a + "/" + b + "/" + j) except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again later. If the problem persists, \ - try notifying a system administrator.") + "An issue occurred with the local filesystem. Please try again "+\ + "later. If the problem persists, try notifying a system "+\ + "administrator.") try: for k, v in metadata.items(): - with codecs.open(ppath + "." + k, encoding = "utf-8", mode = "w+") as fd: + with codecs.open( + ppath + "." + k, + encoding = "utf-8", + mode = "w+" + ) as fd: fd.write(v) except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again later. If the problem persists, \ - try notifying a system administrator.") + "An issue occurred with the local filesystem. Please try again "+\ + "later. If the problem persists, try notifying a system "+\ + "administrator.") return @@ -128,12 +135,17 @@ def get_paste_contents(paste_id): b = paste_id[2:4] try: - with codecs.open("pastes/" + a + "/" + b + "/" + paste_id, encoding = "utf-8", mode = "r") as fd: + with codecs.open( + "pastes/" + a + "/" + b + "/" + paste_id, + encoding = "utf-8", + mode = "r" + ) as fd: return fd.read() except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again later. If the problem persists, try \ - notifying a system administrator.") + "An issue occurred with the local filesystem. Please try again "+\ + "later. If the problem persists, try notifying a system "+\ + "administrator.") def get_paste_metadata(paste_id): @@ -155,17 +167,23 @@ def get_paste_metadata(paste_id): for f in os.listdir("pastes/" + a + "/" + b + "/"): if (paste_id in f) and ("." in f): t = f.split(".")[1] - with codecs.open("pastes/" + a + "/" + b + "/" + f, encoding = "utf-8", mode = "r") as fd: + with codecs.open( + "pastes/" + a + "/" + b + "/" + f, + encoding = "utf-8", + mode = "r" + ) as fd: ret[t] = fd.read() except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again later. If the problem persists, \ - try notifying a system administrator.") + "An issue occurred with the local filesystem. Please try again "+\ + "later. If the problem persists, try notifying a system "+\ + "administrator.") if len(ret) == 0: raise e.WarningException( - "Failed to load Paste Metadata. Some features like the paste date may not work. If the problem persists, \ - try notifying a system administrator.") + "Failed to load Paste Metadata. Some features like the paste "+\ + "date may not work. If the problem persists, try notifying a "+\ + "system administrator.") return ret @@ -179,7 +197,8 @@ def get_paste_metadata_value(paste_id, key): passed are typically ASCII. :param paste_id: ASCII string which represents the ID of the paste :param key: key of the metadata - :return: value of the metadata key provided for the given ID, None if the key wasn't set + :return: value of the metadata key provided for the given ID, None if + the key wasn't set """ if get_paste_metadata(paste_id)[key]: @@ -218,5 +237,6 @@ def get_all_paste_ids(): except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again later. If the problem persists, try \ - notifying a system administrator.") + "An issue occurred with the local filesystem. Please try again "+\ + "later. If the problem persists, try notifying a system "+\ + "administrator.") From 5c1334808d5117f9592fc3b352afb23c3d43a0d9 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 14:00:00 +0300 Subject: [PATCH 07/15] Refactor list() to use `logic.py` --- logic.py | 15 +++++++++++++++ torpaste.py | 42 ++++++++++++++---------------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/logic.py b/logic.py index 63b6af0..e4bcaa8 100644 --- a/logic.py +++ b/logic.py @@ -71,3 +71,18 @@ def view_existing_paste(paste_id, config): return "WARNING", errmsg, 500 return "OK", (paste_content, paste_date), 200 + +def get_paste_listing(config): + if (not config['PASTE_LIST_ACTIVE']): + return "ERROR", "Paste listing has been disabled by the administrator.", + 503 + + try: + paste_list = config['b'].get_all_paste_ids() + except config['b'].e.ErrorException as errmsg: + return "ERROR", errmsg, 500 + + if (paste_list[0] == "none"): + return "OK", "none", 200 + + return "OK", paste_list, 200 diff --git a/torpaste.py b/torpaste.py index 18040d6..36e6e31 100755 --- a/torpaste.py +++ b/torpaste.py @@ -118,44 +118,30 @@ def raw_paste(pasteid): @app.route("/list") def list(): - # listing disabled! - if not config['PASTE_LIST_ACTIVE']: - return render_template( - "index.html", - config = config, - version = VERSION, - page = "new", - error = 'Paste listing has been disabled by the administrator.' - ) + status, data, code = logic.get_paste_listing(config) - try: - paste_list = b.get_all_paste_ids() - except b.e.ErrorException as errmsg: - return render_template( - "index.html", - config = config, - version = VERSION, - page = "new", - error = errmsg + if (status == "ERROR"): + return Response( + render_template( + "index.html", + config = config, + version = VERSION, + page = "new", + error = data + ), + code ) - if paste_list[0] == 'none': - return render_template( + return Response( + render_template( "list.html", - pastes = ['none'], + pastes = data, config = config, version = VERSION, page = "list" ) - return render_template( - "list.html", - pastes = paste_list, - config = config, - version = VERSION, - page = "list" ) - @app.route("/about") def about_tor_paste(): return render_template( From 0794b0ae68c032ddf646896ee0564910a971bbdb Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 14:06:57 +0300 Subject: [PATCH 08/15] Remove format_size from `torpaste.py` --- torpaste.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/torpaste.py b/torpaste.py index 36e6e31..b9efa08 100755 --- a/torpaste.py +++ b/torpaste.py @@ -152,19 +152,6 @@ def about_tor_paste(): ) -# Functions -def format_size(size): - scales = ["bytes", "kB", "MB", "GB", "TB", "PB"] - count = 0 - while 1 == 1: - if size > 1024.0: - size /= 1024.0 - count += 1 - else: - break - return str(round(size, 1)) + " " + scales[count] - - # Required Initialization Code # necessary for local modules import (backends, exceptions) From 4ff9fc7853543c649604937af685d7d78e1bfaa7 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 14:08:49 +0300 Subject: [PATCH 09/15] Remove unneeded import from `torpaste.py` --- torpaste.py | 1 - 1 file changed, 1 deletion(-) diff --git a/torpaste.py b/torpaste.py index b9efa08..285eb87 100755 --- a/torpaste.py +++ b/torpaste.py @@ -4,7 +4,6 @@ import importlib import time from datetime import datetime -from hashlib import sha256 from os import getenv import sys import logic From e823d436558cd1276e536bb41cb5c2d562473264 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 14:15:18 +0300 Subject: [PATCH 10/15] Completely convert `logic.py` to PEP8 --- logic.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/logic.py b/logic.py index e4bcaa8..c306e54 100644 --- a/logic.py +++ b/logic.py @@ -3,6 +3,7 @@ import time from hashlib import sha256 + def format_size(size): scales = ["bytes", "kB", "MB", "GB", "TB", "PB"] count = 0 @@ -14,24 +15,25 @@ def format_size(size): break return str(round(size, 1)) + " " + scales[count] + def create_new_paste(content, config): try: paste_id = str(sha256(content.encode('utf-8')).hexdigest()) except: - return "ERROR", "An issue occured while handling the paste. "+\ - "Please try again later. If the problem persists, try "+\ - "notifying a system administrator." + return "ERROR", "An issue occured while handling the paste. " +\ + "Please try again later. If the problem persists, try " +\ + "notifying a system administrator." if (len(content.encode('utf-8')) > config['MAX_PASTE_SIZE']): - return "ERROR", "The paste sent is too large. This TorPaste "+\ - "instance has a maximum allowed paste size of "+\ - format_size(config['MAX_PASTE_SIZE']) + "." + return "ERROR", "The paste sent is too large. This TorPaste " +\ + "instance has a maximum allowed paste size of " +\ + format_size(config['MAX_PASTE_SIZE']) + "." try: config['b'].new_paste(paste_id, content) except config['b'].e.ErrorException as errmsg: return "ERROR", errmsg - + try: config['b'].update_paste_metadata( paste_id, @@ -44,19 +46,20 @@ def create_new_paste(content, config): return "OK", paste_id + def view_existing_paste(paste_id, config): if (not paste_id.isalnum()): - return "ERROR", "Invalid Paste ID. Please check the link "+\ - "you used or use the Pastes button above.", 400 + return "ERROR", "Invalid Paste ID. Please check the link " +\ + "you used or use the Pastes button above.", 400 if (len(paste_id) != 64): - return "ERROR", "Paste ID has invalid length. Paste IDs "+\ - "are 64 characters long. Please make sure the link you "+\ - "clicked is correct or use the Pastes button above.", 400 + return "ERROR", "Paste ID has invalid length. Paste IDs " +\ + "are 64 characters long. Please make sure the link you " +\ + "clicked is correct or use the Pastes button above.", 400 if (not config['b'].does_paste_exist(paste_id)): - return "ERROR", "A paste with this Paste ID could not be "+\ - "found. Sorry.", 404 + return "ERROR", "A paste with this Paste ID could not be " +\ + "found. Sorry.", 404 try: paste_content = config['b'].get_paste_contents(paste_id) @@ -72,10 +75,11 @@ def view_existing_paste(paste_id, config): return "OK", (paste_content, paste_date), 200 + def get_paste_listing(config): if (not config['PASTE_LIST_ACTIVE']): - return "ERROR", "Paste listing has been disabled by the administrator.", - 503 + return "ERROR", "Paste listing has been disabled by the " +\ + "administrator.", 503 try: paste_list = config['b'].get_all_paste_ids() From 68a977cab14881ad41dfbea19842ccba3b135f58 Mon Sep 17 00:00:00 2001 From: DaKnOb Date: Sat, 15 Oct 2016 14:21:57 +0300 Subject: [PATCH 11/15] Modify `torpaste.py` to follow PEP8 --- torpaste.py | 89 +++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/torpaste.py b/torpaste.py index 285eb87..84dc7f4 100755 --- a/torpaste.py +++ b/torpaste.py @@ -15,24 +15,25 @@ VERSION = "0.6" COMPATIBLE_BACKENDS = ["filesystem"] + @app.route('/') def index(): return render_template( "index.html", - config = config, - version = VERSION, - page = "main" + config=config, + version=VERSION, + page="main" ) -@app.route("/new", methods = ["GET", "POST"]) +@app.route("/new", methods=["GET", "POST"]) def new_paste(): if request.method == "GET": return render_template( "index.html", - config = config, - version = VERSION, - page = "new" + config=config, + version=VERSION, + page="new" ) else: if (request.form['content']): @@ -44,10 +45,10 @@ def new_paste(): return Response( render_template( "index.html", - config = config, - version = VERSION, - page = "new", - error = message + config=config, + version=VERSION, + page="new", + error=message ), 400 ) @@ -58,10 +59,10 @@ def new_paste(): return Response( render_template( "index.html", - config = config, - version = VERSION, - error = "Please enter some text to include in the paste.", - page = "new" + config=config, + version=VERSION, + error="Please enter some text to include in the paste.", + page="new" ), 400 ) @@ -75,30 +76,32 @@ def view_paste(pasteid): return Response( render_template( "index.html", - config = config, - version = VERSION, - error = data, - page = "new" + config=config, + version=VERSION, + error=data, + page="new" ), code ) if (status == "OK"): - paste_date = datetime.fromtimestamp(int(data[1]) + time.altzone + 3600)\ - .strftime("%H:%M:%S %d/%m/%Y") + paste_date = datetime.fromtimestamp( + int( + data[1] + ) + time.altzone + 3600).strftime("%H:%M:%S %d/%m/%Y") paste_size = logic.format_size(len(data[0].encode('utf-8'))) return Response( render_template( "view.html", - content = data[0], - date = paste_date, - size = paste_size, - pid = pasteid, - config = config, - version = VERSION, - page = "view" + content=data[0], + date=paste_date, + size=paste_size, + pid=pasteid, + config=config, + version=VERSION, + page="view" ), 200 ) @@ -123,10 +126,10 @@ def list(): return Response( render_template( "index.html", - config = config, - version = VERSION, - page = "new", - error = data + config=config, + version=VERSION, + page="new", + error=data ), code ) @@ -134,20 +137,21 @@ def list(): return Response( render_template( "list.html", - pastes = data, - config = config, - version = VERSION, - page = "list" + pastes=data, + config=config, + version=VERSION, + page="list" ) ) + @app.route("/about") def about_tor_paste(): return render_template( "about.html", - config = config, - version = VERSION, - page = "about" + config=config, + version=VERSION, + page="about" ) @@ -156,6 +160,7 @@ def about_tor_paste(): # necessary for local modules import (backends, exceptions) sys.path.append('.') + # Handle Environment Variables (for configuration) def load_config(): # Web App @@ -167,8 +172,10 @@ def load_config(): if BACKEND in COMPATIBLE_BACKENDS: b = importlib.import_module('backends.'+BACKEND) else: - print("Configured backend (" + BACKEND + ") is not compatible with "+\ - "current version.") + print( + "Configured backend (" + BACKEND + ") is not compatible with " + + "current version." + ) exit(1) # Maximum Paste Size From ae7c804c2db5f4efc95eb681446c805009916675 Mon Sep 17 00:00:00 2001 From: DaKnOb <daknob.mac@gmail.com> Date: Sat, 15 Oct 2016 14:37:55 +0300 Subject: [PATCH 12/15] Convert `filesystem.py` from `backends` to PEP8 --- backends/filesystem.py | 63 +++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/backends/filesystem.py b/backends/filesystem.py index 7d19acf..2e9a958 100644 --- a/backends/filesystem.py +++ b/backends/filesystem.py @@ -44,13 +44,14 @@ def new_paste(paste_id, paste_content): ppath = "pastes/" + a + "/" + b + "/" + paste_id try: - with codecs.open(ppath, encoding = "utf-8", mode = "w+") as fd: + with codecs.open(ppath, encoding="utf-8", mode="w+") as fd: fd.write(paste_content) except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again "+\ - "later. If the problem persists, try notifying a system "+\ - "administrator.") + "An issue occurred with the local filesystem. Please try again " + + "later. If the problem persists, try notifying a system " + + "administrator." + ) return @@ -85,23 +86,25 @@ def update_paste_metadata(paste_id, metadata): os.remove("pastes/" + a + "/" + b + "/" + j) except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again "+\ - "later. If the problem persists, try notifying a system "+\ - "administrator.") + "An issue occurred with the local filesystem. Please try again " + + "later. If the problem persists, try notifying a system " + + "administrator." + ) try: for k, v in metadata.items(): with codecs.open( ppath + "." + k, - encoding = "utf-8", - mode = "w+" + encoding="utf-8", + mode="w+" ) as fd: fd.write(v) except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again "+\ - "later. If the problem persists, try notifying a system "+\ - "administrator.") + "An issue occurred with the local filesystem. Please try again " + + "later. If the problem persists, try notifying a system " + + "administrator." + ) return @@ -137,15 +140,16 @@ def get_paste_contents(paste_id): try: with codecs.open( "pastes/" + a + "/" + b + "/" + paste_id, - encoding = "utf-8", - mode = "r" + encoding="utf-8", + mode="r" ) as fd: return fd.read() except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again "+\ - "later. If the problem persists, try notifying a system "+\ - "administrator.") + "An issue occurred with the local filesystem. Please try again " + + "later. If the problem persists, try notifying a system " + + "administrator." + ) def get_paste_metadata(paste_id): @@ -169,21 +173,23 @@ def get_paste_metadata(paste_id): t = f.split(".")[1] with codecs.open( "pastes/" + a + "/" + b + "/" + f, - encoding = "utf-8", - mode = "r" + encoding="utf-8", + mode="r" ) as fd: ret[t] = fd.read() except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again "+\ - "later. If the problem persists, try notifying a system "+\ - "administrator.") + "An issue occurred with the local filesystem. Please try again " + + "later. If the problem persists, try notifying a system " + + "administrator." + ) if len(ret) == 0: raise e.WarningException( - "Failed to load Paste Metadata. Some features like the paste "+\ - "date may not work. If the problem persists, try notifying a "+\ - "system administrator.") + "Failed to load Paste Metadata. Some features like the paste " + + "date may not work. If the problem persists, try notifying a " + + "system administrator." + ) return ret @@ -237,6 +243,7 @@ def get_all_paste_ids(): except: raise e.ErrorException( - "An issue occurred with the local filesystem. Please try again "+\ - "later. If the problem persists, try notifying a system "+\ - "administrator.") + "An issue occurred with the local filesystem. Please try again " + + "later. If the problem persists, try notifying a system " + + "administrator." + ) From 0b6189e0677febcf7ea671ed2ed32276bc674e21 Mon Sep 17 00:00:00 2001 From: DaKnOb <daknob.mac@gmail.com> Date: Sat, 15 Oct 2016 14:50:27 +0300 Subject: [PATCH 13/15] Split last occurence of codecs to multiline --- backends/filesystem.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backends/filesystem.py b/backends/filesystem.py index 2e9a958..fff8f07 100644 --- a/backends/filesystem.py +++ b/backends/filesystem.py @@ -44,7 +44,11 @@ def new_paste(paste_id, paste_content): ppath = "pastes/" + a + "/" + b + "/" + paste_id try: - with codecs.open(ppath, encoding="utf-8", mode="w+") as fd: + with codecs.open( + ppath, + encoding="utf-8", + mode="w+" + ) as fd: fd.write(paste_content) except: raise e.ErrorException( From e52ecad261098b2efdacfdc3f5da0581774b2fcc Mon Sep 17 00:00:00 2001 From: DaKnOb <daknob.mac@gmail.com> Date: Sat, 15 Oct 2016 15:30:12 +0300 Subject: [PATCH 14/15] Add DocStrings to `logic.py` --- logic.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/logic.py b/logic.py index c306e54..e39fdc0 100644 --- a/logic.py +++ b/logic.py @@ -5,6 +5,14 @@ def format_size(size): + """ + This method formats an arbitrary amount of bytes to a printable + string. For example 1024 will be converted to 1.0 kB. + :param size: an amount of bytes + :return: a string that's ready to be printed representing + approximately the amount of bytes in a human readable + way. + """ scales = ["bytes", "kB", "MB", "GB", "TB", "PB"] count = 0 while (1 == 1): @@ -17,6 +25,16 @@ def format_size(size): def create_new_paste(content, config): + """ + This method is responsible for creating new pastes by directly + talking to the currently used backend. It is also responsible + for ensuring the current input is valid, such as for example that + it is under the Maximum Allowed Paste Size. + :param content: The content of the paste to create + :param config: The TorPaste configuration object + :return: The result of the action (ERROR/OK), some data (error message/ + Paste ID) as well as the suggested HTTP Status Code to return. + """ try: paste_id = str(sha256(content.encode('utf-8')).hexdigest()) except: @@ -48,6 +66,16 @@ def create_new_paste(content, config): def view_existing_paste(paste_id, config): + """ + This method is responsible for checking if a paste with a given Paste ID + exists, and if it does, return its contents and needed metadata in order + for the View Paste view to work. + :param paste_id: The Paste ID to look for. + :param config: The TorPaste configuration object + :return: The result of the action (ERROR/WARNING/OK), some data (error + message / data tuple) as well as the suggested HTTP Status Code + to return. + """ if (not paste_id.isalnum()): return "ERROR", "Invalid Paste ID. Please check the link " +\ "you used or use the Pastes button above.", 400 @@ -77,6 +105,13 @@ def view_existing_paste(paste_id, config): def get_paste_listing(config): + """ + This method is responsible for returning a list of all currently saved + pastes, or a list with only one element ("none") if there are no pastes. + :param config: The TorPaste configuration object + :return: A list with all Paste IDs of all stored pastes. If no stored + pastes exist, a list with only one element, "none". + """ if (not config['PASTE_LIST_ACTIVE']): return "ERROR", "Paste listing has been disabled by the " +\ "administrator.", 503 From de479fda8bc8bd1bbe26b72a723436a3938ec296 Mon Sep 17 00:00:00 2001 From: DaKnOb <daknob.mac@gmail.com> Date: Sat, 15 Oct 2016 15:35:05 +0300 Subject: [PATCH 15/15] Fix bug where "WARNING"s were not handled in Paste Viewing --- torpaste.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/torpaste.py b/torpaste.py index 84dc7f4..1aa645d 100755 --- a/torpaste.py +++ b/torpaste.py @@ -92,19 +92,22 @@ def view_paste(pasteid): paste_size = logic.format_size(len(data[0].encode('utf-8'))) - return Response( - render_template( - "view.html", - content=data[0], - date=paste_date, - size=paste_size, - pid=pasteid, - config=config, - version=VERSION, - page="view" - ), - 200 - ) + if (status == "WARNING"): + paste_date = "Not available." + + return Response( + render_template( + "view.html", + content=data[0], + date=paste_date, + size=paste_size, + pid=pasteid, + config=config, + version=VERSION, + page="view" + ), + 200 + ) @app.route("/raw/<pasteid>")