MCPcopy Index your code
hub / github.com/aptly-dev/aptly / apiFilesUpload

Function apiFilesUpload

api/files.go:101–186  ·  view source on GitHub ↗

@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)

Source from the content-addressed store, hash-verified

99// @Failure 500 {object} Error "Internal Server Error"
100// @Router /api/files/{dir} [post]
101func 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 }

Callers

nothing calls this directly

Calls 8

SanitizePathFunction · 0.92
verifyDirFunction · 0.85
AbortWithJSONErrorFunction · 0.85
UploadPathMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
ByNameMethod · 0.45
CopyMethod · 0.45

Tested by

no test coverage detected