Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage the Nextcloud trash #4432

Merged
merged 7 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a route to restore from the trash bin on NextCloud
  • Loading branch information
nono committed Jun 27, 2024
commit f1974c0505119e8eb4d15acdcfbb694221d6fa9a
39 changes: 39 additions & 0 deletions docs/nextcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,42 @@ Content-Type: application/vnd.api+json
]
}
```

#### Status codes

- 200 OK, for a success
- 401 Unauthorized, when authentication to the NextCloud fails
- 404 Not Found, when the account is not found or the directory is not found on the NextCloud

## POST /remote/nextcloud/:account/restore/*path

This route can be used to restore a file/directory from the trashbin on the
NextCloud.

The `:account` parameter is the identifier of the NextCloud `io.cozy.account`.

The `*path` parameter is the path of the file on the NextCloud.

**Note:** a permission on `POST io.cozy.files` is required to use this route.

### Request

```http
POST /remote/nextcloud/4ab2155707bb6613a8b9463daf00381b/restore/Old HTTP/1.1
Host: cozy.example.net
Authorization: Bearer eyJhbG...
```

### Response

```http
HTTP/1.1 204 No Content
```

#### Status codes

- 204 No Content, when the file/directory has been restored
- 400 Bad Request, when the account is not configured for NextCloud
- 401 Unauthorized, when authentication to the NextCloud fails
- 404 Not Found, when the account is not found or the file/directory is not found on the NextCloud
- 409 Conflict, when a directory or file already exists where the file/directory should be restored on the NextCloud.
6 changes: 6 additions & 0 deletions model/nextcloud/nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ func (nc *NextCloud) Copy(oldPath, newPath string) error {
return nc.webdav.Copy(oldPath, newPath)
}

func (nc *NextCloud) Restore(path string) error {
path = "/trashbin/" + nc.userID + "/" + path
dst := "/trashbin/" + nc.userID + "/restore/" + filepath.Base(path)
return nc.webdav.Move(path, dst)
}

func (nc *NextCloud) ListFiles(path string) ([]jsonapi.Object, error) {
items, err := nc.webdav.List("/files/" + nc.userID + "/" + path)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ func (c *Client) Move(oldPath, newPath string) error {
u := url.URL{
Scheme: c.Scheme,
Host: c.Host,
User: url.UserPassword(c.Username, c.Password),
Path: c.BasePath + fixSlashes(newPath),
}
headers := map[string]string{
"Destination": u.String(),
"Overwrite": "F",
"Overwrite": "T",
}
res, err := c.req("MOVE", oldPath, 0, headers, nil)
if err != nil {
Expand All @@ -98,7 +97,6 @@ func (c *Client) Copy(oldPath, newPath string) error {
u := url.URL{
Scheme: c.Scheme,
Host: c.Host,
User: url.UserPassword(c.Username, c.Password),
Path: c.BasePath + fixSlashes(newPath),
}
headers := map[string]string{
Expand Down
20 changes: 20 additions & 0 deletions web/remote/nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ func nextcloudUpstream(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}

func nextcloudRestore(c echo.Context) error {
inst := middlewares.GetInstance(c)
if err := middlewares.AllowWholeType(c, permission.POST, consts.Files); err != nil {
return err
}

accountID := c.Param("account")
nc, err := nextcloud.New(inst, accountID)
if err != nil {
return wrapNextcloudErrors(err)
}

path := c.Param("*")
if err := nc.Restore(path); err != nil {
return wrapNextcloudErrors(err)
}
return c.NoContent(http.StatusNoContent)
}

func nextcloudRoutes(router *echo.Group) {
group := router.Group("/nextcloud/:account")
group.GET("/trash/*", nextcloudGetTrash)
Expand All @@ -275,6 +294,7 @@ func nextcloudRoutes(router *echo.Group) {
group.POST("/copy/*", nextcloudCopy)
group.POST("/downstream/*", nextcloudDownstream)
group.POST("/upstream/*", nextcloudUpstream)
group.POST("/restore/*", nextcloudRestore)
}

func wrapNextcloudErrors(err error) error {
Expand Down