0bin/zerobin/paste.py

271 lines
8.4 KiB
Python
Raw Normal View History

2012-04-24 22:15:38 +04:00
import os
import hashlib
2015-05-10 20:19:02 +03:00
import base64
import lockfile
2020-08-14 16:08:55 +03:00
import json
2012-04-24 22:15:38 +04:00
from datetime import datetime, timedelta
2020-08-14 16:08:55 +03:00
import bleach
2020-08-14 17:41:45 +03:00
from zerobin.utils import settings
2015-05-10 20:19:02 +03:00
2012-04-24 22:15:38 +04:00
class Paste(object):
"""
2019-05-20 12:31:21 +03:00
A paste object to deal with the file opening/parsing/saving and the
2012-04-24 22:15:38 +04:00
calculation of the expiration date.
"""
2020-08-14 17:41:45 +03:00
DIR_CACHE: set = set()
2012-04-24 22:15:38 +04:00
DURATIONS = {
2020-08-11 17:37:03 +03:00
"1_day": 24 * 3600,
"1_month": 30 * 24 * 3600,
"never": 365 * 24 * 3600 * 100,
2012-04-24 22:15:38 +04:00
}
2020-08-14 16:08:55 +03:00
def __init__(
2020-08-14 17:41:45 +03:00
self,
uuid=None,
uuid_length=None,
content=None,
expiration=None,
title="",
btc_tip_address="",
2020-08-14 16:08:55 +03:00
):
2012-04-24 22:15:38 +04:00
2012-04-26 17:26:58 +04:00
self.content = content
self.expiration = self.get_expiration(expiration)
2020-08-14 16:08:55 +03:00
self.title = bleach.clean(title, strip=True)[:60]
2020-08-14 17:41:45 +03:00
self.btc_tip_address = bleach.clean(btc_tip_address, strip=True)[:50]
2012-04-24 22:15:38 +04:00
if not uuid:
2015-05-10 20:19:02 +03:00
# generate the uuid from the decoded content by hashing it
2019-05-20 12:31:21 +03:00
# and turning it into base64, with some characters strippped
2020-08-11 17:37:03 +03:00
uuid = hashlib.sha1(self.content.encode("utf8"))
2015-05-10 20:19:02 +03:00
uuid = base64.b64encode(uuid.digest()).decode()
2020-08-11 17:37:03 +03:00
uuid = uuid.rstrip("=\n").replace("/", "-")
2015-05-10 20:19:02 +03:00
if uuid_length:
uuid = uuid[:uuid_length]
self.uuid = uuid
2012-04-24 22:15:38 +04:00
2012-04-26 13:38:55 +04:00
def get_expiration(self, expiration):
2012-04-24 22:15:38 +04:00
"""
2015-05-10 20:19:02 +03:00
Return a date at which the Paste will expire
2012-04-24 22:15:38 +04:00
or if it should be destroyed after first reading.
Do not modify the value if it's already a date object or
if it's burn_after_reading
2012-04-24 22:15:38 +04:00
"""
2012-04-24 22:15:38 +04:00
try:
2012-04-26 13:38:55 +04:00
return datetime.now() + timedelta(seconds=self.DURATIONS[expiration])
2012-04-24 22:15:38 +04:00
except KeyError:
2015-05-10 20:19:02 +03:00
return expiration
2012-04-24 22:15:38 +04:00
@classmethod
def build_path(cls, *dirs):
"""
Generic static content path builder. Return a path to
a location in the static content file dir.
"""
return os.path.join(settings.PASTE_FILES_ROOT, *dirs)
2012-04-24 22:15:38 +04:00
@classmethod
def get_path(cls, uuid):
"""
Return the file path of a paste given uuid
"""
return cls.build_path(uuid[:2], uuid[2:4], uuid)
@property
def path(self):
"""
Return the file path for this path. Use get_path().
"""
return self.get_path(self.uuid)
2020-08-11 17:37:03 +03:00
@property
def owner_key(self):
"""
Return a key that gives you admin rights on this paste
"""
payload = (settings.SECRET_KEY + self.uuid).encode("ascii")
return hashlib.sha256(payload).hexdigest()
2012-04-24 22:15:38 +04:00
@classmethod
def load_from_file(cls, path):
"""
Return an instance of the paste object with the content of the
given file.
"""
try:
2015-05-10 20:19:02 +03:00
with open(path) as paste:
uuid = os.path.basename(path)
expiration = next(paste).strip()
content = next(paste).strip()
2020-08-14 16:08:55 +03:00
try:
metadata = json.loads(next(paste).strip())
except (StopIteration, json.decoder.JSONDecodeError):
metadata = {}
2015-05-10 20:19:02 +03:00
if "burn_after_reading" not in expiration:
2020-08-11 17:37:03 +03:00
expiration = datetime.strptime(expiration, "%Y-%m-%d %H:%M:%S.%f")
2012-04-24 22:15:38 +04:00
except StopIteration:
2020-08-14 12:18:59 +03:00
raise TypeError("File %s is malformed" % path)
2012-04-24 22:15:38 +04:00
except (IOError, OSError):
2020-08-14 12:18:59 +03:00
raise ValueError("Can not open paste from file %s" % path)
2020-08-14 16:08:55 +03:00
return Paste(
uuid=uuid,
expiration=expiration,
content=content,
title=" ".join(metadata.get("title", "").split()),
2020-08-14 17:41:45 +03:00
btc_tip_address=" ".join(metadata.get("btc_tip_address", "").split()),
2020-08-14 16:08:55 +03:00
)
2012-04-24 22:15:38 +04:00
@classmethod
def load(cls, uuid):
"""
Return an instance of the paste object with the content of the
file matching this uuid. Use load_from_file() and get_path()
"""
return cls.load_from_file(cls.get_path(uuid))
2012-05-21 19:14:01 +04:00
def increment_counter(self):
"""
2015-05-10 20:19:02 +03:00
Increment pastes counter.
2012-05-21 19:14:01 +04:00
2015-05-10 20:19:02 +03:00
It uses a lock file to prevent multi access to the file.
"""
2012-05-21 19:14:01 +04:00
path = settings.PASTE_FILES_ROOT
2020-08-11 17:37:03 +03:00
counter_file = os.path.join(path, "counter")
2012-05-21 23:24:39 +04:00
2020-08-14 16:08:55 +03:00
with lockfile.LockFile(counter_file):
2020-08-11 17:37:03 +03:00
# Read the value from the counter
try:
with open(counter_file, "r") as fcounter:
counter_value = int(fcounter.read(50)) + 1
except (ValueError, IOError, OSError):
counter_value = 1
2012-05-21 23:24:39 +04:00
2020-08-11 17:37:03 +03:00
# write new value to counter
with open(counter_file, "w") as fcounter:
fcounter.write(str(counter_value))
2012-05-21 19:14:01 +04:00
2012-04-24 22:15:38 +04:00
def save(self):
"""
Save the content of this paste to a file.
"""
head, tail = self.uuid[:2], self.uuid[2:4]
# the static files are saved in project_dir/static/xx/yy/uuid
# xx and yy are generated from the uuid (see get_path())
# we need to check if they are created before writting
2019-05-20 12:31:21 +03:00
# but since we want to prevent too many writes, we create
# an in memory cache that will hold the result of this check for
2012-04-24 22:15:38 +04:00
# each worker. If the dir is not in cache, we check the FS, and
# if the dir is not in there, we create the dir
if head not in self.DIR_CACHE:
2012-04-26 23:04:36 +04:00
self.DIR_CACHE.add(head)
2012-04-24 22:15:38 +04:00
2012-04-26 23:04:36 +04:00
if not os.path.isdir(self.build_path(head)):
os.makedirs(self.build_path(head, tail))
self.DIR_CACHE.add((head, tail))
if (head, tail) not in self.DIR_CACHE:
path = self.build_path(head, tail)
self.DIR_CACHE.add((head, tail))
if not os.path.isdir(path):
os.mkdir(path)
2012-04-26 23:04:36 +04:00
# add a timestamp to burn after reading to allow
# a quick period of time where you can redirect to the page without
# deleting the paste
2015-05-10 20:19:02 +03:00
if "burn_after_reading" == self.expiration:
2020-08-11 17:37:03 +03:00
expiration = self.expiration + "#%s" % datetime.now() # TODO: use UTC dates
2015-05-10 20:19:02 +03:00
else:
2020-08-14 17:41:45 +03:00
expiration = str(self.expiration)
2012-05-12 12:33:01 +04:00
# write the paste
2020-08-11 17:37:03 +03:00
with open(self.path, "w") as f:
f.write(expiration + "\n")
f.write(self.content + "\n")
2020-08-14 17:41:45 +03:00
metadata = {}
2020-08-14 16:08:55 +03:00
if self.title:
2020-08-14 17:41:45 +03:00
metadata["title"] = self.title
if self.btc_tip_address:
metadata["btc_tip_address"] = self.btc_tip_address
f.write(json.dumps(metadata) + "\n")
2012-05-12 12:33:01 +04:00
2012-04-24 22:15:38 +04:00
return self
@classmethod
def get_pastes_count(cls):
"""
Return the number of created pastes.
(must have option DISPLAY_COUNTER enabled for the pastes to be
be counted)
"""
2020-08-11 17:37:03 +03:00
counter_file = os.path.join(settings.PASTE_FILES_ROOT, "counter")
try:
2015-05-10 20:19:02 +03:00
count = int(open(counter_file).read(50))
except (IOError, OSError):
count = 0
2020-08-11 17:37:03 +03:00
return "{0:,}".format(count)
@property
def humanized_expiration(self):
"""
Return the expiration date in a human friendly format.
In 3 minutes, or in 3 days or the 23/01/2102
"""
try:
expiration = self.expiration - datetime.now()
# in_seconds doesn't exist in python 2.6
expiration = expiration.days * 24 * 60 * 60 + expiration.seconds
except TypeError:
return None
if expiration < 60:
2020-08-11 17:37:03 +03:00
return "in %s s" % expiration
if expiration < 60 * 60:
2020-08-11 17:37:03 +03:00
return "in %s m" % int(expiration / 60)
if expiration < 60 * 60 * 24:
2020-08-11 17:37:03 +03:00
return "in %s h" % int(expiration / (60 * 60))
if expiration < 60 * 60 * 24 * 10:
2020-08-11 17:37:03 +03:00
return "in %s days(s)" % int(expiration / (60 * 60 * 24))
2020-08-11 17:37:03 +03:00
return "the %s" % self.expiration.strftime("%m/%d/%Y")
2012-04-24 22:15:38 +04:00
def delete(self):
"""
Delete the paste file.
"""
os.remove(self.path)
@classmethod
2020-08-14 12:18:59 +03:00
def iter_all(cls, on_error=lambda e: e):
for p in settings.PASTE_FILES_ROOT.rglob("*"):
if p.is_file() and "counter" not in str(p):
2020-08-14 12:18:59 +03:00
try:
yield Paste.load_from_file(p)
except (TypeError, ValueError) as e:
on_error(e)
@property
def has_expired(self):
try:
return self.expiration < datetime.now()
except TypeError:
return False