Skip to content

Commit

Permalink
[IMP](controllers) 添加修改文件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
“jony-byqc” committed Dec 19, 2022
1 parent e17978a commit 9bb62c9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
38 changes: 38 additions & 0 deletions internal/web/controllers/api_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,44 @@ func (s *Service) FilePreview(c *Context) {
}
}

// ModifyFile
// @Summary 文件修改
// @Description 文件修改
// @Param id query string true "远端文件路径"
// @Param host_id query integer true "主机 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/save [post]
func (s *Service) ModifyFile(c *Context) {
var params payload.ModifyFileParams
err := c.ShouldBind(&params)
if err != nil {
c.ResponseError(err.Error())
} else {
file := s.GetRWFile(params.HostId, params.Id)

defer file.Close()
ModifyContentByte, err1 := base64.StdEncoding.DecodeString(params.ModifyContent)
if err1 != nil {
s.Logger.Errorf("修改内容解码失败")
c.ResponseError("修改内容解码失败")
return
}
_, err2 := file.Write(ModifyContentByte)
if err2 == nil {
s.Logger.Info("文件修改成功")
c.ResponseOk("文件修改成功")
} else {
s.Logger.Errorf("文件修改失败!", err) //nolint:govet
c.ResponseError("文件修改失败")
return
}
}
}

// 这些方法暂时不重构

func (s *Service) FileUploadUnBlock(c *Context) {
Expand Down
17 changes: 17 additions & 0 deletions internal/web/controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,20 @@ func (s *Service) RunCmdWithContext(host *models.Host, cmd ssh.Command, ch chan
s.runPlayerWithContext(host, cmd, ch, ctx)
}
}

// GetRWFile 获取可读写的 sftp.File
func (s *Service) GetRWFile(hostId int, path string) *sftp.File {
host, err := models.GetHostById(hostId)
if err != nil {
return nil
}
client, err := s.sshManager.NewClientWithSftp(host)
if err != nil {
return nil
}
file, err := client.GetRWFile(path)
if err != nil {
return nil
}
return file
}
7 changes: 7 additions & 0 deletions internal/web/payload/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type OptionsFileParams struct {
HostId int `form:"host_id" binding:"required"`
}

// ModifyFileParams 解析修改文件接口使用
type ModifyFileParams struct {
Id string `json:"id"`
HostId int `json:"host_id" binding:"required"`
ModifyContent string `json:"modify_content"`
}

type MkdirParams struct {
OptionsFileParams
Dir string `form:"dir" binding:"required"`
Expand Down
1 change: 1 addition & 0 deletions internal/web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func InitRouter(s *controllers.Service) *controllers.Service {
apiV1.GET("/tools/preview", Handle(s.FilePreview))
apiV1.GET("/tools/browse", Handle(s.GetPathInfo))
apiV1.POST("/tools/mkdir", Handle(s.MakeDirRemote))
apiV1.POST("/tools/modify", Handle(s.ModifyFile))
apiV1.GET("/tools/download", Handle(s.DownLoadFile))
apiV1.POST("/tools/delete", Handle(s.DeleteFile))
apiV1.POST("/tools/upload_file", Handle(s.FileUploadUnBlock))
Expand Down
9 changes: 9 additions & 0 deletions pkg/transport/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ func (c *Client) GetFile(path string) (*sftp.File, error) {
return file, err
}

// GetRWFile 获取可读写的sftp.File 可添加参数控制 write 方式(追加/清空后再追加等等)
func (c *Client) GetRWFile(path string) (*sftp.File, error) {
file, err := c.sftpClient.OpenFile(path, os.O_RDWR|os.O_TRUNC|os.O_CREATE|os.O_SYNC)
if err != nil {
return nil, err
}
return file, err
}

func (c *Client) IsDir(path string) bool {
// 检查远程是文件还是目录
info, err := c.sftpClient.Stat(path)
Expand Down

0 comments on commit 9bb62c9

Please sign in to comment.