-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.go
182 lines (167 loc) · 5.36 KB
/
attachment.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
/*
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 common
import (
"fmt"
"math"
"os"
"path"
"path/filepath"
"strings"
"github.com/admpub/nging/application/registry/upload/helper"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
// IsRightUploadFile 是否是正确的上传文件
func IsRightUploadFile(ctx echo.Context, src string) error {
return helper.IsRightUploadFile(ctx, src)
}
// RemoveAvatar 删除头像
func RemoveAvatar(typ string, id uint64) error {
userDir := filepath.Join(helper.UploadDir, typ, fmt.Sprint(id))
if !com.IsDir(userDir) {
return nil
}
return os.RemoveAll(userDir)
}
// MoveAvatarToUserDir 移动临时文件夹中的头像到用户目录
func MoveAvatarToUserDir(ctx echo.Context, src string, typ string, id uint64) (string, error) {
if strings.Contains(src, `://`) {
return src, nil
}
var newPath string
if err := helper.IsRightUploadFile(ctx, src); err != nil {
return newPath, err
}
name := path.Base(src)
guestFile := filepath.Join(helper.UploadDir, typ, `0`, name)
if !com.FileExists(guestFile) {
return src, nil
}
userDir := filepath.Join(helper.UploadDir, typ, fmt.Sprint(id))
os.MkdirAll(userDir, os.ModePerm)
ext := path.Ext(src)
userFile := userDir + echo.FilePathSeparator + `avatar` + ext
err := os.Rename(guestFile, userFile)
if err != nil {
return newPath, err
}
newPath = helper.UploadURLPath + typ + `/` + fmt.Sprint(id) + `/` + name
p := strings.LastIndex(guestFile, `.`)
if p > 0 {
filePrefix := guestFile[0:p] + `_`
guestFiles, err := filepath.Glob(filePrefix + `*` + guestFile[p:])
if err != nil {
return newPath, err
}
for _, file := range guestFiles {
name := filepath.Base(file)
name = strings.TrimPrefix(name, filePrefix)
userFile := userDir + echo.FilePathSeparator + `avatar_` + name
err := os.Rename(file, userFile)
if err != nil {
return newPath, err
}
}
}
return newPath, err
}
// DirSharding 文件夹分组(暂不使用)
func DirSharding(id uint64) uint64 {
return uint64(math.Ceil(float64(id) / float64(50000)))
}
// RemoveUploadedFile 删除被上传的文件
func RemoveUploadedFile(typ string, id uint64) error {
sdir := filepath.Join(helper.UploadDir, typ, fmt.Sprint(id))
if !com.IsDir(sdir) {
return nil
}
return os.RemoveAll(sdir)
}
// MoveUploadedFileToOwnerDir 移动上传的文件到所有者目录
func MoveUploadedFileToOwnerDir(ctx echo.Context, src string, typ string, id uint64) (string, error) {
var newPath string
if err := helper.IsRightUploadFile(ctx, src); err != nil {
return newPath, err
}
name := path.Base(src)
unownedFile := filepath.Join(helper.UploadDir, typ, `0`, name)
if !com.FileExists(unownedFile) {
return src, nil
}
sdir := filepath.Join(helper.UploadDir, typ, fmt.Sprint(id))
os.MkdirAll(sdir, os.ModePerm)
ownedFile := sdir + echo.FilePathSeparator + name
err := os.Rename(unownedFile, ownedFile)
if err != nil {
return newPath, err
}
newPath = helper.UploadURLPath + typ + `/` + fmt.Sprint(id) + `/` + name
p := strings.LastIndex(unownedFile, `.`)
if p > 0 {
filePrefix := unownedFile[0:p] + `_`
unownedFiles, err := filepath.Glob(filePrefix + `*` + unownedFile[p:])
if err != nil {
return newPath, err
}
for _, file := range unownedFiles {
name := filepath.Base(file)
userFile := sdir + echo.FilePathSeparator + name
err := os.Rename(file, userFile)
if err != nil {
return newPath, err
}
}
}
return newPath, err
}
// ModifyAsThumbnailName 将指向临时文件夹的缩略图路径改为新位置上的缩略图路径
// originName 为新位置上的原始图路径
// thumbnailName 为临时位置上的缩略图路径
func ModifyAsThumbnailName(originName, thumbnailName string) string {
name := path.Base(thumbnailName)
position := strings.Index(name, `_`)
var suffix string
if position > 0 {
suffix = name[position:]
}
if len(suffix) > 0 {
return originName[0:strings.LastIndex(originName, `.`)] + suffix
}
return originName
}
// Replacex 根据map替换
func Replacex(s string, oldAndNew map[string]string) string {
for oldName, newName := range oldAndNew {
s = strings.Replace(s, oldName, newName, -1)
}
return s
}
// MoveEmbedTemporaryFiles 转移被嵌入到文本内容中临时文件
func MoveEmbedTemporaryFiles(ctx echo.Context, content string, typ string, id uint64) (int, string, error) {
files := helper.ParseTemporaryFileName(content)
oldAndNew := map[string]string{}
for _, fileN := range files {
if _, ok := oldAndNew[fileN]; ok {
continue
}
newPath, err := MoveUploadedFileToOwnerDir(ctx, fileN, typ, id)
if err != nil {
return 0, content, err
}
oldAndNew[fileN] = newPath
}
return len(oldAndNew), Replacex(content, oldAndNew), nil
}