-
Notifications
You must be signed in to change notification settings - Fork 56
/
upload.go
178 lines (155 loc) · 4.1 KB
/
upload.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
package serverHandler
import (
"errors"
"io"
"mjpclab.dev/ghfs/src/shimgo"
"mjpclab.dev/ghfs/src/util"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
const file = "file"
const dirFile = "dirfile"
const innerDirFile = "innerdirfile"
func getAvailableFilename(fsPrefix, filename string, mustAppendSuffix bool) string {
if len(fsPrefix) == 0 {
fsPrefix = "/"
} else if fsPrefix[len(fsPrefix)-1] != '/' {
fsPrefix = fsPrefix + "/"
}
if !mustAppendSuffix {
if _, err := os.Lstat(fsPrefix + filename); os.IsNotExist(err) {
return filename
}
}
filenamePrefix, filenameSuffix := util.SplitFilename(filename)
for i := 1; ; i++ {
newFilename := filenamePrefix + "-" + strconv.Itoa(i) + filenameSuffix
if _, err := os.Lstat(fsPrefix + newFilename); os.IsNotExist(err) {
return newFilename
}
}
return ""
}
func (h *aliasHandler) saveUploadFiles(authUserName, fsPrefix string, createDir, overwriteExists bool, aliasSubItems []os.FileInfo, r *http.Request) bool {
var errs []error
reader, err := r.MultipartReader()
if err != nil {
errs = append(errs, err)
return false
}
for {
part, err := reader.NextPart()
if err != nil {
if err != io.EOF {
errs = append(errs, err)
}
break
}
inputPartFilePath := part.FileName()
if len(inputPartFilePath) == 0 {
continue
}
partFilePath, ok := getCleanDirFilePath(inputPartFilePath)
if !ok {
errs = append(errs, errors.New("upload: illegal file path "+inputPartFilePath))
continue
}
filenameIndex := shimgo.Strings_LastIndexByte(partFilePath, '/')
fsInfix := ""
formname := part.FormName()
if formname == dirFile {
if filenameIndex > 0 {
fsInfix = partFilePath[0:filenameIndex]
}
} else if formname == innerDirFile { // get file path, strip first level of dir
if filenameIndex <= 0 {
continue
}
filepath := partFilePath[0:filenameIndex]
if prefixEndIndex := strings.IndexByte(filepath, '/'); prefixEndIndex > 0 {
fsInfix = filepath[prefixEndIndex+1:]
}
} else if formname == file {
// noop
} else {
errs = append(errs, errors.New("upload: unknown mode "+formname))
continue
}
filePrefix := fsPrefix
if len(fsInfix) > 0 {
if !createDir {
errs = append(errs, errors.New("upload: mkdir is not enabled for "+fsPrefix))
continue
}
if len(aliasSubItems) > 0 {
fsInfixPart1 := fsInfix
fsInfixSlashIndex := strings.IndexByte(fsInfixPart1, '/')
if fsInfixSlashIndex > 0 {
fsInfixPart1 = fsInfixPart1[0:fsInfixSlashIndex]
}
if containsItem(aliasSubItems, fsInfixPart1) {
errs = append(errs, errors.New("upload: ignore path shadowed by alias "+fsInfix))
continue
}
}
filePrefix += "/" + fsInfix
err := os.MkdirAll(filePrefix, 0755)
if err != nil {
errs = append(errs, err)
continue
}
}
filename := partFilePath
if filenameIndex >= 0 {
filename = filename[filenameIndex+1:]
}
if len(filename) == 0 {
continue
}
isFilenameAliased := len(fsInfix) == 0 && containsItem(aliasSubItems, filename)
var fsFilename string
if overwriteExists && !isFilenameAliased {
tryPath := filePrefix + "/" + filename
var info os.FileInfo
info, err = os.Lstat(tryPath)
if info != nil && !info.IsDir() {
err = os.Remove(tryPath)
if err != nil && !os.IsNotExist(err) {
errs = append(errs, err)
}
// even remove failed, still try to write content to file by TRUNCATE mode
fsFilename = filename
}
}
if len(fsFilename) == 0 {
fsFilename = getAvailableFilename(filePrefix, filename, isFilenameAliased)
}
if len(fsFilename) == 0 {
err := errors.New("no available filename for " + filename)
errs = append(errs, err)
continue
}
fsPath := filepath.Join(filePrefix, fsFilename)
h.logUpload(authUserName, filename, fsPath, r)
file, err := os.OpenFile(fsPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
errs = append(errs, err)
continue
}
_, err = io.Copy(file, part)
if err != nil {
errs = append(errs, err)
}
err = file.Close()
if err != nil {
errs = append(errs, err)
}
}
if h.logErrors(errs) {
return false
}
return true
}