2026-05-30 18:55:50 +08:00
|
|
|
package file
|
|
|
|
|
|
2026-05-31 20:36:10 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"apigo.cc/go/jsmod"
|
|
|
|
|
)
|
2026-05-30 18:55:50 +08:00
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
jsmod.Register("file", map[string]any{
|
2026-05-31 20:36:10 +08:00
|
|
|
// 读操作 (映射到私有包装器)
|
2026-06-10 02:19:58 +08:00
|
|
|
"Exists": func(ctx context.Context, path string) bool {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return Exists(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Read": func(ctx context.Context, path string) (string, error) {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return Read(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"ReadBytes": func(ctx context.Context, path string) ([]byte, error) {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ReadBytes(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"ReadLines": func(ctx context.Context, path string) ([]string, error) {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ReadLines(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"ReadDir": func(ctx context.Context, path string) ([]FileInfo, error) {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ReadDir(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"GetFileInfo": func(ctx context.Context, path string) *FileInfo {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return GetFileInfo(p)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 写操作
|
2026-06-10 02:19:58 +08:00
|
|
|
"Write": func(ctx context.Context, path string, content string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Write(p, content)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"WriteBytes": func(ctx context.Context, path string, content []byte) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return WriteBytes(p, content)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Remove": func(ctx context.Context, path string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Remove(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Mkdir": func(ctx context.Context, path string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Mkdir(p)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Copy": func(ctx context.Context, from, to string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
pFrom, err := VerifyPathForSafeMode(ctx, from)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pTo, err := VerifyPathForSafeMode(ctx, to)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Copy(pFrom, pTo)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Move": func(ctx context.Context, from, to string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
pFrom, err := VerifyPathForSafeMode(ctx, from)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pTo, err := VerifyPathForSafeMode(ctx, to)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Move(pFrom, pTo)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Replace": func(ctx context.Context, path, old, new string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Replace(p, old, new)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 序列化
|
2026-06-10 02:19:58 +08:00
|
|
|
"UnmarshalFile": func(ctx context.Context, path string, to any) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return UnmarshalFile(p, to)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"MarshalFile": func(ctx context.Context, path string, data any) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return MarshalFile(p, data)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"MarshalFilePretty": func(ctx context.Context, path string, data any) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
p, err := VerifyPathForSafeMode(ctx, path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return MarshalFilePretty(p, data)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 归档
|
2026-06-10 02:19:58 +08:00
|
|
|
"Archive": func(ctx context.Context, src, dest string) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
pSrc, err := VerifyPathForSafeMode(ctx, src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pDest, err := VerifyPathForSafeMode(ctx, dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Archive(pSrc, pDest)
|
|
|
|
|
},
|
2026-06-10 02:19:58 +08:00
|
|
|
"Extract": func(ctx context.Context, src, dest string, strip bool) error {
|
2026-05-31 20:36:10 +08:00
|
|
|
pSrc, err := VerifyPathForSafeMode(ctx, src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pDest, err := VerifyPathForSafeMode(ctx, dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return Extract(pSrc, pDest, strip)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 压缩工具 (无路径,不校验)
|
2026-06-10 02:19:58 +08:00
|
|
|
"Compress": Compress,
|
|
|
|
|
"Decompress": Decompress,
|
2026-05-31 20:36:10 +08:00
|
|
|
})
|
2026-05-30 18:55:50 +08:00
|
|
|
}
|