@Summary Upload Files @Description **Upload files to a directory** @Description @Description - one or more files can be uploaded @Description - existing uploaded are overwritten @Description @Description **Example:** @Description ``` @Description $ curl -X POST -F file=@aptly_0.9~dev+217+ge5d646c_i
(c *gin.Context)
| 99 | // @Failure 500 {object} Error "Internal Server Error" |
| 100 | // @Router /api/files/{dir} [post] |
| 101 | func apiFilesUpload(c *gin.Context) { |
| 102 | if !verifyDir(c) { |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | path := filepath.Join(context.UploadPath(), utils.SanitizePath(c.Params.ByName("dir"))) |
| 107 | err := os.MkdirAll(path, 0777) |
| 108 | |
| 109 | if err != nil { |
| 110 | AbortWithJSONError(c, 500, err) |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | err = c.Request.ParseMultipartForm(10 * 1024 * 1024) |
| 115 | if err != nil { |
| 116 | AbortWithJSONError(c, 400, err) |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | stored := []string{} |
| 121 | openFiles := []*os.File{} |
| 122 | |
| 123 | // Write all files first |
| 124 | for _, files := range c.Request.MultipartForm.File { |
| 125 | for _, file := range files { |
| 126 | src, err := file.Open() |
| 127 | if err != nil { |
| 128 | // Close any files we've opened |
| 129 | for _, f := range openFiles { |
| 130 | _ = f.Close() |
| 131 | } |
| 132 | AbortWithJSONError(c, 500, err) |
| 133 | return |
| 134 | } |
| 135 | |
| 136 | destPath := filepath.Join(path, filepath.Base(file.Filename)) |
| 137 | dst, err := os.Create(destPath) |
| 138 | if err != nil { |
| 139 | _ = src.Close() |
| 140 | // Close any files we've opened |
| 141 | for _, f := range openFiles { |
| 142 | _ = f.Close() |
| 143 | } |
| 144 | AbortWithJSONError(c, 500, err) |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | _, err = io.Copy(dst, src) |
| 149 | _ = src.Close() |
| 150 | if err != nil { |
| 151 | _ = dst.Close() |
| 152 | // Close any files we've opened |
| 153 | for _, f := range openFiles { |
| 154 | _ = f.Close() |
| 155 | } |
| 156 | AbortWithJSONError(c, 500, err) |
| 157 | return |
| 158 | } |
nothing calls this directly
no test coverage detected