30 lines
885 B
Python
30 lines
885 B
Python
from sqlalchemy import event
|
|
from pathlib import Path
|
|
|
|
from ..database import db
|
|
from ..database.types import ModelSerializeMixin, Serial
|
|
|
|
|
|
class Image(db.Model, ModelSerializeMixin):
|
|
__tablename__ = "image"
|
|
id: int = db.Column(Serial, primary_key=True)
|
|
filename_: str = db.Column("filename", db.String(255), nullable=False)
|
|
mimetype_: str = db.Column("mimetype", db.String(127), nullable=False)
|
|
thumbnail_: str = db.Column("thumbnail", db.String(255))
|
|
path_: str = db.Column("path", db.String(255))
|
|
|
|
def open(self):
|
|
return open(self.path_, "rb")
|
|
|
|
|
|
@event.listens_for(Image, "before_delete")
|
|
def clear_file(mapper, connection, target: Image):
|
|
if target.path_:
|
|
p = Path(target.path_)
|
|
if p.exists():
|
|
p.unlink()
|
|
if target.thumbnail_:
|
|
p = Path(target.thumbnail_)
|
|
if p.exists():
|
|
p.unlink()
|