2026-05-12 15:07:34 +02:00
|
|
|
import logging
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
|
from fastapi.responses import JSONResponse
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
from devplacepy.database import get_table, get_int_setting
|
2026-05-23 08:34:13 +02:00
|
|
|
from devplacepy.utils import require_user_api
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
from devplacepy.attachments import store_attachment, delete_attachment as _delete_attachment, is_extension_allowed
|
2026-05-12 15:07:34 +02:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/upload")
|
|
|
|
|
async def upload_file(request: Request):
|
2026-05-23 08:34:13 +02:00
|
|
|
user = require_user_api(request)
|
2026-05-12 15:07:34 +02:00
|
|
|
form = await request.form()
|
|
|
|
|
file = form.get("file")
|
|
|
|
|
|
|
|
|
|
if not file or not hasattr(file, "filename") or not file.filename:
|
|
|
|
|
return JSONResponse({"error": "No file provided"}, status_code=400)
|
|
|
|
|
|
2026-05-23 01:50:31 +02:00
|
|
|
ext = Path(file.filename).suffix.lower()
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
if not is_extension_allowed(ext):
|
2026-05-13 21:17:57 +02:00
|
|
|
return JSONResponse({"error": f"File type '{ext}' not allowed"}, status_code=415)
|
2026-05-12 15:07:34 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
content = await file.read()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f"Failed to read uploaded file: {e}")
|
|
|
|
|
return JSONResponse({"error": "Failed to read file"}, status_code=400)
|
|
|
|
|
|
2026-05-13 21:17:57 +02:00
|
|
|
result = store_attachment(content, file.filename, user["uid"])
|
|
|
|
|
if result is None:
|
2026-05-23 06:55:11 +02:00
|
|
|
max_size_mb = get_int_setting("max_upload_size_mb", 10)
|
2026-05-12 15:07:34 +02:00
|
|
|
return JSONResponse({"error": f"File exceeds {max_size_mb}MB limit"}, status_code=413)
|
|
|
|
|
|
2026-05-13 21:17:57 +02:00
|
|
|
logger.info(f"File uploaded: {file.filename} ({len(content)} bytes) -> {result['url']}")
|
|
|
|
|
return JSONResponse(result, status_code=201)
|
2026-05-12 15:07:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/delete/{attachment_uid}")
|
2026-05-13 21:17:57 +02:00
|
|
|
async def delete_attachment_route(request: Request, attachment_uid: str):
|
2026-05-23 08:34:13 +02:00
|
|
|
user = require_user_api(request)
|
2026-05-13 21:17:57 +02:00
|
|
|
att = get_table("attachments").find_one(uid=attachment_uid)
|
2026-05-12 15:07:34 +02:00
|
|
|
if not att:
|
|
|
|
|
return JSONResponse({"error": "Attachment not found"}, status_code=404)
|
2026-05-13 21:17:57 +02:00
|
|
|
if att.get("user_uid") and att["user_uid"] != user["uid"]:
|
|
|
|
|
return JSONResponse({"error": "Not authorized"}, status_code=403)
|
|
|
|
|
_delete_attachment(attachment_uid)
|
2026-05-12 15:07:34 +02:00
|
|
|
logger.info(f"Attachment {attachment_uid} deleted by {user['username']}")
|
|
|
|
|
return JSONResponse({"status": "deleted"})
|