summaryrefslogtreecommitdiff
path: root/db.py
diff options
context:
space:
mode:
Diffstat (limited to 'db.py')
-rw-r--r--db.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/db.py b/db.py
index 9c49155..3e16cd9 100644
--- a/db.py
+++ b/db.py
@@ -4,7 +4,7 @@ from sqlalchemy.orm import sessionmaker, relation, backref
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql import and_
-import settings, os
+import settings, os, mimetypes
engine = create_engine(settings.db_path)
@@ -56,5 +56,15 @@ class File(Base):
hash = self.hash, filename = self.filename, ext = os.path.splitext(self.filename)[1],
size = self.pretty_size(os.path.getsize(self.get_path())), date = self.date.strftime('%Y-%m-%d %H:%M:%S UTC'))
+ def get_mime_type(self):
+ return mimetypes.guess_type(self.filename, strict = False)[0] or 'application/octet-stream'
+
+ def is_image(self):
+ return self.get_mime_type().startswith('image')
+
+ def image_html(self):
+ return u'<a href="/f/{hash}{ext}" title="{filename}"><img src="/t/{hash}" alt="{filename}" /></a>'.format(
+ hash = self.hash, filename = self.filename, ext = os.path.splitext(self.filename)[1])
+
Base.metadata.create_all()
Session = sessionmaker(bind = engine, autoflush = True, autocommit = False)