diff options
-rw-r--r-- | db.py | 5 | ||||
-rwxr-xr-x | fbin.py | 19 |
2 files changed, 17 insertions, 7 deletions
@@ -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): @@ -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) |