feat: dateiaustausch im backend
Dateien als Clip-Art "file": gestreamter Upload mit Größengrenze, Kontingent pro Benutzer und Mindestreserve an freiem Speicher. Download immer als Anhang mit sandbox-CSP, Vorschau nur für geprüfte JPEG/PNG/GIF/WebP. Ablauf nach 1/7/30 Tagen oder nie; der Aufräumjob löscht abgelaufene und verwaiste Dateien. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ae334d2248
commit
52a8fd019f
12 files changed
+1027
-11
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from app.models.base import Base, UTCDateTime, utcnow
|
||||
from app.models.clip import Clip, ClipKind
|
||||
from app.models.file import StoredFile
|
||||
from app.models.login_attempt import LoginAttempt
|
||||
from app.models.project import PROJECT_COLORS, Project
|
||||
from app.models.session import UserSession
|
||||
@@ -13,6 +14,7 @@ __all__ = [
|
||||
"PROJECT_COLORS",
|
||||
"Project",
|
||||
"Role",
|
||||
"StoredFile",
|
||||
"UTCDateTime",
|
||||
"User",
|
||||
"UserSession",
|
||||
|
||||
@@ -2,16 +2,22 @@ from __future__ import annotations
|
||||
|
||||
import enum
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Enum, ForeignKey, Index, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base, UTCDateTime, utcnow
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.file import StoredFile
|
||||
|
||||
|
||||
class ClipKind(enum.StrEnum):
|
||||
TEXT = "text"
|
||||
CODE = "code"
|
||||
# Uploaded file; content holds the file name so search finds it
|
||||
FILE = "file"
|
||||
|
||||
|
||||
class Clip(Base):
|
||||
@@ -37,3 +43,7 @@ class Clip(Base):
|
||||
version: Mapped[int] = mapped_column(default=1)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
|
||||
|
||||
file: Mapped[StoredFile | None] = relationship(
|
||||
back_populates="clip", lazy="selectin", passive_deletes=True
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import BigInteger, ForeignKey, Index, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base, UTCDateTime, utcnow
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.clip import Clip
|
||||
|
||||
|
||||
class StoredFile(Base):
|
||||
"""An uploaded file; belongs to exactly one clip of kind "file"."""
|
||||
|
||||
__tablename__ = "files"
|
||||
__table_args__ = (
|
||||
Index("ix_files_user", "user_id"),
|
||||
Index("ix_files_expires_at", "expires_at"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
clip_id: Mapped[int] = mapped_column(ForeignKey("clips.id", ondelete="CASCADE"), unique=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
||||
# Random name on disk; the original name never touches the file system
|
||||
storage_name: Mapped[str] = mapped_column(String(64), unique=True)
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
content_type: Mapped[str] = mapped_column(String(127))
|
||||
size: Mapped[int] = mapped_column(BigInteger)
|
||||
sha256: Mapped[str] = mapped_column(String(64))
|
||||
# Only set when the content really is a JPEG/PNG/GIF/WebP image (checked on upload)
|
||||
previewable: Mapped[bool] = mapped_column(default=False)
|
||||
expires_at: Mapped[datetime | None] = mapped_column(UTCDateTime)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)
|
||||
|
||||
clip: Mapped[Clip] = relationship(back_populates="file")
|
||||
Reference in new issue
Block a user