2018-08-08 14:04:06 +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-08-08 14:04:06 +00:00
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//SharesList list all shares of the logged in user
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) SharesList() ([]types.Share, error) {
|
2018-09-03 10:17:02 +00:00
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.shares, nil)
|
2018-08-08 14:04:06 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var r types.SharesListResponse
|
|
|
|
|
res.JSON(&r)
|
|
|
|
|
return r.Ocs.Data, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//Shares return shares from a specific file or folder
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) Shares(path string, reshares bool, subfiles bool) ([]types.Share, error) {
|
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
|
Params: map[string]string{
|
|
|
|
|
"path": path,
|
|
|
|
|
"reshares": strconv.FormatBool(reshares),
|
|
|
|
|
"subfiles": strconv.FormatBool(subfiles),
|
|
|
|
|
},
|
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.shares, ro)
|
2018-08-08 14:04:06 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var r types.SharesListResponse
|
|
|
|
|
res.JSON(&r)
|
|
|
|
|
return r.Ocs.Data, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//Share Get information about a known Share
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) Share(shareID string) (types.Share, error) {
|
2018-09-03 10:17:02 +00:00
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.shares, nil, shareID)
|
2018-08-08 14:04:06 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return types.Share{}, err
|
|
|
|
|
}
|
|
|
|
|
var r types.SharesListResponse
|
|
|
|
|
res.JSON(&r)
|
|
|
|
|
return r.Ocs.Data[0], nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//ShareCreate create a share
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) ShareCreate(
|
|
|
|
|
path string,
|
|
|
|
|
shareType types.ShareType,
|
|
|
|
|
permission types.SharePermission,
|
|
|
|
|
shareWith string,
|
|
|
|
|
publicUpload bool,
|
|
|
|
|
password string,
|
|
|
|
|
) (types.Share, error) {
|
|
|
|
|
|
|
|
|
|
if (shareType == types.UserShare || shareType == types.GroupShare) && shareWith == "" {
|
|
|
|
|
return types.Share{}, fmt.Errorf("shareWith cannot be empty for ShareType UserShare or GroupShare")
|
|
|
|
|
}
|
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
|
Data: map[string]string{
|
|
|
|
|
"path": path,
|
|
|
|
|
"shareType": strconv.Itoa(int(shareType)),
|
|
|
|
|
"shareWith": shareWith,
|
|
|
|
|
"publicUpload": strconv.FormatBool(publicUpload),
|
|
|
|
|
"password": password,
|
|
|
|
|
"permissions": strconv.Itoa(int(permission)),
|
|
|
|
|
},
|
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
|
res, err := c.baseRequest(http.MethodPost, routes.shares, ro)
|
2018-08-08 14:04:06 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return types.Share{}, err
|
|
|
|
|
}
|
|
|
|
|
var r types.SharesResponse
|
|
|
|
|
res.JSON(&r)
|
|
|
|
|
return r.Ocs.Data, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//ShareDelete Remove the given share.
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) ShareDelete(shareID int) error {
|
2018-09-03 10:17:02 +00:00
|
|
|
|
_, err := c.baseRequest(http.MethodDelete, routes.shares, nil, strconv.Itoa(shareID))
|
2018-08-08 14:04:06 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
// ShareUpdate update share details
|
2018-08-08 14:04:06 +00:00
|
|
|
|
// expireDate expireDate expects a well formatted date string, e.g. ‘YYYY-MM-DD’
|
|
|
|
|
func (c *Client) ShareUpdate(shareUpdate types.ShareUpdate) error {
|
|
|
|
|
errs := make(chan types.UpdateError)
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(4)
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := c.ShareUpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil {
|
|
|
|
|
errs <- types.UpdateError{
|
|
|
|
|
Field: "password",
|
|
|
|
|
Error: err,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := c.ShareUpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil {
|
|
|
|
|
errs <- types.UpdateError{
|
|
|
|
|
Field: "expireDate",
|
|
|
|
|
Error: err,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := c.ShareUpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil {
|
|
|
|
|
errs <- types.UpdateError{
|
|
|
|
|
Field: "permissions",
|
|
|
|
|
Error: err,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := c.ShareUpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil {
|
|
|
|
|
errs <- types.UpdateError{
|
|
|
|
|
Field: "publicUpload",
|
|
|
|
|
Error: err,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
go func() {
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(errs)
|
|
|
|
|
}()
|
|
|
|
|
return types.NewUpdateError(errs)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
// ShareUpdateExpireDate updates the share's expire date
|
2018-08-08 14:04:06 +00:00
|
|
|
|
// expireDate expects a well formatted date string, e.g. ‘YYYY-MM-DD’
|
|
|
|
|
func (c *Client) ShareUpdateExpireDate(shareID int, expireDate string) error {
|
|
|
|
|
return c.baseShareUpdate(strconv.Itoa(shareID), "expireDate", expireDate)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//ShareUpdatePublicUpload enable or disable public upload
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) ShareUpdatePublicUpload(shareID int, public bool) error {
|
|
|
|
|
return c.baseShareUpdate(strconv.Itoa(shareID), "publicUpload", strconv.FormatBool(public))
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//ShareUpdatePassword updates share password
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) ShareUpdatePassword(shareID int, password string) error {
|
|
|
|
|
return c.baseShareUpdate(strconv.Itoa(shareID), "password", password)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:24:44 +00:00
|
|
|
|
//ShareUpdatePermissions update permissions
|
2018-08-08 14:04:06 +00:00
|
|
|
|
func (c *Client) ShareUpdatePermissions(shareID int, permissions types.SharePermission) error {
|
|
|
|
|
return c.baseShareUpdate(strconv.Itoa(shareID), "permissions", strconv.Itoa(int(permissions)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) baseShareUpdate(shareID string, key string, value string) error {
|
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
|
Data: map[string]string{key: value},
|
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
|
_, err := c.baseRequest(http.MethodPut, routes.shares, ro, shareID)
|
2018-08-08 14:04:06 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|