2018-09-03 10:17:02 +00:00
|
|
|
package gonextcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
req "github.com/levigross/grequests"
|
2018-10-08 16:50:11 +00:00
|
|
|
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
|
2018-09-03 10:17:02 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFoldersList returns the groups folders
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFoldersList() (map[int]types.GroupFolder, error) {
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.groupfolders, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.GroupFoldersListResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
gfs := formatBadIDAndGroups(r.Ocs.Data)
|
|
|
|
return gfs, nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFolders returns the group folder details
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFolders(id int) (types.GroupFolder, error) {
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.groupfolders, nil, strconv.Itoa(id))
|
|
|
|
if err != nil {
|
|
|
|
return types.GroupFolder{}, err
|
|
|
|
}
|
|
|
|
var r types.GroupFoldersResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
if r.Ocs.Data.ID == 0 {
|
|
|
|
return types.GroupFolder{}, fmt.Errorf("%d is not a valid groupfolder's id", id)
|
|
|
|
}
|
|
|
|
return r.Ocs.Data.FormatGroupFolder(), nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFoldersCreate creates a group folder
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFoldersCreate(name string) (id int, err error) {
|
|
|
|
// TODO: Validate Folder name
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"mountpoint": name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
var r types.GroupFoldersCreateResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
id, _ = strconv.Atoi(r.Ocs.Data.ID)
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFoldersRename renames the group folder
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFoldersRename(groupID int, name string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"mountpoint": name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// GroupFolders's response does not give any clues about success or failure
|
|
|
|
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(groupID), "mountpoint")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO func (c *Client) GroupFoldersDelete(id int) error {
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFoldersAddGroup adds group to folder
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFoldersAddGroup(folderID int, groupName string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"group": groupName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// GroupFolders's response does not give any clues about success or failure
|
|
|
|
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFoldersRemoveGroup remove a group from the group folder
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFoldersRemoveGroup(folderID int, groupName string) error {
|
|
|
|
// GroupFolders's response does not give any clues about success or failure
|
|
|
|
_, err := c.baseRequest(http.MethodDelete, routes.groupfolders, nil, strconv.Itoa(folderID), "groups", groupName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
//GroupFoldersSetGroupPermissions set groups permissions
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) GroupFoldersSetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"permissions": strconv.Itoa(int(permission)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// GroupFolders's response does not give any clues about success or failure
|
|
|
|
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups", groupName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GroupFoldersSetQuota set quota on the group folder. quota in bytes, use -3 for unlimited
|
|
|
|
func (c *Client) GroupFoldersSetQuota(folderID int, quota int) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"quota": strconv.Itoa(int(quota)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// GroupFolders's response does not give any clues about success or failure
|
|
|
|
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "quota")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatBadIDAndGroups(g map[string]types.GroupFolderBadFormatIDAndGroups) map[int]types.GroupFolder {
|
|
|
|
var gfs = map[int]types.GroupFolder{}
|
|
|
|
for k := range g {
|
|
|
|
i, _ := strconv.Atoi(k)
|
|
|
|
d := g[k]
|
|
|
|
gfs[i] = d.FormatGroupFolder()
|
|
|
|
}
|
|
|
|
return gfs
|
|
|
|
}
|