Files
Docwell/Server/internal/httpapi/transfer.go
T
tajniak81andClaude Opus 5.5 40d515d15b Add toggles, embeds, link cards, block links, archive, version diff, space export and desktop 1.0
Editor:
- Toggles (Tiptap Details). Open state is per reader, not saved.
- Embeds for YouTube, Vimeo, Loom, Figma, CodePen, Spotify, Google Docs
  and Maps, in sandboxed frames. Other addresses become link cards.
- Link cards with title, description and picture read by docwell-server
  (GET /link-preview). The fetcher refuses private, loopback, link-local
  and CGNAT addresses after DNS resolution, redirects included.
- Block links: a margin button gives a block a short id and copies
  /p/<id>#b-<block>. Opening one jumps to the block and flashes it.
- Markdown, HTML and PDF exports handle the new blocks; HTML import reads
  <details> as a toggle.

Pages and spaces:
- History gets a Changes view: block and word diff against the previous
  version or the page as it is now.
- Archive: archived pages leave the tree and become read-only but stay
  readable; a space's Archive lists them with Unarchive. New migration
  (pages.archived_at) and page.archived / page.unarchived webhook events.
- Full space export (GET /spaces/{id}/export): a zip of Markdown or HTML
  files with subpages in folders, an index, and attachments under _files/.
  Links between exported pages and to attachments become relative paths.

Desktop 1.0:
- Several servers, each with its own sign-in; old settings files carry over.
- Session tokens in the system keychain (go-keyring), moved out of
  localStorage on first start.
- Native menu bar (File, Edit, View, Window, Help) with a Server submenu.
- docwell:// links may point at a block.
- Release workflow builds Windows NSIS, macOS DMG and Linux deb, rpm and
  AppImage installers and attaches them to the release;
  build/set-version.mjs stamps the version into their metadata.

Not yet run: the release workflow, the desktop menus by eye, and macOS or
Linux builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 14:14:14 +01:00

87 lines
3.4 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"io"
"mime"
"net/http"
"strconv"
"docwell/server/internal/access"
"docwell/server/internal/directory"
)
// TransferService is the part of directory.Service that imports and exports pages.
type TransferService interface {
ExportPage(ctx context.Context, c access.Caller, id, format string) (directory.Export, error)
ImportPages(ctx context.Context, c access.Caller, spaceID, parent string, files []directory.ImportFile) ([]directory.Page, error)
PrepareSpaceExport(ctx context.Context, c access.Caller, spaceID, format string) (*directory.SpaceExport, error)
}
// maxImportRequest caps one import request: up to 20 files, each within its
// kind's limit (5 MB of text, 25 MB for Word, 100 MB for a Notion export),
// and not more than one Notion export's worth in all.
const maxImportRequest = directory.MaxImportFile + 1<<20
func (a *api) transferRoutes(mux *routeMux) {
mux.HandleFunc("GET /api/v1/pages/{id}/export", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
e, err := a.dir.ExportPage(r.Context(), caller(r), r.PathValue("id"), r.URL.Query().Get("format"))
if err != nil {
a.writeError(w, err)
return
}
h := w.Header()
h.Set("Content-Type", e.ContentType)
h.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": e.Filename}))
h.Set("Content-Length", strconv.Itoa(len(e.Body)))
h.Set("X-Content-Type-Options", "nosniff")
_, _ = w.Write(e.Body)
}))
mux.HandleFunc("GET /api/v1/spaces/{id}/export", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
e, err := a.dir.PrepareSpaceExport(r.Context(), caller(r), r.PathValue("id"), r.URL.Query().Get("format"))
if err != nil {
a.writeError(w, err)
return
}
h := w.Header()
h.Set("Content-Type", "application/zip")
h.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": e.Filename}))
h.Set("X-Content-Type-Options", "nosniff")
// The zip streams as it is built; a failure half-way can only cut it short.
if err := e.WriteTo(r.Context(), w); err != nil {
a.log.Warn("space export failed", "space", r.PathValue("id"), "err", err)
}
}))
mux.HandleFunc("POST /api/v1/spaces/{id}/import", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxImportRequest)
if err := r.ParseMultipartForm(32 << 20); err != nil { // Larger uploads spill to temporary files.
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid", "message": "expected multipart/form-data up to 100 MB"})
return
}
defer func() { _ = r.MultipartForm.RemoveAll() }()
var files []directory.ImportFile
for _, fh := range r.MultipartForm.File["file"] {
if len(files) == 20 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid", "message": "import at most 20 files at once"})
return
}
f, err := fh.Open()
if err != nil {
a.writeError(w, err)
return
}
data, err := io.ReadAll(io.LimitReader(f, directory.MaxImportFile+1))
_ = f.Close()
if err != nil {
a.writeError(w, err)
return
}
files = append(files, directory.ImportFile{Name: fh.Filename, Data: data})
}
pages, err := a.dir.ImportPages(r.Context(), caller(r), r.PathValue("id"), r.FormValue("parent"), files)
a.respondStatus(w, http.StatusCreated, pages, err)
}))
}