sharelinks

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package sharelinks holds the Share Link feature: anonymous, expiring links that grant access to one Book.

Index

Constants

View Source
const SettingsKey = "sharing"

SettingsKey is the app_settings key holding the sharing settings document.

View Source
const SharedFailedPartwayMessage = "This file couldn't be converted to this format. Download it in another format, or ask the person who shared it with you."

SharedFailedPartwayMessage is a Share Link's answer to a Download as entry whose earlier build failed partway (downloadcache.ErrFailedPartway). The recipient cannot edit the book, so it points them to the sharer.

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken() (string, error)

GenerateToken returns a new random Share Link token: 32 bytes from a cryptographically secure source, base64url encoded without padding or a prefix.

func RegisterBookRoutes

func RegisterBookRoutes(booksGroup *echo.Group, authMiddleware *auth.Middleware, shareLinkService *Service, appSettingsService *appsettings.Service)

RegisterBookRoutes registers the management routes under /books on a group that only authenticates, since a role may hold shares operations without Books Read. Each route checks its shares operation here and the book's library access in the handler.

func RegisterPublicRoutes

func RegisterPublicRoutes(api *echo.Group, shareLinkService *Service, bookService *books.Service, dlCache *downloadcache.Cache, coverCache *covers.ThumbnailCache)

RegisterPublicRoutes registers the unauthenticated recipient family under /share/:token. The server skips it in Demo Mode. Every handler calls publicHandler.resolve first, and none mounts a books handler: those require an authenticated user, so they would 401 every recipient and skip the refusal rules. A new refusal rule goes in resolve.

func RegisterRoutes

func RegisterRoutes(api *echo.Group, authMiddleware *auth.Middleware, appSettingsService *appsettings.Service)

RegisterRoutes registers the sharing settings endpoints under /settings. They live here rather than in pkg/settings because pkg/books imports pkg/settings, and this package imports pkg/books.

func SaveSettings

func SaveSettings(ctx context.Context, store *appsettings.Service, s Settings) error

SaveSettings persists the sharing settings.

Types

type CreateOptions

type CreateOptions struct {
	BookID          int
	CreatedByUserID int
	Label           *string
	ExpiresAt       *time.Time
}

CreateOptions describes a new Share Link.

type CreateShareLinkPayload

type CreateShareLinkPayload struct {
	Label     *string    `json:"label,omitempty" mod:"trim" validate:"omitempty,max=200" tstype:"string"`
	ExpiresAt *time.Time `json:"expires_at,omitempty" tstype:"string"`
}

CreateShareLinkPayload is the request body for POST /books/:id/share-links. The client turns an expiration preset into an absolute timestamp; the server knows nothing about presets. An omitted expires_at means the link never expires, which the sharing settings can forbid.

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(db *bun.DB) *Service

func (*Service) BookLibraryID

func (svc *Service) BookLibraryID(ctx context.Context, bookID int) (int, error)

BookLibraryID returns the library a book belongs to, or errcodes.NotFound.

func (*Service) Create

func (svc *Service) Create(ctx context.Context, opts CreateOptions) (*models.ShareLink, error)

Create inserts a Share Link with a fresh token and returns it with its creator loaded.

func (*Service) Delete

func (svc *Service) Delete(ctx context.Context, link *models.ShareLink) error

Delete removes a Share Link whatever its state.

func (*Service) ListForBook

func (svc *Service) ListForBook(ctx context.Context, bookID int) ([]*models.ShareLink, error)

ListForBook returns every Share Link on a book from every creator, newest first, with creators and their library access loaded.

func (*Service) RecordDownload

func (svc *Service) RecordDownload(ctx context.Context, linkID int) error

RecordDownload counts one file download and sets last used.

func (*Service) RecordOpen

func (svc *Service) RecordOpen(ctx context.Context, linkID int) error

RecordOpen counts one opening of the recipient page and sets last used.

func (*Service) RetrieveByToken

func (svc *Service) RetrieveByToken(ctx context.Context, token string) (*models.ShareLink, error)

RetrieveByToken returns the Share Link with the given token, its creator, and the creator's library access, or errcodes.NotFound.

func (*Service) RetrieveForBook

func (svc *Service) RetrieveForBook(ctx context.Context, bookID, linkID int) (*models.ShareLink, error)

RetrieveForBook returns the Share Link with the given id on the given book, or errcodes.NotFound when the link does not exist or belongs to another book.

func (*Service) Revoke

func (svc *Service) Revoke(ctx context.Context, link *models.ShareLink) (*models.ShareLink, error)

Revoke stamps revoked_at on the link. Revocation is permanent, so a link that is already revoked keeps its original time, even when two revokes race. It returns the link as stored.

type Settings

type Settings struct {
	Enabled           bool `json:"enabled"`
	RequireExpiration bool `json:"require_expiration"`
}

Settings is the admin-controlled sharing policy. It lives in the settings store rather than server config (ADR 0008). The zero value is the default: sharing off and expiration optional.

func LoadSettings

func LoadSettings(ctx context.Context, store *appsettings.Service) (Settings, error)

LoadSettings reads the sharing settings, returning the defaults if none have been saved.

type ShareLinkResponse

type ShareLinkResponse struct {
	models.ShareLink  `tstype:",extends"`
	State             string `json:"state" tstype:"ShareLinkState"`
	PausedReason      string `json:"paused_reason,omitempty" tstype:"ShareLinkPausedReason"`
	CreatedByUsername string `json:"created_by_username"`
}

ShareLinkResponse is one Share Link as the sharer sees it: the model plus its derived state, why an active link is paused (omitted when it is not), and the creator's username. The list endpoint returns a bare array of these, and create and revoke return one.

type SharedBookResponse

type SharedBookResponse struct {
	models.Book      `tstype:",extends"`
	SharedBy         string     `json:"shared_by"`
	ExpiresAt        *time.Time `json:"expires_at"`
	CoverAspectRatio string     `json:"cover_aspect_ratio" tstype:"CoverAspectRatio"`
	TagNamespaces    []string   `json:"tag_namespaces"`
}

SharedBookResponse is the book a recipient sees through a Share Link. It is the Book shape the detail page already renders, with every filesystem path and cover filename blanked and the library removed, plus who shared it, when the link expires, the library's cover aspect ratio so the cover box keeps its shape, and its tag namespaces so the tags group as they do on the book page.

type SharingSettingsResponse

type SharingSettingsResponse struct {
	Enabled           bool `json:"enabled"`
	RequireExpiration bool `json:"require_expiration"`
}

SharingSettingsResponse is the response for GET/PUT /settings/sharing.

Field shape must stay identical to Settings (same field names, same types, same order) because the handler converts between them with `SharingSettingsResponse(settings)`.

type UpdateSharingSettingsPayload

type UpdateSharingSettingsPayload struct {
	Enabled           *bool `json:"enabled,omitempty"`
	RequireExpiration *bool `json:"require_expiration,omitempty"`
}

UpdateSharingSettingsPayload is the request body for PUT /settings/sharing.

Fields are pointers so each switch can save on its own: an omitted field keeps its saved value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址