vault

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package vault 管理一个文档根目录,是全程序唯一直接接触文件系统的单元。

Index

Constants

This section is empty.

Variables

View Source
var ErrNotReadable = errors.New("路径不是可读的文档")

ErrNotReadable 表示路径在根内但不是一篇可读的文档 (目录、设备文件、隐藏文件,或读取时发生 I/O 错误)。

存在这个哨兵是为了兜住「既不是越界、也不是不存在」的第三类情况。 没有它,这类错误只能原样透传 *fs.PathError,而那里面带着服务端的绝对路径—— 一个 ?path= 空串或 ?path=%00 就能把文档根的真实路径回显给调用方。

View Source
var ErrOutsideRoot = errors.New("路径超出文档根目录")

ErrOutsideRoot 表示请求的路径落在文档根目录之外。 返回给 HTTP 层时必须转成 400,且不得回显任何文件系统路径。

Functions

This section is empty.

Types

type Event

type Event struct {
	Kind EventKind `json:"kind"`
	Rel  string    `json:"rel"` // KindDocChanged 时为变更文件的相对路径
}

Event 是一次文件系统变更通知。

type EventKind

type EventKind int

EventKind 区分变更事件的类型。

const (
	// KindDocChanged 表示某篇文档内容发生变化。
	KindDocChanged EventKind = iota
	// KindTreeChanged 表示目录结构发生变化,需要刷新文件树。
	KindTreeChanged
)

type Node

type Node struct {
	Name     string  `json:"name"`
	Rel      string  `json:"rel"` // 相对文档根目录的路径,根节点为空串
	IsDir    bool    `json:"isDir"`
	Children []*Node `json:"children,omitempty"`
}

Node 是文件树中的一个节点。目录节点的 Children 按「目录在前、文件在后」排列, 两组各自按名称升序。

type RawDoc

type RawDoc struct {
	Rel     string
	Content []byte
}

RawDoc 是单篇文档的原始内容快照。 纯文本提取由 render 包负责,vault 不依赖它。

type Vault

type Vault interface {
	// Root 返回消解符号链接后的绝对根路径。
	Root() string
	// Tree 返回当前的文件树快照。没有任何文档时返回一个空的根节点。
	Tree() *Node
	// Read 读取根目录内的相对路径文件。
	// 路径逃逸时返回 ErrOutsideRoot,文件不存在时返回 os.ErrNotExist,
	// 路径在根内但不是可读文档(目录、设备文件、隐藏文件,或读取时发生 I/O 错误)时返回 ErrNotReadable。
	Read(rel string) ([]byte, error)
	// RawDocs 返回全部 Markdown 文档的原始内容快照。
	RawDocs() []RawDoc
	// Watch 持续推送文件变更事件,直到 ctx 结束时关闭返回的 channel。
	Watch(ctx context.Context) <-chan Event
}

Vault 管理一个文档根目录。

func New

func New(path string) (Vault, error)

New 以 path 为文档根构造 Vault。 path 可以是目录,也可以是单个 Markdown 文件(此时以其所在目录为根)。

Jump to

Keyboard shortcuts

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