1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00
0bin/zerobin/paste.py

231 lines
7.2 KiB
Python
Raw Normal View History

2015-05-10 20:19:02 +03:00
# coding: utf-8
from __future__ import unicode_literals, absolute_import
2012-04-24 22:15:38 +04:00
import os
import hashlib
2015-05-10 20:19:02 +03:00
import base64
import lockfile
2012-04-24 22:15:38 +04:00
from datetime import datetime, timedelta
2015-05-10 20:19:02 +03:00
from zerobin.utils import settings, to_ascii, as_unicode, safe_open as open
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.
"""
DIR_CACHE = set()
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-11 17:37:03 +03:00
def __init__(self, uuid=None, uuid_length=None, content=None, expiration=None):
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)
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()
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-11 17:37:03 +03:00
raise TypeError(to_ascii("File %s is malformed" % path))
2012-04-24 22:15:38 +04:00
except (IOError, OSError):
2020-08-11 17:37:03 +03:00
raise ValueError(to_ascii("Can not open paste from file %s" % path))
2012-05-14 19:17:49 +04:00
return Paste(uuid=uuid, expiration=expiration, content=content)
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")
lock = lockfile.LockFile(counter_file)
2012-05-21 23:24:39 +04:00
with lock:
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:
expiration = as_unicode(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")
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)