62 Star 374 Fork 119

admpub / nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
uploaded_file.go 4.34 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-11-02 11:21 . update
/*
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 manager
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/webx-top/com"
"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/registry/upload/helper"
)
// UploadedFile 本地附件文件管理
func UploadedFile(ctx echo.Context) error {
var err error
id := ctx.Formx(`id`).Uint()
filePath := ctx.Form(`path`)
do := ctx.Form(`do`)
root := helper.UploadDir
mgr := filemanager.New(root, config.DefaultConfig.Sys.EditableFileMaxBytes, ctx)
absPath := root
if err == nil && len(root) > 0 {
if len(filePath) > 0 {
filePath = filepath.Clean(filePath)
absPath = filepath.Join(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)
}
}
if filePath == `.` {
filePath = ``
}
gallery := ctx.Formx(`gallery`).Bool()
var urlParam string
if gallery {
urlParam += `&gallery=1`
}
pathSlice := strings.Split(strings.Trim(filePath, echo.FilePathSeparator), echo.FilePathSeparator)
pathLinks := make(echo.KVList, len(pathSlice))
encodedSep := filemanager.EncodedSepa
urlPrefix := fmt.Sprintf(`/manager/uploaded_file?id=%d`+urlParam+`&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(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
})
if gallery {
return ctx.Render(`manager/uploaded_photo`, err)
}
return ctx.Render(`manager/uploaded_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)
}
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.2.3

搜索帮助