-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploads.go
More file actions
141 lines (115 loc) · 3.56 KB
/
Copy pathuploads.go
File metadata and controls
141 lines (115 loc) · 3.56 KB
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
package api
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/mux"
)
const (
// FileMode is the mode for the file.
FileMode = 0o640
// DirMode is the mode for the directory.
DirMode = 0o750
)
// statusError carries an HTTP status for errors returned to clients as JSON.
type statusError struct {
Code int
Msg string
}
func (e *statusError) Error() string {
return e.Msg
}
// uploadHandler handles PUT /api/file/{path} and writes the request body to that path.
// Parent directories are created as needed. Wildcards are not permitted.
func (a *API) uploadHandler(resp http.ResponseWriter, req *http.Request) {
var se *statusError
switch relPath, created, err := a.writeUploadedFile(mux.Vars(req)["path"], req.Body); {
case errors.As(err, &se):
writeError(resp, se.Code, se.Msg)
case err != nil:
writeError(resp, http.StatusInternalServerError, err.Error())
case created:
writeJSON(resp, http.StatusCreated, map[string]string{"path": relPath}, "created")
default:
writeJSON(resp, http.StatusOK, map[string]string{"path": relPath}, "updated")
}
}
func (a *API) validatePutFilePath(rawPath string) (string, bool, error) {
path, err := a.safePath(rawPath)
if err != nil {
return "", false, &statusError{Code: http.StatusBadRequest, Msg: err.Error()}
}
if strings.ContainsRune(path, '*') {
return "", false, &statusError{
Code: http.StatusBadRequest,
Msg: "wildcards not allowed for file upload",
}
}
info, statErr := os.Stat(path)
if statErr == nil && info.IsDir() {
return "", false, &statusError{Code: http.StatusBadRequest, Msg: "path is a directory; use /api/list/"}
}
created := errors.Is(statErr, os.ErrNotExist)
if statErr != nil && !created {
return "", false, fmt.Errorf("stat: %w", statErr)
}
return path, created, nil
}
func commitUploadedFile(tmpPath, destPath string, created bool) error {
err := os.Chmod(tmpPath, FileMode) //nolint:gosec // temp path from CreateTemp under validated directory
if err != nil {
return fmt.Errorf("chmod: %w", err)
}
if !created {
err = os.Remove(destPath) //nolint:gosec // destPath returned from safePath under outputPath
if err != nil {
return fmt.Errorf("removing existing file: %w", err)
}
}
err = os.Rename(tmpPath, destPath) //nolint:gosec // paths validated; atomic replace after write
if err != nil {
return fmt.Errorf("renaming file: %w", err)
}
return nil
}
func (a *API) writeUploadedFile(rawPath string, body io.Reader) (string, bool, error) {
filePath, created, err := a.validatePutFilePath(rawPath)
if err != nil {
return "", false, err
}
dir := filepath.Dir(filePath)
err = os.MkdirAll(dir, DirMode) //nolint:gosec // dir is filepath.Dir of a path from safePath under outputPath
if err != nil {
return "", false, fmt.Errorf("creating parent directories: %w", err)
}
tmpFile, err := os.CreateTemp(dir, ".fogwillow-upload-*")
if err != nil {
return "", false, fmt.Errorf("temp file: %w", err)
}
tmpPath := tmpFile.Name()
cleanupTmp := true
defer func() {
if cleanupTmp {
_ = os.Remove(tmpPath) //nolint:gosec // temp file created in this function under validated dir
}
}()
_, err = io.Copy(tmpFile, body)
if err != nil {
_ = tmpFile.Close()
return "", false, fmt.Errorf("writing body: %w", err)
}
err = tmpFile.Close()
if err != nil {
return "", false, fmt.Errorf("closing temp file: %w", err)
}
err = commitUploadedFile(tmpPath, filePath, created)
if err != nil {
return "", false, err
}
cleanupTmp = false
return strings.TrimPrefix(filePath, a.outputPath+string(filepath.Separator)), created, nil
}