// 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) }) }