2012-04-24 22:15:38 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
2012-05-22 16:39:34 +04:00
|
|
|
import locale
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2012-05-14 19:17:49 +04:00
|
|
|
from utils import settings
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Paste(object):
|
|
|
|
"""
|
|
|
|
A paste objet to deal with the file opening/parsing/saving and the
|
|
|
|
calculation of the expiration date.
|
|
|
|
"""
|
|
|
|
|
|
|
|
DIR_CACHE = set()
|
|
|
|
|
|
|
|
DURATIONS = {
|
|
|
|
u'1_day': 24 * 3600,
|
|
|
|
u'1_month': 30 * 24 * 3600,
|
|
|
|
u'never': 365 * 24 * 3600 * 100,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-29 22:17:37 +04: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
|
2012-04-27 01:41:20 +04:00
|
|
|
self.expiration = expiration
|
|
|
|
|
2012-04-26 17:26:58 +04:00
|
|
|
if isinstance(self.content, unicode):
|
2012-05-12 12:33:01 +04:00
|
|
|
self.content = self.content.encode('utf8')
|
2012-04-26 17:26:58 +04:00
|
|
|
|
2012-04-28 15:10:29 +04:00
|
|
|
self.expiration = self.get_expiration(expiration)
|
2012-04-24 22:15:38 +04:00
|
|
|
|
2013-04-29 22:17:37 +04:00
|
|
|
if not uuid:
|
|
|
|
uuid = hashlib.sha1(self.content)\
|
|
|
|
.digest().encode('base64').rstrip('=\n').replace('/', '-')
|
|
|
|
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
|
|
|
"""
|
|
|
|
Return a tuple with the date at which the Paste will expire
|
|
|
|
or if it should be destroyed after first reading.
|
2012-04-28 15:10:29 +04:00
|
|
|
|
|
|
|
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-28 15:10:29 +04:00
|
|
|
|
|
|
|
if (isinstance(expiration, datetime) or
|
|
|
|
'burn_after_reading' in str(expiration)):
|
|
|
|
return expiration
|
|
|
|
|
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:
|
|
|
|
return u'burn_after_reading'
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def build_path(cls, *dirs):
|
|
|
|
"""
|
|
|
|
Generic static content path builder. Return a path to
|
|
|
|
a location in the static content file dir.
|
|
|
|
"""
|
2012-04-27 01:41:20 +04:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_from_file(cls, path):
|
|
|
|
"""
|
|
|
|
Return an instance of the paste object with the content of the
|
|
|
|
given file.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
paste = open(path)
|
|
|
|
uuid = os.path.basename(path)
|
2012-04-26 17:26:58 +04:00
|
|
|
expiration = paste.next().strip()
|
|
|
|
content = paste.next().strip()
|
2012-04-27 01:41:20 +04:00
|
|
|
if "burn_after_reading" not in str(expiration):
|
2012-05-12 12:33:01 +04:00
|
|
|
expiration = datetime.strptime(expiration, '%Y-%m-%d %H:%M:%S.%f')
|
2012-04-24 22:15:38 +04:00
|
|
|
|
|
|
|
except StopIteration:
|
|
|
|
raise TypeError(u'File %s is malformed' % path)
|
|
|
|
except (IOError, OSError):
|
|
|
|
raise ValueError(u'Can not open paste from file %s' % path)
|
2012-04-27 01:41:20 +04:00
|
|
|
|
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):
|
|
|
|
"""
|
2012-05-22 16:39:34 +04:00
|
|
|
Increment pastes counter
|
2012-05-21 19:14:01 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
# simple counter incrementation
|
|
|
|
# using lock file to prevent multi access to the file
|
|
|
|
# could be improved.
|
|
|
|
|
|
|
|
|
|
|
|
path = settings.PASTE_FILES_ROOT
|
2012-05-21 23:24:39 +04:00
|
|
|
counter_file = os.path.join(path, 'counter')
|
|
|
|
lock_file = os.path.join(path, 'counter.lock')
|
|
|
|
|
|
|
|
if not os.path.isfile(lock_file):
|
|
|
|
try:
|
|
|
|
#make lock file
|
|
|
|
flock = open(lock_file, "w")
|
2012-05-22 16:39:34 +04:00
|
|
|
flock.write('lock')
|
2012-05-21 23:24:39 +04:00
|
|
|
flock.close()
|
|
|
|
|
|
|
|
# init counter (first time)
|
|
|
|
if not os.path.isfile(counter_file):
|
|
|
|
fcounter = open(counter_file, "w")
|
|
|
|
fcounter.write('1')
|
|
|
|
fcounter.close()
|
|
|
|
|
|
|
|
# get counter value
|
|
|
|
fcounter = open(counter_file, "r")
|
|
|
|
counter_value = fcounter.read(50)
|
|
|
|
fcounter.close()
|
|
|
|
|
|
|
|
try:
|
|
|
|
counter_value = long(counter_value) + 1
|
|
|
|
except ValueError:
|
|
|
|
counter_value = 1
|
|
|
|
|
|
|
|
# write new value to counter
|
|
|
|
fcounter = open(counter_file, "w")
|
|
|
|
fcounter.write(str(counter_value))
|
|
|
|
fcounter.close()
|
|
|
|
|
|
|
|
#remove lock file
|
|
|
|
os.remove(lock_file)
|
2012-05-22 19:40:57 +04:00
|
|
|
finally:
|
2012-05-21 23:24:39 +04:00
|
|
|
if os.path.isfile(lock_file):
|
|
|
|
#remove lock file
|
|
|
|
os.remove(lock_file)
|
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
|
|
|
|
# but since we want to prevent to many writes, we create
|
|
|
|
# an in memory cache that will hold the result of this check fo
|
|
|
|
# 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))
|
|
|
|
|
2012-05-21 13:04:28 +04:00
|
|
|
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
|
|
|
|
2012-04-27 01:41:20 +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
|
|
|
|
if self.expiration == "burn_after_reading":
|
|
|
|
self.expiration = self.expiration + '#%s' % datetime.now()
|
|
|
|
|
2012-05-12 12:33:01 +04:00
|
|
|
# write the paste
|
2012-05-01 17:20:10 +04:00
|
|
|
with open(self.path, 'w') as f:
|
|
|
|
f.write(unicode(self.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
|
|
|
|
|
|
|
|
|
2012-05-22 16:39:34 +04:00
|
|
|
@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)
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
locale.setlocale(locale.LC_ALL, 'en_US')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
counter_file = os.path.join(settings.PASTE_FILES_ROOT, 'counter')
|
|
|
|
try:
|
|
|
|
count = long(open(counter_file).read(50))
|
|
|
|
except (IOError, OSError):
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
return locale.format("%d", long(count), grouping=True)
|
|
|
|
|
|
|
|
|
2012-05-22 21:58:23 +04:00
|
|
|
@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:
|
2012-05-22 22:11:38 +04:00
|
|
|
expiration = self.expiration - datetime.now()
|
|
|
|
# in_seconds doesn't exist in python 2.6
|
|
|
|
expiration = expiration.days * 24 * 60 * 60 + expiration.seconds
|
|
|
|
|
2012-05-22 21:58:23 +04:00
|
|
|
except TypeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if expiration < 60:
|
|
|
|
return 'in %s s' % expiration
|
|
|
|
|
|
|
|
if expiration < 60 * 60:
|
|
|
|
return 'in %s m' % int(expiration / 60)
|
|
|
|
|
|
|
|
if expiration < 60 * 60 * 24:
|
|
|
|
return 'in %s h' % int(expiration / (60 * 60))
|
|
|
|
|
|
|
|
if expiration < 60 * 60 * 24 * 10:
|
|
|
|
return 'in %s days(s)' % int(expiration / (60 * 60 * 24))
|
|
|
|
|
|
|
|
return 'the %s' % self.expiration.strftime('%m/%d/%Y')
|
|
|
|
|
2012-05-22 16:39:34 +04:00
|
|
|
|
2012-04-24 22:15:38 +04:00
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Delete the paste file.
|
|
|
|
"""
|
2012-04-27 01:41:20 +04:00
|
|
|
os.remove(self.path)
|
2014-07-07 03:23:56 +04:00
|
|
|
|
|
|
|
def is_alive(self):
|
|
|
|
"""
|
|
|
|
Return True if the paste is alive.
|
|
|
|
False if it may be deleted
|
|
|
|
"""
|
|
|
|
now = datetime.now()
|
|
|
|
# keep alive false by default
|
|
|
|
keep_alive = False
|
|
|
|
# Delete the paste if it expired:
|
|
|
|
if 'burn_after_reading' in str(self.expiration):
|
|
|
|
# burn_after_reading contains the paste creation date
|
|
|
|
# if this read appends 10 seconds after the creation date
|
|
|
|
# we don't delete the paste because it means it's the redirection
|
|
|
|
# to the paste that happens during the paste creation
|
|
|
|
try:
|
|
|
|
keep_alive = self.expiration.split('#')[1]
|
|
|
|
keep_alive = datetime.strptime(keep_alive,
|
|
|
|
'%Y-%m-%d %H:%M:%S.%f')
|
|
|
|
keep_alive = now < keep_alive + timedelta(seconds=10)
|
|
|
|
except IndexError:
|
|
|
|
keep_alive = False
|
|
|
|
else:
|
|
|
|
keep_alive = now < self.expiration
|
|
|
|
|
|
|
|
return keep_alive
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def purge(self, path=settings.PASTE_FILES_ROOT):
|
|
|
|
"""
|
|
|
|
Purge the content folder
|
|
|
|
Return the number of paste deleted.
|
|
|
|
"""
|
|
|
|
# we don't want to read 'counter' files
|
|
|
|
ignore_files = ['counter', ]
|
|
|
|
# keep a count on purged pastes
|
|
|
|
deleted_paste = 0
|
|
|
|
if os.path.exists(path):
|
|
|
|
for folder, _ , files in os.walk(path):
|
|
|
|
for fname in files:
|
|
|
|
if fname in ignore_files:
|
|
|
|
continue
|
|
|
|
if Paste._purge_file(os.path.join(folder, fname)):
|
|
|
|
deleted_paste += 1
|
|
|
|
return deleted_paste
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _purge_file(self, fname):
|
|
|
|
"""
|
|
|
|
Purges the given file if expired.
|
|
|
|
"""
|
|
|
|
paste = Paste.load_from_file(fname)
|
|
|
|
# Burn after reading will always returns paste.is_alive() == False
|
|
|
|
# We don't want to kill them now
|
|
|
|
is_burn_after_reading = 'burn_after_reading' in str(paste.expiration)
|
|
|
|
if not is_burn_after_reading and not paste.is_alive():
|
|
|
|
paste.delete()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|