@Summary Upload One File @Description **Upload one file to a directory** @Description @Description - file is uploaded @Description - existing uploaded are overwritten @Description @Description **Example:** @Description ``` @Description $ dput aptly aptly_0.9~dev+217+ge5d646c_i386.changes @Descripti
(c *gin.Context)
| 205 | // @Failure 500 {object} Error "Internal Server Error" |
| 206 | // @Router /api/files/{dir}/{file} [put] |
| 207 | func apiFilesUploadOne(c *gin.Context) { |
| 208 | if !verifyDir(c) { |
| 209 | return |
| 210 | } |
| 211 | |
| 212 | fileName := c.Params.ByName("file") |
| 213 | if !verifyPath(fileName) { |
| 214 | AbortWithJSONError(c, 400, fmt.Errorf("wrong file")) |
| 215 | return |
| 216 | } |
| 217 | |
| 218 | path := filepath.Join(context.UploadPath(), utils.SanitizePath(c.Params.ByName("dir"))) |
| 219 | err := os.MkdirAll(path, 0777) |
| 220 | |
| 221 | if err != nil { |
| 222 | AbortWithJSONError(c, 500, err) |
| 223 | return |
| 224 | } |
| 225 | stored := []string{} |
| 226 | |
| 227 | destPath := filepath.Join(path, fileName) |
| 228 | dst, err := os.Create(destPath) |
| 229 | if err != nil { |
| 230 | AbortWithJSONError(c, 500, err) |
| 231 | return |
| 232 | } |
| 233 | defer func() { _ = dst.Close() }() |
| 234 | |
| 235 | if _, err = io.Copy(dst, c.Request.Body); err != nil { |
| 236 | AbortWithJSONError(c, 500, err) |
| 237 | return |
| 238 | } |
| 239 | |
| 240 | if err = syncFile(dst); err != nil { |
| 241 | AbortWithJSONError(c, 500, fmt.Errorf("error syncing file %s: %s", fileName, err)) |
| 242 | return |
| 243 | } |
| 244 | |
| 245 | stored = append(stored, filepath.Join(c.Params.ByName("dir"), fileName)) |
| 246 | |
| 247 | apiFilesUploadedCounter.WithLabelValues(c.Params.ByName("dir")).Inc() |
| 248 | c.JSON(200, stored) |
| 249 | } |
| 250 | |
| 251 | // @Summary List Files |
| 252 | // @Description **Show uploaded files in upload directory** |
nothing calls this directly
no test coverage detected