v1.0.8
This commit is contained in:
兔子
2023-04-14 17:48:27 +08:00
parent c699bc7d0c
commit a40ce9169d
3 changed files with 70 additions and 44 deletions

View File

@@ -161,9 +161,11 @@ var Parse struct {
ParamsBefore func(params url.Values) (result map[string]any)
Params func(params map[string]any) (result map[string]any)
}
var Net struct {
Tcping func(host any, opts ...map[string]any) (ok bool, detail []map[string]any)
}
var Mime struct {
Type func(suffix any) (mime string)
}
}

9
utils/basics.go Normal file
View File

@@ -0,0 +1,9 @@
package utils
// ForMap - 遍历数组,返回新数组
func ForMap[T any](slice []T, fun func(item T) (result T)) (newSlice []T) {
for key, val := range slice {
slice[key] = fun(val)
}
return slice
}

View File

@@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync"
)
@@ -20,9 +21,9 @@ type FileStruct struct {
type FileRequest struct {
// 文件名
Name string
// 文件路径
// 文件路径(包含文件名)
Path string
// 目录路径
// 目录路径(不包含文件名)
Dir string
// 文件后缀
Ext string
@@ -30,7 +31,14 @@ type FileRequest struct {
Limit int
// 读取偏移量
Page int
// 返回结果格式
Format string
// 是否包含子目录
Sub bool
// 域名 - 用于拼接文件路径
Domain string
// 前缀 - 用于过滤前缀
Prefix string
}
// FileResponse - File 响应
@@ -57,6 +65,18 @@ func File(request ...FileRequest) *FileStruct {
request[0].Page = 1
}
if IsEmpty(request[0].Format) {
request[0].Format = "network"
}
if IsEmpty(request[0].Sub) {
request[0].Sub = true
}
if IsEmpty(request[0].Ext) {
request[0].Ext = "*"
}
return &FileStruct{
request : &request[0],
response: &FileResponse{},
@@ -87,6 +107,18 @@ func (this *FileStruct) Ext(ext any) *FileStruct {
return this
}
// Domain 设置域名(用于拼接文件路径)
func (this *FileStruct) Domain(domain any) *FileStruct {
this.request.Domain = cast.ToString(domain)
return this
}
// Prefix 设置前缀(用于过滤前缀)
func (this *FileStruct) Prefix(prefix any) *FileStruct {
this.request.Prefix = cast.ToString(prefix)
return this
}
// Limit 设置限制行数
func (this *FileStruct) Limit(limit any) *FileStruct {
this.request.Limit = cast.ToInt(limit)
@@ -216,46 +248,17 @@ func (this *FileStruct) Byte(path ...any) (result *FileResponse) {
}
// List 获取指定目录下的所有文件
func (this *FileStruct) List(opt ...map[string]any) (result *FileResponse) {
func (this *FileStruct) List(path ...any) (result *FileResponse) {
// 默认参数
defOpt := map[string]any{
// 获取指定后缀的文件
"ext": []string{"*"},
// 包含子目录
"sub": true,
// 返回路径格式
"format": "network",
// 域名
"domain": "",
// 过滤前缀
"prefix": "",
// 目录路径
"dir": "",
}
if len(opt) != 0 {
// 合并参数
for key, val := range defOpt {
if opt[0][key] == nil {
opt[0][key] = val
}
}
} else {
// 默认参数
opt = append(opt, defOpt)
}
conf := opt[0]
if !IsEmpty(conf["dir"]) {
this.request.Dir = cast.ToString(conf["dir"])
if len(path) != 0 {
this.request.Path = cast.ToString(path[0])
}
if IsEmpty(this.request.Dir) {
this.response.Error = errors.New("目录路径不能为空")
return this.response
}
var slice []string
this.response.Error = filepath.Walk(this.request.Dir, func(path string, info os.FileInfo, err error) error {
// 忽略当前目录
@@ -263,13 +266,19 @@ func (this *FileStruct) List(opt ...map[string]any) (result *FileResponse) {
return nil
}
// 忽略子目录
if !conf["sub"].(bool) && filepath.Dir(path) != path {
if !this.request.Sub && filepath.Dir(path) != path {
return nil
}
// []string 转 []any
var exts []any
for _, v := range conf["ext"].([]string) {
exts = append(exts, v)
// this.request.Ext 逗号分隔的字符串 转 []string
for _, val := range strings.Split(this.request.Ext, ",") {
// 忽略空字符串
if IsEmpty(val) {
continue
}
// 去除空格
exts = append(exts, strings.TrimSpace(val))
}
// 忽略指定后缀
if !InArray("*", exts) && !InArray(filepath.Ext(path), exts) {
@@ -280,16 +289,22 @@ func (this *FileStruct) List(opt ...map[string]any) (result *FileResponse) {
})
// 转码为网络路径
if conf["format"] == "network" {
if this.request.Format == "network" {
for key, val := range slice {
slice[key] = filepath.ToSlash(val)
if !IsEmpty(conf["domain"]) {
slice[key] = cast.ToString(conf["domain"]) + slice[key][len(cast.ToString(conf["prefix"])):]
if !IsEmpty(this.request.Domain) {
slice[key] = this.request.Domain + slice[key][len(this.request.Prefix):]
}
}
}
this.response.Slice = cast.ToSlice(slice)
for _, val := range slice {
this.response.Slice = append(this.response.Slice, val)
}
this.response.Result = slice
this.response.Text = strings.Join(slice, ",")
this.response.Byte = []byte(this.response.Text)
return this.response
}