Import (Import on a space's page list, or POST /spaces/{id}/import):
- Word (.docx, up to 25 MB): Title and heading styles, bulleted and
numbered lists with nesting, quotes, code styles, bold, italic,
underline, strikethrough, links (including HYPERLINK fields) and
images, which are stored as attachments of the new page. Tables come in
as one paragraph per row, as in HTML import.
- Notion export (.zip, Markdown & CSV or HTML, up to 100 MB and 2000
pages, split exports included): rebuilds the page tree, points links
between the pages (relative and notion.so ones) at the new pages, and
stores images and files as attachments. A database becomes a page that
lists its rows, with the row pages under it. The response lists the
pages made at the parent's level.
- Every file is read before any page is made, so a bad file leaves
nothing behind. Archives are capped by bytes actually unpacked, not by
the sizes they declare.
PDF export (page menu, or ?format=pdf): A4 with the Go fonts (Latin,
Polish, Greek, Cyrillic), a bookmark per heading and page numbers.
Images on the page are drawn in (JPEG as is; PNG, GIF and WebP redrawn);
images on other sites are not fetched. Diagrams become a link to their
file and Mermaid charts show their source. Emoji are left out, since the
fonts have none.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
2.6 KiB
Go
71 lines
2.6 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: 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("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)
|
|
}))
|
|
}
|