Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code #51

Merged
merged 15 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backends/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
69 changes: 50 additions & 19 deletions backends/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ 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

Expand Down Expand Up @@ -84,17 +90,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+") 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

Expand Down Expand Up @@ -128,12 +142,18 @@ 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):
Expand All @@ -155,17 +175,25 @@ 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

Expand All @@ -179,7 +207,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]:
Expand Down Expand Up @@ -218,5 +247,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."
)
127 changes: 127 additions & 0 deletions logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!bin/python

import time
from hashlib import sha256


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):
if (size > 1024.0):
size /= 1024.0
count += 1
else:
break
return str(round(size, 1)) + " " + scales[count]


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:
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


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

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


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

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
Loading