Skip to content

Commit

Permalink
feature: upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqingc committed Oct 10, 2018
1 parent 5031569 commit ae923f0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,28 @@ GET /default/<path to a file>

```
GET /default/<path to a directory>?restype=directory
```

### Upload a file

```
POST /default/<path to a directory where you want to put your file>
```

The request will be rejected either when the directory does not exist or when the file already exists

**Request content type**

form-data

**Request body**

|Name|Description|
|-|-|
|file|Required. File|

**Response status**

```
200 OK
```
3 changes: 3 additions & 0 deletions pkg/routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
var Router = gin.Default()

func init() {
// Set a lower memory limit for multipart forms (default is 32 MiB)
Router.MaxMultipartMemory = 8 << 20 // 8 MiB
v1 := Router.Group("/api/v1")
{
v1.GET("/default/*contentPath", v1handlers.HandleGet)
v1.PUT("/default/*contentPath", v1handlers.HandlePut)
v1.DELETE("/default/*contentPath", v1handlers.HandleDelete)
v1.PATCH("/default/*contentPath", v1handlers.HandlePatch)
v1.POST("/default/*contentPath", v1handlers.HandlePost)
}
}
61 changes: 61 additions & 0 deletions pkg/v1handlers/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package v1handlers

import (
"fmt"
"log"
"net/http"
"os"
"path"

"github.com/gin-gonic/gin"
)

func HandlePost(c *gin.Context) {
paramContentPath := c.Param("contentPath")

if err := EnsureSecurePaths(paramContentPath); err != nil {
log.Println("checkpath failed:", err)
c.String(http.StatusBadRequest, err.Error())
return
}

fullDirPath := path.Join(MountedVolume, paramContentPath)

dirInfo, err := os.Stat(fullDirPath)
if err != nil || !(dirInfo.IsDir()) {
c.String(http.StatusBadRequest, fmt.Sprintf("no such directory %s", paramContentPath))
return
}

// if there is other post request,
// it should be processed here
handleUpload(c)
return
}

func handleUpload(c *gin.Context) {
paramContentPath := c.Param("contentPath")

// upload file
// TODO: upload directory
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}

dst := path.Join(MountedVolume, paramContentPath, file.Filename)

if _, err := os.Stat(dst); !os.IsNotExist(err) {
errMsg := fmt.Sprintf("%s already exists", file.Filename)
c.String(http.StatusBadRequest, errMsg)
return
}

if err := c.SaveUploadedFile(file, dst); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}

c.String(http.StatusOK, "file uploaded")
}

0 comments on commit ae923f0

Please sign in to comment.