partitioncloud-server/partitioncloud/modules/classes/attachment.py

47 lines
1.1 KiB
Python
Raw Normal View History

import os
from ..db import get_db
class Attachment():
def __init__(self, uuid=None, data=None):
db = get_db()
if uuid is not None:
self.uuid = uuid
data = db.execute(
"""
SELECT * FROM attachments
WHERE uuid = ?
""",
(self.uuid,)
).fetchone()
if data is None:
raise LookupError
elif data is not None:
self.uuid = data["uuid"]
else:
raise LookupError
self.name = data["name"]
self.user_id = data["user_id"]
self.filetype = data["filetype"]
self.partition_uuid = data["partition_uuid"]
def delete(self, instance_path):
db = get_db()
db.execute(
"""
DELETE FROM attachments
WHERE uuid = ?
""",
(self.uuid,)
)
db.commit()
2023-12-15 11:36:34 +01:00
os.remove(f"{instance_path}/attachments/{self.uuid}.{self.filetype}")
def __repr__(self):
2023-12-15 11:36:34 +01:00
return f"{self.name}.{self.filetype}"