diff --git a/go.mod b/go.mod index 3bb4903..cdab7de 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/json-iterator/go v1.1.12 - github.com/spf13/cast v1.5.0 + github.com/spf13/cast v1.6.0 github.com/spf13/viper v1.15.0 golang.org/x/crypto v0.7.0 ) diff --git a/go.sum b/go.sum index 291d015..8083443 100644 --- a/go.sum +++ b/go.sum @@ -156,6 +156,8 @@ github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/utils/app.go b/utils/app.go index 6663f55..0408329 100644 --- a/utils/app.go +++ b/utils/app.go @@ -6,6 +6,7 @@ func init() { Array.Remove = ArrayRemove Array.Unique = ArrayUnique[any] Array.Empty = ArrayEmpty[any] + Array.Reverse = ArrayReverse[any] Array.MapWithField = ArrayMapWithField Map.WithField = MapWithField[map[string]any] Map.WithoutField = MapWithoutField[map[string]any] @@ -23,6 +24,7 @@ var Array struct { Remove func(array []string, args ...string) (slice []string) Unique func(array []any) (slice []any) Empty func(array []any) (slice []any) + Reverse func(array []any) (slice []any) MapWithField func(array []map[string]any, field any) (slice []any) } diff --git a/utils/array.go b/utils/array.go index 2fafc6f..ffea830 100644 --- a/utils/array.go +++ b/utils/array.go @@ -70,3 +70,11 @@ func ArrayMapWithField(array []map[string]any, field any) (slice []any) { return slice } + +// ArrayReverse - 数组反转 +func ArrayReverse[T any](array []T) (slice []T) { + for i, j := 0, len(array)-1; i < j; i, j = i+1, j-1 { + array[i], array[j] = array[j], array[i] + } + return array +} diff --git a/utils/file.go b/utils/file.go index a7855b3..a6e379f 100644 --- a/utils/file.go +++ b/utils/file.go @@ -281,7 +281,12 @@ func (this *FileStruct) Download(path ...any) (result *FileResponse) { this.response.Error = err return this.response } - defer saveFile.Close() + defer func(saveFile *os.File) { + err := saveFile.Close() + if err != nil { + return + } + }(saveFile) _, err = io.Copy(saveFile, resp.Body) if err != nil { @@ -855,3 +860,40 @@ func (this *FileStruct) extract(file *zip.File, dir string) (err error) { return nil } + +// Rename 重命名文件 +func (this *FileStruct) Rename(path ...any) (result *FileResponse) { + + if len(path) != 0 { + this.request.Path = cast.ToString(path[0]) + } + + if Is.Empty(this.request.Path) { + this.response.Error = errors.New("文件路径不能为空") + return this.response + } + + if Is.Empty(this.request.Name) { + this.response.Error = errors.New("文件名不能为空") + return this.response + } + + // 判断文件是否存在 + if _, err := os.Stat(this.request.Path); os.IsNotExist(err) { + this.response.Error = err + return this.response + } + + // 重命名文件 - 放在同一个目录下 + err := os.Rename(this.request.Path, filepath.Dir(this.request.Path)+"/"+this.request.Name) + if err != nil { + this.response.Error = err + return this.response + } + + this.response.Text = "1" + this.response.Result = true + this.response.Byte = []byte{1} + + return this.response +}