Skip to content

Commit

Permalink
feat: 文件管理接口
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 23, 2023
1 parent 026e131 commit a80ddfa
Show file tree
Hide file tree
Showing 16 changed files with 2,559 additions and 3 deletions.
452 changes: 449 additions & 3 deletions app/http/controllers/file_controller.go

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions app/http/requests/file/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Archive struct {
Paths []string `form:"paths" json:"paths"`
File string `form:"file" json:"file"`
}

func (r *Archive) Authorize(ctx http.Context) error {
return nil
}

func (r *Archive) Rules(ctx http.Context) map[string]string {
return map[string]string{
"paths": "array",
"paths.*": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
"file": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_not_exists",
}
}

func (r *Archive) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Archive) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Archive) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
34 changes: 34 additions & 0 deletions app/http/requests/file/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Copy struct {
Old string `form:"old" json:"old"`
New string `form:"new" json:"new"`
}

func (r *Copy) Authorize(ctx http.Context) error {
return nil
}

func (r *Copy) Rules(ctx http.Context) map[string]string {
return map[string]string{
"old": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
"new": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$",
}
}

func (r *Copy) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Copy) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Copy) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
32 changes: 32 additions & 0 deletions app/http/requests/file/exist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Exist struct {
Path string `form:"path" json:"path"`
}

func (r *Exist) Authorize(ctx http.Context) error {
return nil
}

func (r *Exist) Rules(ctx http.Context) map[string]string {
return map[string]string{
"path": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
}
}

func (r *Exist) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Exist) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Exist) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
34 changes: 34 additions & 0 deletions app/http/requests/file/move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Move struct {
Old string `form:"old" json:"old"`
New string `form:"new" json:"new"`
}

func (r *Move) Authorize(ctx http.Context) error {
return nil
}

func (r *Move) Rules(ctx http.Context) map[string]string {
return map[string]string{
"old": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
"new": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_not_exists",
}
}

func (r *Move) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Move) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Move) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
32 changes: 32 additions & 0 deletions app/http/requests/file/not_exist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type NotExist struct {
Path string `form:"path" json:"path"`
}

func (r *NotExist) Authorize(ctx http.Context) error {
return nil
}

func (r *NotExist) Rules(ctx http.Context) map[string]string {
return map[string]string{
"path": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_not_exists",
}
}

func (r *NotExist) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *NotExist) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *NotExist) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
38 changes: 38 additions & 0 deletions app/http/requests/file/permission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Permission struct {
Path string `form:"path" json:"path"`
Mode uint `form:"mode" json:"mode" filter:"uint"`
Owner string `form:"owner" json:"owner"`
Group string `form:"group" json:"group"`
}

func (r *Permission) Authorize(ctx http.Context) error {
return nil
}

func (r *Permission) Rules(ctx http.Context) map[string]string {
return map[string]string{
"path": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
"mode": "regex:^[0-7]{3}$|uint",
"owner": "regex:^[a-zA-Z0-9_-]+$",
"group": "regex:^[a-zA-Z0-9_-]+$",
}
}

func (r *Permission) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Permission) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Permission) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
34 changes: 34 additions & 0 deletions app/http/requests/file/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Search struct {
Path string `form:"path" json:"path"`
KeyWord string `form:"keyword" json:"keyword"`
}

func (r *Search) Authorize(ctx http.Context) error {
return nil
}

func (r *Search) Rules(ctx http.Context) map[string]string {
return map[string]string{
"path": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
"keyword": "required|string",
}
}

func (r *Search) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Search) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Search) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
34 changes: 34 additions & 0 deletions app/http/requests/file/un_archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type UnArchive struct {
File string `form:"file" json:"file"`
Path string `form:"path" json:"path"`
}

func (r *UnArchive) Authorize(ctx http.Context) error {
return nil
}

func (r *UnArchive) Rules(ctx http.Context) map[string]string {
return map[string]string{
"file": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_exists",
"path": "regex:^/[a-zA-Z0-9_-]+(\\/[a-zA-Z0-9_-]+)*$|path_not_exists",
}
}

func (r *UnArchive) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UnArchive) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UnArchive) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
2 changes: 2 additions & 0 deletions app/providers/validation_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ func (receiver *ValidationServiceProvider) rules() []validation.Rule {
&rules.Exists{},
&rules.NotExists{},
&rules.Captcha{},
&rules.PathExists{},
&rules.PathNotExists{},
}
}
36 changes: 36 additions & 0 deletions app/rules/path_exist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package rules

import (
"github.com/goravel/framework/contracts/validation"
"github.com/spf13/cast"
"panel/pkg/tools"
)

type PathExists struct {
}

// Signature The name of the rule.
func (receiver *PathExists) Signature() string {
return "path_exists"
}

// Passes Determine if the validation rule passes.
func (receiver *PathExists) Passes(_ validation.Data, val any, options ...any) bool {
// 用户请求过来的数据
requestValue, err := cast.ToStringE(val)
if err != nil {
return false
}

// 判断是否为空
if len(requestValue) == 0 {
return false
}

return tools.Exists(requestValue)
}

// Message Get the validation error message.
func (receiver *PathExists) Message() string {
return "路径 %v 不存在"
}
36 changes: 36 additions & 0 deletions app/rules/path_not_exist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package rules

import (
"github.com/goravel/framework/contracts/validation"
"github.com/spf13/cast"
"panel/pkg/tools"
)

type PathNotExists struct {
}

// Signature The name of the rule.
func (receiver *PathNotExists) Signature() string {
return "path_exists"
}

// Passes Determine if the validation rule passes.
func (receiver *PathNotExists) Passes(_ validation.Data, val any, options ...any) bool {
// 用户请求过来的数据
requestValue, err := cast.ToStringE(val)
if err != nil {
return false
}

// 判断是否为空
if len(requestValue) == 0 {
return false
}

return !tools.Exists(requestValue)
}

// Message Get the validation error message.
func (receiver *PathNotExists) Message() string {
return "路径 %v 已存在"
}
Loading

0 comments on commit a80ddfa

Please sign in to comment.