summaryrefslogtreecommitdiff
path: root/fbin/db.py
blob: 9493f65a70f9d2b1287109ac549bc0f811d46194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import datetime
import mimetypes
import os

# FIXME: reimported from elsewhere
from sqlalchemy.orm.exc import NoResultFound  # noqa: F401
from sqlalchemy.exc import IntegrityError  # noqa: F401
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, index=True)
    jab_id = db.Column(db.String(24), unique=True, index=True)
    api_key_date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    files = db.relation('File', backref='user', order_by='File.date.desc()')

    def __init__(self, username, jab_id):
        self.username = username
        self.jab_id = jab_id


class UserSession(db.Model):
    __tablename__ = 'sessions'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True)
    access_token = db.Column(db.String)
    refresh_token = db.Column(db.String)
    updated = db.Column(db.DateTime)

    def __init__(self, user_id, access_token, refresh_token):
        self.user_id = user_id
        self.access_token = access_token
        self.refresh_token = refresh_token


class File(db.Model):
    __tablename__ = 'files'

    id = db.Column(db.Integer, primary_key=True)
    hash = db.Column(db.String, unique=True, index=True)
    filename = db.Column(db.String)
    size = db.Column(db.BigInteger)
    date = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
    ip = db.Column(db.String)
    accessed = db.Column(db.DateTime)
    scanned = db.Column(db.Boolean, nullable=False, default=False)
    blocked_reason = db.Column(db.JSON)

    def __init__(self, hash, filename, size, date, user_id=None, ip=None):
        self.hash = hash
        self.filename = filename
        self.size = size
        self.date = date
        self.user_id = user_id
        self.ip = ip

    @staticmethod
    def pretty_size(size):
        if size is None:
            return 'N/A'
        suffixes = (('TiB', 2**40), ('GiB', 2**30), ('MiB', 2**20), ('KiB', 2**10))
        for suf, threshold in suffixes:
            if size >= threshold:
                return '{:.2f} {}'.format(size / threshold, suf)
            else:
                continue
        return '{} B'.format(size)

    @property
    def formatted_size(self):
        return self.pretty_size(self.size)

    @property
    def formatted_date(self):
        return 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 is_video(self):
        return self.get_mime_type().startswith('video')

    @property
    def ext(self):
        return os.path.splitext(self.filename)[1]