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
@@ -5,13 +5,17 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import get_settings
|
||||
from app.languages import LANGUAGES
|
||||
from app.models import Clip, ClipKind, utcnow
|
||||
from app.models import Clip, ClipKind, StoredFile, utcnow
|
||||
from app.services.files import remove_from_disk
|
||||
from app.services.projects import require_project
|
||||
from app.services.users import UserError
|
||||
|
||||
# Changing only these fields does not count as an edit (no new updated_at)
|
||||
_META_FIELDS = frozenset({"pinned", "project_id"})
|
||||
|
||||
# Fixed for file clips: content is the file name
|
||||
_CONTENT_FIELDS = frozenset({"kind", "content", "language"})
|
||||
|
||||
# Filter value for clips without a project
|
||||
NO_PROJECT = "none"
|
||||
SEARCH_MAX_LENGTH = 200
|
||||
@@ -36,7 +40,7 @@ def _normalize(kind: ClipKind, title: str | None, content: str, language: str |
|
||||
title = (title or "").strip() or None
|
||||
if title is not None and len(title) > 200:
|
||||
raise UserError("Der Titel ist zu lang (höchstens 200 Zeichen).")
|
||||
if kind == ClipKind.TEXT:
|
||||
if kind != ClipKind.CODE:
|
||||
language = None
|
||||
else:
|
||||
language = language or "plaintext"
|
||||
@@ -100,6 +104,8 @@ def create_clip(
|
||||
pinned: bool = False,
|
||||
project_id: int | None = None,
|
||||
) -> Clip:
|
||||
if kind == ClipKind.FILE:
|
||||
raise UserError("Dateien werden über den Upload angelegt.")
|
||||
title, language = _normalize(kind, title, content, language)
|
||||
if project_id is not None:
|
||||
require_project(db, user_id, project_id)
|
||||
@@ -128,6 +134,10 @@ def update_clip(
|
||||
# Early check so a stale client gets the conflict, not a follow-up validation error
|
||||
if expected_version is not None and clip.version != expected_version:
|
||||
raise ClipConflict(clip)
|
||||
if clip.kind == ClipKind.FILE and set(changes) & _CONTENT_FIELDS:
|
||||
raise UserError("Bei Dateien lassen sich nur Titel, Projekt und Anheften ändern.")
|
||||
if changes.get("kind") == ClipKind.FILE and clip.kind != ClipKind.FILE:
|
||||
raise UserError("Ein Clip lässt sich nicht in eine Datei umwandeln.")
|
||||
kind = changes.get("kind", clip.kind)
|
||||
title = changes.get("title", clip.title)
|
||||
content = changes.get("content", clip.content)
|
||||
@@ -164,6 +174,13 @@ def update_clip(
|
||||
|
||||
|
||||
def delete_clip(db: Session, user_id: int, clip_id: int) -> bool:
|
||||
storage_name = db.scalar(
|
||||
select(StoredFile.storage_name).where(
|
||||
StoredFile.clip_id == clip_id, StoredFile.user_id == user_id
|
||||
)
|
||||
)
|
||||
result = db.execute(delete(Clip).where(Clip.id == clip_id, Clip.user_id == user_id))
|
||||
db.commit()
|
||||
if storage_name is not None:
|
||||
remove_from_disk(user_id, storage_name)
|
||||
return result.rowcount > 0
|
||||
Reference in new issue
Block a user