64 Star 391 Fork 127

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
vhost_file.go 4.22 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-10-07 22:29 . 添加云服务账号和文件管理
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package caddy
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/config"
"github.com/admpub/nging/application/library/filemanager"
"github.com/admpub/nging/application/library/notice"
"github.com/admpub/nging/application/model"
)
func VhostFile(ctx echo.Context) error {
var err error
id := ctx.Formx(`id`).Uint()
filePath := ctx.Form(`path`)
do := ctx.Form(`do`)
m := model.NewVhost(ctx)
err = m.Get(nil, db.Cond{`id`: id})
mgr := filemanager.New(m.Root, config.DefaultConfig.Sys.EditableFileMaxBytes, ctx)
absPath := m.Root
if err == nil && len(m.Root) > 0 {
if len(filePath) > 0 {
filePath = filepath.Clean(filePath)
absPath = filepath.Join(m.Root, filePath)
}
switch do {
case `edit`:
data := ctx.Data()
if _, ok := Editable(absPath); !ok {
data.SetInfo(ctx.T(`此文件不能在线编辑`), 0)
} else {
content := ctx.Form(`content`)
encoding := ctx.Form(`encoding`)
dat, err := mgr.Edit(absPath, content, encoding)
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetData(dat, 1)
}
}
return ctx.JSON(data)
case `rename`:
data := ctx.Data()
newName := ctx.Form(`name`)
err = mgr.Rename(absPath, newName)
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetCode(1)
}
return ctx.JSON(data)
case `mkdir`:
data := ctx.Data()
newName := ctx.Form(`name`)
err = mgr.Mkdir(filepath.Join(absPath, newName), os.ModePerm)
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetCode(1)
}
return ctx.JSON(data)
case `delete`:
err = mgr.Remove(absPath)
if err != nil {
handler.SendFail(ctx, err.Error())
}
return ctx.Redirect(ctx.Referer())
case `upload`:
err = mgr.Upload(absPath)
if err != nil {
user := handler.User(ctx)
if user != nil {
notice.OpenMessage(user.Username, `upload`)
notice.Send(user.Username, notice.NewMessageWithValue(`upload`, ctx.T(`文件上传出错`), err.Error()))
}
return ctx.JSON(echo.H{`error`: err.Error()}, 500)
}
return ctx.String(`OK`)
default:
var dirs []os.FileInfo
var exit bool
err, exit, dirs = mgr.List(absPath)
if exit {
return err
}
ctx.Set(`dirs`, dirs)
}
}
ctx.Set(`data`, m)
if filePath == `.` {
filePath = ``
}
pathSlice := strings.Split(strings.Trim(filePath, echo.FilePathSeparator), echo.FilePathSeparator)
pathLinks := make(echo.KVList, len(pathSlice))
encodedSep := filemanager.EncodedSepa
urlPrefix := ctx.Request().URL().Path() + fmt.Sprintf(`?id=%d&path=`, id) + encodedSep
for k, v := range pathSlice {
urlPrefix += com.URLEncode(v)
pathLinks[k] = &echo.KV{K: v, V: urlPrefix}
urlPrefix += encodedSep
}
ctx.Set(`pathLinks`, pathLinks)
ctx.Set(`rootPath`, strings.TrimSuffix(m.Root, echo.FilePathSeparator))
ctx.Set(`path`, filePath)
ctx.Set(`absPath`, absPath)
ctx.SetFunc(`Editable`, func(fileName string) bool {
_, ok := Editable(fileName)
return ok
})
ctx.SetFunc(`Playable`, func(fileName string) string {
mime, _ := Playable(fileName)
return mime
})
ctx.Set(`activeURL`, `/caddy/vhost`)
return ctx.Render(`caddy/file`, err)
}
func Editable(fileName string) (string, bool) {
return config.DefaultConfig.Sys.Editable(fileName)
}
func Playable(fileName string) (string, bool) {
return config.DefaultConfig.Sys.Playable(fileName)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.2.2

搜索帮助