Skip to content

Commit c7ea6b8

Browse files
allardbe-slaneolynx
authored andcommitted
Add API endpoint suitable for dput.
1 parent a20eb68 commit c7ea6b8

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

api/files.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,68 @@ func apiFilesUpload(c *gin.Context) {
185185
c.JSON(200, stored)
186186
}
187187

188+
// @Summary Upload One File
189+
// @Description **Upload one file to a directory**
190+
// @Description
191+
// @Description - file is uploaded
192+
// @Description - existing uploaded are overwritten
193+
// @Description
194+
// @Description **Example:**
195+
// @Description ```
196+
// @Description $ dput aptly aptly_0.9~dev+217+ge5d646c_i386.changes
197+
// @Description ```
198+
// @Tags Files
199+
// @Param dir path string true "Directory to upload files to. Created if does not exist"
200+
// @Param file path string true "File to upload"
201+
// @Produce json
202+
// @Success 200 {array} string "Name of uploaded file"
203+
// @Failure 400 {object} Error "Bad Request"
204+
// @Failure 404 {object} Error "Not Found"
205+
// @Failure 500 {object} Error "Internal Server Error"
206+
func apiFilesUploadOne(c *gin.Context) {
207+
if !verifyDir(c) {
208+
return
209+
}
210+
211+
path := filepath.Join(context.UploadPath(), utils.SanitizePath(c.Params.ByName("dir")))
212+
err := os.MkdirAll(path, 0777)
213+
214+
if err != nil {
215+
AbortWithJSONError(c, 500, err)
216+
return
217+
}
218+
stored := []string{}
219+
220+
destPath := filepath.Join(path, c.Params.ByName("file"))
221+
dst, err := os.Create(destPath)
222+
if err != nil {
223+
AbortWithJSONError(c, 500, err)
224+
return
225+
}
226+
defer dst.Close()
227+
228+
buf := make([]byte, 1024)
229+
for {
230+
n, err := c.Request.Body.Read(buf)
231+
if err != nil && err != io.EOF {
232+
AbortWithJSONError(c, 400, err)
233+
return
234+
}
235+
if n == 0 {
236+
break
237+
}
238+
if _, err := dst.Write(buf[:n]); err != nil {
239+
AbortWithJSONError(c, 500, err)
240+
return
241+
}
242+
}
243+
244+
stored = append(stored, filepath.Join(c.Params.ByName("dir"), c.Params.ByName("file")))
245+
246+
apiFilesUploadedCounter.WithLabelValues(c.Params.ByName("dir")).Inc()
247+
c.JSON(200, stored)
248+
}
249+
188250
// @Summary List Files
189251
// @Description **Show uploaded files in upload directory**
190252
// @Description

api/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func Router(c *ctx.AptlyContext) http.Handler {
182182
{
183183
api.GET("/files", apiFilesListDirs)
184184
api.POST("/files/:dir", apiFilesUpload)
185+
api.PUT("/files/:dir/:file", apiFilesUploadOne)
185186
api.GET("/files/:dir", apiFilesListFiles)
186187
api.DELETE("/files/:dir", apiFilesDeleteDir)
187188
api.DELETE("/files/:dir/:name", apiFilesDeleteFile)

0 commit comments

Comments
 (0)