Files
Docwell/Server/internal/httpapi/transfer.go
T
tajniak81andClaude Opus 5.5 011833fcef Add Markdown and HTML import and export
docwell-server converts between page documents and Markdown or HTML (a
new convert package), so every client and the API get the same results.
Pages export as a download from the page menu; spaces import one page
per .md, .txt or .html file, taking a leading level-1 heading as the
title. Import keeps only what the editor schema knows, which drops
scripts, styles, event handlers and javascript: links; tables become
one paragraph per row until the editor has tables.

The UI package gains DwMenu, a small accessible action menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 17:04:39 +01:00

70 lines
2.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)
}
// maxImportRequest caps one import request: each file may be 5 MB, and up to
// 20 files may come at once, but not more than 25 MB in all.
const maxImportRequest = 25 << 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("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(maxImportRequest); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid", "message": "expected multipart/form-data up to 25 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, 5<<20+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)
}))
}