From 0323d71d750ca69653a9934d344547a5a4222c48 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 30 Aug 2013 22:05:54 +0200 Subject: Added ip and accessed to the 'files' table. --- db.py | 5 ++++- fbin.py | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/db.py b/db.py index 064f8e5..8458704 100644 --- a/db.py +++ b/db.py @@ -31,13 +31,16 @@ class File(Base): filename = Column(String) date = Column(DateTime) user_id = Column(Integer, ForeignKey('users.id'), nullable = True) + ip = Column(String) + accessed = Column(DateTime) - def __init__(self, hash, file_hash, filename, date, user_id = None): + def __init__(self, hash, file_hash, filename, date, user_id = None, ip = None): self.hash = hash self.file_hash = file_hash self.filename = filename self.date = date self.user_id = user_id + self.ip = ip @staticmethod def pretty_size(size): diff --git a/fbin.py b/fbin.py index d3d84c8..f9ca772 100755 --- a/fbin.py +++ b/fbin.py @@ -81,16 +81,23 @@ class Application(object): finally: session.close() - def get_file(self, hash): + def get_file(self, hash, update_accessed = False): session = db.Session() try: - return session.query(db.File).filter(db.File.hash == hash).one() + f = session.query(db.File).filter(db.File.hash == hash).one() + if update_accessed: + f.accessed = datetime.datetime.utcnow() + session.add(f) + session.commit() + # Refresh after field update. + session.refresh(f) + return f except db.NoResultFound: return None finally: session.close() - def add_file(self, path, filename, file_hash, user = None): + def add_file(self, path, filename, file_hash, user = None, ip = None): hash = ''.join(random.choice(base62_alphabet) for x in xrange(5)) new_path = os.path.join(settings.file_directory, hash + os.path.splitext(filename)[1]) os.rename(path, new_path) @@ -99,7 +106,7 @@ class Application(object): session = db.Session() try: - file = db.File(hash, file_hash, filename, datetime.datetime.utcnow(), user.id if user else None) + file = db.File(hash, file_hash, filename, datetime.datetime.utcnow(), user.id if user else None, ip) session.add(file) session.commit() finally: @@ -173,7 +180,7 @@ class Application(object): hash = path[1] if '.' in hash: hash = hash.split('.')[0] - file = self.get_file(hash) + file = self.get_file(hash, True) filename = file.get_path() if filename == None: start_response('404 Not Found', [('Content-Type', 'text/html')]) @@ -266,7 +273,7 @@ class Application(object): hash = f.hash else: # temp.name will be moved to the destination filename - hash = self.add_file(temp.name, filename, file_hash, user) + hash = self.add_file(temp.name, filename, file_hash, user, environ['REMOTE_ADDR']) # This avoids silly "not bound to a Session" errors when trying to use a newly added file object. f = self.get_file(hash) -- cgit v1.2.3