vault

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 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 Vault

type Vault interface {
	// Root 返回消解符号链接后的绝对根路径。
	Root() string
	// Tree 返回当前的文件树快照。没有任何文档时返回一个空的根节点。
	Tree() *Node
	// ReadDoc 读取一篇 Markdown 文档,扩展名不是 .md/.markdown 时返回 ErrNotReadable。
	// 其余错误语义同 ReadAsset。
	ReadDoc(rel string) ([]byte, error)
	// ReadAsset 读取文档中引用的资源文件。
	// 路径逃逸时返回 ErrOutsideRoot,文件不存在时返回 os.ErrNotExist,
	// 路径在根内但不可读(目录、设备文件、点目录内的非资源文件,或 I/O 错误)时返回 ErrNotReadable。
	ReadAsset(rel string) ([]byte, error)
	// 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