fsview

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package fsview 提供受限工作目录内的文件浏览 / 管理。

路径钉在 session 的 cwd(git 项目的 worktree 或非 git 项目目录),前端只传相对路径, 后端 safeJoin 解析并校验「结果仍落在 root 内」,防 ../ 越界与符号链接逃逸。

供右侧「文件」面板:列目录(懒加载,git 仓库尊重 .gitignore)、读文件、增删改。

Index

Constants

This section is empty.

Variables

View Source
var ErrEscapesRoot = errors.New("path escapes workspace root")

ErrEscapesRoot 路径越界(试图跳出 root)。

Functions

func CreateDir

func CreateDir(root, rel string) error

CreateDir 新建目录(含父级)。

func CreateFile

func CreateFile(root, rel, content string) error

CreateFile 新建文件(含内容)。父目录自动创建;已存在则报错。

func DeletePath

func DeletePath(root, rel string) error

DeletePath 删除文件或目录(递归)。

func RenamePath

func RenamePath(root, rel, newName string) (string, error)

RenamePath 把 root/rel 改名为 newName(仅叶子名,不含路径)。 返回新的相对路径。非法名(含路径分隔符 / . / ..)拒绝。

func WriteFile

func WriteFile(root, rel, content string) error

WriteFile overwrites root/rel with content (the preview's edit/save path). The target must stay inside root (safeJoin) and must not be a directory. Parent directories are created if missing (covers save-after-delete mid-edit); an existing file's permission bits are preserved.

Types

type FileData

type FileData struct {
	Content  string `json:"content"`
	Binary   bool   `json:"binary"`
	TooLarge bool   `json:"tooLarge"`
}

FileData is the structured result of ReadFile: the text content plus the reasons content may be absent. Binary / too-large files return zero Content with the flag set, so callers (frontend preview/edit gating) decide off a typed field instead of matching placeholder strings.

func ReadFile

func ReadFile(root, rel string) (FileData, error)

ReadFile reads the text content of root/rel. Binary or oversized files return FileData with the corresponding flag set and no content.

type FileNode

type FileNode struct {
	Name  string `json:"name"`
	Path  string `json:"path"` // 相对 root 的路径(用 / 分隔,前端原样回传用于展开/操作)
	IsDir bool   `json:"isDir"`
	Size  int64  `json:"size,omitempty"` // 文件字节数
}

FileNode 树节点(一层):目录或文件。

func FuzzyFind

func FuzzyFind(root, scope, query string, limit int) ([]FileNode, error)

FuzzyFind 在 root 的 scope 子树下按 query 子串模糊匹配路径,返回最多 limit 个命中 (文件与目录都参与匹配)。

scope 限定搜索范围(相对 root 的路径,空表示整棵 root 树);先经 safeJoin 校验防越界。

query 行为:

  • 空 / 纯空白:返回 scope 的直接子项(含目录,等价 ListDir),作为 picker 初始态 —— 用户打开查找器还没输入时直接看到顶层可选项。
  • 非空:在 scope 子树内按整条相对路径(含目录段)子串匹配,大小写不敏感,命中含目录。 例如 "sub/b" 命中 "src/sub/b.go","go" 命中所有 .go 文件与名为 xxxgo 的目录。

数据源分两条:

  • git 仓库:复用 gitVisibleFiles(尊重 .gitignore);目录从文件路径隐式推导 (git 不跟踪目录,但含文件的目录必然存在)。
  • 非 git 目录:filepath.WalkDir 跳过 heavyDirs(.git / node_modules 等)整棵子树。

返回路径始终相对 root(与 ListDir 一致,前端原样回传)。limit<=0 取 defaultFuzzyLimit。 结果按路径字母序(大小写不敏感),目录与文件混合排序(fuzzy finder 语义,非 ListDir 的目录优先)。

func ListDir

func ListDir(root, rel string) ([]FileNode, error)

ListDir 列出 root/rel 的直接子项(一层,懒加载):目录在前,文件在后,均按字母序。 git 仓库下尊重 .gitignore(用 git ls-files 拿到可见集合);非 git 目录降级为 os.ReadDir。

type ImageData

type ImageData struct {
	DataURL   string `json:"dataUrl"`   // data:<mime>;base64,<b64>
	Extension string `json:"extension"` // 不含点,如 "png"
}

ImageData 是 ReadImage 的返回:dataURL 可直接喂 <img src>,扩展名供前端下载名 / 分类。

func ReadImage

func ReadImage(root, rel string) (ImageData, error)

ReadImage 读取 root/rel 的图片,返回 dataURL(data:<mime>;base64,<b64>)与扩展名。 路径钉在 root(safeJoin 防 ../ 与符号链接越界);过大或非图片报错。

mime 推断:先按扩展名(覆盖 SVG 等文本格式),扩展名缺失/未知时按内容嗅探 (http.DetectContentType);二者都拿不到 image/* 视为非图片。

Jump to

Keyboard shortcuts

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