summaryrefslogtreecommitdiff
path: root/animism-align/cli/app/sql/models/upload.py
diff options
context:
space:
mode:
Diffstat (limited to 'animism-align/cli/app/sql/models/upload.py')
-rw-r--r--animism-align/cli/app/sql/models/upload.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/animism-align/cli/app/sql/models/upload.py b/animism-align/cli/app/sql/models/upload.py
new file mode 100644
index 0000000..5863b07
--- /dev/null
+++ b/animism-align/cli/app/sql/models/upload.py
@@ -0,0 +1,44 @@
+from sqlalchemy import create_engine, Table, Column, String, Integer, DateTime
+import sqlalchemy.sql.functions as func
+from sqlalchemy_utc import UtcDateTime, utcnow
+from wtforms_alchemy import ModelForm
+
+from app.sql.common import db, Base, Session
+
+from app.utils.file_utils import sha256_tree
+from app.settings import app_cfg
+
+from os.path import join
+
+class Upload(Base):
+ """Table for storing references to various media"""
+ __tablename__ = 'upload'
+ id = Column(Integer, primary_key=True)
+ sha256 = Column(String(256), nullable=False)
+ fn = Column(String(256), nullable=False)
+ ext = Column(String(4, convert_unicode=True), nullable=False)
+ username = Column(String(16, convert_unicode=True), nullable=False)
+ created_at = Column(UtcDateTime(), default=utcnow())
+
+ def toJSON(self):
+ return {
+ 'id': self.id,
+ 'sha256': self.sha256,
+ 'fn': self.fn,
+ 'ext': self.ext,
+ 'username': self.username,
+ 'url': self.url(),
+ 'created_at': self.created_at,
+ }
+
+ def filename(self):
+ return "{}{}".format(self.fn)
+
+ def filepath(self):
+ return join(app_cfg.DIR_UPLOADS, sha256_tree(self.sha256))
+
+ def fullpath(self):
+ return join(self.filepath(), self.filename())
+
+ def url(self):
+ return join(app_cfg.URL_UPLOADS, sha256_tree(self.sha256), self.filename())