| 123 | } |
| 124 | |
| 125 | func resourcePostHandler(fileCache FileCache) handleFunc { |
| 126 | return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { |
| 127 | if !d.user.Perm.Create || !d.Check(r.URL.Path) { |
| 128 | return http.StatusForbidden, nil |
| 129 | } |
| 130 | |
| 131 | // Directories creation on POST. |
| 132 | if strings.HasSuffix(r.URL.Path, "/") { |
| 133 | err := d.user.Fs.MkdirAll(r.URL.Path, d.settings.DirMode) |
| 134 | return errToStatus(err), err |
| 135 | } |
| 136 | |
| 137 | file, err := files.NewFileInfo(&files.FileOptions{ |
| 138 | Fs: d.user.Fs, |
| 139 | Path: r.URL.Path, |
| 140 | Modify: d.user.Perm.Modify, |
| 141 | Expand: false, |
| 142 | ReadHeader: d.server.TypeDetectionByHeader, |
| 143 | Checker: d, |
| 144 | }) |
| 145 | if err == nil { |
| 146 | if r.URL.Query().Get("override") != "true" { |
| 147 | return http.StatusConflict, nil |
| 148 | } |
| 149 | |
| 150 | // Permission for overwriting the file |
| 151 | if !d.user.Perm.Modify { |
| 152 | return http.StatusForbidden, nil |
| 153 | } |
| 154 | |
| 155 | err = delThumbs(r.Context(), fileCache, file) |
| 156 | if err != nil { |
| 157 | return errToStatus(err), err |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | err = d.RunHook(func() error { |
| 162 | info, writeErr := writeFile(d.user.Fs, r.URL.Path, r.Body, d.settings.FileMode, d.settings.DirMode) |
| 163 | if writeErr != nil { |
| 164 | return writeErr |
| 165 | } |
| 166 | |
| 167 | etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size()) |
| 168 | w.Header().Set("ETag", etag) |
| 169 | return nil |
| 170 | }, "upload", r.URL.Path, "", d.user) |
| 171 | |
| 172 | if err != nil { |
| 173 | _ = d.user.Fs.RemoveAll(r.URL.Path) |
| 174 | } |
| 175 | |
| 176 | return errToStatus(err), err |
| 177 | }) |
| 178 | } |
| 179 | |
| 180 | var resourcePutHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { |
| 181 | if !d.user.Perm.Modify || !d.Check(r.URL.Path) { |