Files
tajniak81andClaude Opus 5.5 dac6d6da63 Add public share links and the share dialog
Editors can give a page a public link that anyone can read without an
account, optionally with the pages under it. The link is the stored
token, so editors can copy it again later; turning sharing off deletes
it and the URL stops working. Reads through a link never reach trashed
pages, pages outside the shared subtree, or their attachments, and are
marked noindex.

The page header gets a Share button: the dialog explains who can open
the page through its space, lists the space's people and groups, copies
the page link and manages the public link. Shared pages open at
/share/<token> in a read-only view with a tree of the shared subpages.

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

64 lines
2.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"net/http"
"docwell/server/internal/access"
"docwell/server/internal/directory"
)
// ShareService is the part of directory.Service for public share links.
type ShareService interface {
GetShareLink(ctx context.Context, c access.Caller, pageID string) (directory.ShareLink, bool, error)
SetShareLink(ctx context.Context, c access.Caller, pageID string, includeSubpages bool) (directory.ShareLink, error)
DeleteShareLink(ctx context.Context, c access.Caller, pageID string) error
GetSharedPage(ctx context.Context, token, pageID string) (directory.SharedPage, error)
OpenSharedFile(ctx context.Context, token, fileID string, header http.Header) (directory.FileDownload, error)
}
func (a *api) shareRoutes(mux *routeMux) {
mux.HandleFunc("GET /api/v1/pages/{id}/share", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
link, ok, err := a.dir.GetShareLink(r.Context(), caller(r), r.PathValue("id"))
if err == nil && !ok {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not_found", "message": "sharing is off"})
return
}
a.respond(w, link, err)
}))
mux.HandleFunc("PUT /api/v1/pages/{id}/share", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct{ IncludeSubpages bool }
if !a.decode(w, r, &in) {
return
}
link, err := a.dir.SetShareLink(r.Context(), caller(r), r.PathValue("id"), in.IncludeSubpages)
a.respond(w, link, err)
}))
mux.HandleFunc("DELETE /api/v1/pages/{id}/share", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.DeleteShareLink(r.Context(), caller(r), r.PathValue("id")))
}))
// Public reads through a link: no account needed, never indexed.
mux.HandleFunc("GET /api/v1/share/{token}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "noindex")
p, err := a.dir.GetSharedPage(r.Context(), r.PathValue("token"), "")
a.respond(w, p, err)
})
mux.HandleFunc("GET /api/v1/share/{token}/pages/{pageId}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "noindex")
p, err := a.dir.GetSharedPage(r.Context(), r.PathValue("token"), r.PathValue("pageId"))
a.respond(w, p, err)
})
mux.HandleFunc("GET /api/v1/share/{token}/files/{fileId}", func(w http.ResponseWriter, r *http.Request) {
f, err := a.dir.OpenSharedFile(r.Context(), r.PathValue("token"), r.PathValue("fileId"), r.Header)
if err != nil {
a.writeError(w, err)
return
}
w.Header().Set("X-Robots-Tag", "noindex")
serveFile(w, f)
})
}