2020-07-22 17:48:01 +02:00
|
|
|
from app import db
|
|
|
|
from hashlib import sha256
|
|
|
|
import os
|
|
|
|
|
2020-07-23 10:01:57 +02:00
|
|
|
class Attachment(db.Model):
|
|
|
|
__tablename__ = 'attachment'
|
2020-07-22 17:48:01 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
# Original name of the file
|
|
|
|
name = db.Column(db.Unicode(64))
|
|
|
|
|
|
|
|
# Hash of the value
|
|
|
|
hashed = db.Column(db.Unicode(64))
|
|
|
|
|
|
|
|
# The comment linked with
|
|
|
|
comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=False)
|
|
|
|
comment = db.relationship('Comment', backref=backref('attachments', lazy='dynamic'))
|
|
|
|
|
|
|
|
# The size of the file
|
|
|
|
size = db.Column(db.Integer)
|
|
|
|
|
|
|
|
def __init__(self, file, comment):
|
|
|
|
self.name = file.filename
|
|
|
|
self.size = os.stat(file).st_size
|
2020-07-23 10:01:57 +02:00
|
|
|
self.hashed = self.hash_file(file)
|
2020-07-22 17:48:01 +02:00
|
|
|
self.comment = comment
|
|
|
|
|
|
|
|
def hash_file(file):
|
|
|
|
with open(file,"rb") as f:
|
|
|
|
bytes = f.read() # read entire file as bytes
|
2020-07-22 17:51:23 +02:00
|
|
|
hashed = sha256(bytes).hexdigest()
|
2020-07-22 17:48:01 +02:00
|
|
|
return hashed
|