/
uploaded_file.go
235 lines (217 loc) · 6.25 KB
/
uploaded_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
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/v5/application/handler"
"github.com/admpub/nging/v5/application/library/config"
"github.com/admpub/nging/v5/application/library/filemanager"
"github.com/admpub/nging/v5/application/library/notice"
"github.com/admpub/nging/v5/application/library/respond"
uploadLibrary "github.com/admpub/nging/v5/application/library/upload"
"github.com/admpub/nging/v5/application/registry/upload/chunk"
uploadClient "github.com/webx-top/client/upload"
uploadDropzone "github.com/webx-top/client/upload/driver/dropzone"
)
// UploadedFile 本地附件文件管理
func UploadedFile(ctx echo.Context) error {
ctx.Set(`activeURL`, `/manager/uploaded/file`)
return Uploaded(ctx, `file`)
}
func UploadedChunk(ctx echo.Context) error {
ctx.Set(`activeURL`, `/manager/uploaded/file`)
return Uploaded(ctx, `chunk`)
}
func UploadedMerged(ctx echo.Context) error {
ctx.Set(`activeURL`, `/manager/uploaded/file`)
return Uploaded(ctx, `merged`)
}
// Uploaded 本地附件文件管理
func Uploaded(ctx echo.Context, uploadType string) error {
var (
root string
canUpload bool
canEdit bool
canDelete bool
)
switch uploadType {
case `chunk`:
root = chunk.ChunkTempDir
canDelete = true
case `merged`:
root = chunk.MergeSaveDir
canDelete = true
case `file`:
canUpload = true
canEdit = true
canDelete = true
root = uploadLibrary.UploadDir
default:
return echo.ErrNotFound
}
var err error
id := ctx.Formx(`id`).Uint()
filePath := ctx.Form(`path`)
do := ctx.Form(`do`)
mgr := filemanager.New(root, config.FromFile().Sys.EditableFileMaxBytes(), ctx)
absPath := root
if err == nil && len(root) > 0 {
if len(filePath) > 0 {
filePath = filepath.Clean(filePath)
absPath = filepath.Join(root, filePath)
}
user := handler.User(ctx)
switch do {
case `edit`:
if !canEdit {
return echo.ErrNotFound
}
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`:
if !canEdit {
return echo.ErrNotFound
}
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`:
if !canEdit {
return echo.ErrNotFound
}
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`:
if !canDelete {
return echo.ErrNotFound
}
err = mgr.Remove(absPath)
if err != nil {
handler.SendFail(ctx, err.Error())
}
next := ctx.Referer()
if len(next) == 0 {
next = ctx.Request().URL().Path() + fmt.Sprintf(`?id=%d&path=%s`, id, com.URLEncode(filepath.Dir(filePath)))
}
return ctx.Redirect(next)
case `upload`:
if !canUpload {
return echo.ErrNotFound
}
var cu *uploadClient.ChunkUpload
var opts []uploadClient.ChunkInfoOpter
if user != nil {
cu = chunk.NewUploader(fmt.Sprintf(`user/%d`, user.Id))
opts = append(opts, uploadClient.OptChunkInfoMapping(uploadDropzone.MappingChunkInfo))
}
err = mgr.Upload(absPath, cu, opts...)
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 respond.Dropzone(ctx, err, nil)
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/`+uploadType+`?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.Set(`uploadType`, uploadType)
ctx.Set(`canUpload`, canUpload)
ctx.Set(`canEdit`, canEdit)
ctx.Set(`canDelete`, canDelete)
ctx.SetFunc(`Editable`, func(fileName string) bool {
if !canEdit {
return false
}
_, ok := Editable(fileName)
return ok
})
ctx.SetFunc(`Playable`, func(fileName string) string {
mime, _ := Playable(fileName)
return mime
})
ctx.SetFunc(`URLPrefix`, func() string {
return `/manager/uploaded/` + uploadType
})
if gallery {
return ctx.Render(`manager/uploaded_photo`, err)
}
return ctx.Render(`manager/uploaded_file`, err)
}
func Editable(fileName string) (string, bool) {
return config.FromFile().Sys.Editable(fileName)
}
func Playable(fileName string) (string, bool) {
return config.FromFile().Sys.Playable(fileName)
}