Milestone 2.
This commit is contained in:
32
app/routes/attachments.py
Normal file
32
app/routes/attachments.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from flask import Blueprint, abort, current_app, send_from_directory
|
||||
|
||||
from app.services.vault import Vault
|
||||
|
||||
bp = Blueprint("attachments", __name__, url_prefix="/attachments")
|
||||
|
||||
|
||||
@bp.get("/<path:subpath>")
|
||||
def serve_attachment(subpath: str):
|
||||
# Resolve within the vault attachments directory
|
||||
vault_path = current_app.config.get("KB_VAULT_PATH")
|
||||
if not vault_path:
|
||||
abort(404)
|
||||
|
||||
v = Vault(vault_path)
|
||||
attachments_dir = Path(v.paths.attachments).resolve()
|
||||
requested = (attachments_dir / subpath).resolve()
|
||||
|
||||
# Prevent path traversal
|
||||
try:
|
||||
requested.relative_to(attachments_dir)
|
||||
except Exception:
|
||||
abort(403)
|
||||
|
||||
if not requested.exists() or not requested.is_file():
|
||||
abort(404)
|
||||
|
||||
return send_from_directory(attachments_dir, os.path.relpath(str(requested), str(attachments_dir)))
|
@@ -1,8 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import Blueprint, abort, redirect, render_template, request, url_for
|
||||
from flask import Blueprint, abort, redirect, render_template, request, url_for, jsonify, current_app
|
||||
|
||||
from app.services.notes_fs import create_note, list_notes, load_note_by_id, update_note_body
|
||||
from app.services.renderer import render_markdown
|
||||
from app.services.vault import Vault
|
||||
import frontmatter
|
||||
import os
|
||||
|
||||
bp = Blueprint("notes", __name__, url_prefix="/notes")
|
||||
|
||||
@@ -37,8 +41,17 @@ def notes_view(note_id: str):
|
||||
note = load_note_by_id(note_id)
|
||||
if not note:
|
||||
abort(404)
|
||||
# M2 will render Markdown; for now show raw body and metadata
|
||||
return render_template("notes/view.html", note=note)
|
||||
|
||||
all_notes = list_notes()
|
||||
rendered = render_markdown(note.body or "", all_notes=all_notes)
|
||||
|
||||
return render_template(
|
||||
"notes/view.html",
|
||||
note=note,
|
||||
rendered_html=rendered["html"],
|
||||
unresolved_wikilinks=rendered["unresolved_wikilinks"],
|
||||
outbound_note_ids=rendered["outbound_note_ids"],
|
||||
)
|
||||
|
||||
|
||||
@bp.post("/<note_id>/body")
|
||||
@@ -47,4 +60,60 @@ def notes_update_body(note_id: str):
|
||||
note = update_note_body(note_id, new_body)
|
||||
if not note:
|
||||
abort(404)
|
||||
return redirect(url_for("notes.notes_view", note_id=note.id))
|
||||
return redirect(url_for("notes.notes_view", note_id=note.id))
|
||||
|
||||
|
||||
@bp.get("/_debug/ids")
|
||||
def notes_debug_ids():
|
||||
notes = list_notes()
|
||||
return jsonify(
|
||||
notes=[
|
||||
{"id": n.id, "title": n.title, "path": n.rel_path}
|
||||
for n in notes]
|
||||
)
|
||||
|
||||
|
||||
@bp.get("/_debug/scan")
|
||||
def notes_debug_scan():
|
||||
vault_path = current_app.config.get("KB_VAULT_PATH")
|
||||
v = Vault(vault_path)
|
||||
v.ensure_structure()
|
||||
notes_dir = v.paths.notes
|
||||
|
||||
exists = os.path.isdir(notes_dir)
|
||||
ls_notes = []
|
||||
if exists:
|
||||
try:
|
||||
ls_notes = sorted(os.listdir(notes_dir))
|
||||
except Exception as e:
|
||||
ls_notes = [f"<error listing dir: {e}>"]
|
||||
|
||||
discovered = list(v.iter_markdown_files())
|
||||
|
||||
probe = []
|
||||
for p in discovered:
|
||||
try:
|
||||
with open(p, "r", encoding="utf-8", errors="replace") as f:
|
||||
post = frontmatter.loads(f.read())
|
||||
meta = post.metadata or {}
|
||||
probe.append({
|
||||
"path": p,
|
||||
"ok": True,
|
||||
"id": meta.get("id"),
|
||||
"title": meta.get("title"),
|
||||
})
|
||||
except Exception as e:
|
||||
probe.append({
|
||||
"path": p,
|
||||
"ok": False,
|
||||
"error": str(e),
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
"vault": vault_path,
|
||||
"notes_dir": notes_dir,
|
||||
"notes_dir_exists": exists,
|
||||
"notes_dir_list": ls_notes,
|
||||
"discovered_md_files": discovered,
|
||||
"probe": probe,
|
||||
})
|
Reference in New Issue
Block a user