gonextcloud/shares.go

193 lines
5.3 KiB
Go
Raw Normal View History

2018-08-08 14:04:06 +00:00
package gonextcloud
import (
"fmt"
req "github.com/levigross/grequests"
2018-10-26 13:35:50 +00:00
"gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types"
2018-08-08 14:04:06 +00:00
"net/http"
"strconv"
"sync"
)
//SharesI available methods
type SharesI interface {
List() ([]types.Share, error)
GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error)
Get(shareID string) (types.Share, error)
Create(
path string,
shareType types.ShareType,
permission types.SharePermission,
shareWith string,
publicUpload bool,
password string,
) (types.Share, error)
Delete(shareID int) error
Update(shareUpdate types.ShareUpdate) error
UpdateExpireDate(shareID int, expireDate string) error
UpdatePublicUpload(shareID int, public bool) error
UpdatePassword(shareID int, password string) error
UpdatePermissions(shareID int, permissions types.SharePermission) error
}
//Shares contains all Shares available actions
type Shares struct {
c *Client
}
//List list all shares of the logged in user
func (s *Shares) List() ([]types.Share, error) {
res, err := s.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
}
//GetFromPath return shares from a specific file or folder
func (s *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) {
2018-08-08 14:04:06 +00:00
ro := &req.RequestOptions{
Params: map[string]string{
"path": path,
"reshares": strconv.FormatBool(reshares),
"subfiles": strconv.FormatBool(subfiles),
},
}
res, err := s.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
}
//Get information about a known Share
func (s *Shares) Get(shareID string) (types.Share, error) {
res, err := s.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
}
//Create create a share
func (s *Shares) Create(
2018-08-08 14:04:06 +00:00
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)),
},
}
res, err := s.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
}
//Delete Remove the given share.
func (s *Shares) Delete(shareID int) error {
_, err := s.c.baseRequest(http.MethodDelete, routes.shares, nil, strconv.Itoa(shareID))
2018-08-08 14:04:06 +00:00
return err
}
// Update 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 (s *Shares) Update(shareUpdate types.ShareUpdate) error {
2018-08-08 14:04:06 +00:00
errs := make(chan types.UpdateError)
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
if err := s.UpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil {
2018-08-08 14:04:06 +00:00
errs <- types.UpdateError{
Field: "password",
Error: err,
}
}
}()
go func() {
defer wg.Done()
if err := s.UpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil {
2018-08-08 14:04:06 +00:00
errs <- types.UpdateError{
Field: "expireDate",
Error: err,
}
}
}()
go func() {
defer wg.Done()
if err := s.UpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil {
2018-08-08 14:04:06 +00:00
errs <- types.UpdateError{
Field: "permissions",
Error: err,
}
}
}()
go func() {
defer wg.Done()
if err := s.UpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil {
2018-08-08 14:04:06 +00:00
errs <- types.UpdateError{
Field: "publicUpload",
Error: err,
}
}
}()
go func() {
wg.Wait()
close(errs)
}()
return types.NewUpdateError(errs)
}
//UpdateExpireDate 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 (s *Shares) UpdateExpireDate(shareID int, expireDate string) error {
return s.baseShareUpdate(strconv.Itoa(shareID), "expireDate", expireDate)
2018-08-08 14:04:06 +00:00
}
//UpdatePublicUpload enable or disable public upload
func (s *Shares) UpdatePublicUpload(shareID int, public bool) error {
return s.baseShareUpdate(strconv.Itoa(shareID), "publicUpload", strconv.FormatBool(public))
2018-08-08 14:04:06 +00:00
}
//UpdatePassword updates share password
func (s *Shares) UpdatePassword(shareID int, password string) error {
return s.baseShareUpdate(strconv.Itoa(shareID), "password", password)
2018-08-08 14:04:06 +00:00
}
//UpdatePermissions update permissions
func (s *Shares) UpdatePermissions(shareID int, permissions types.SharePermission) error {
return s.baseShareUpdate(strconv.Itoa(shareID), "permissions", strconv.Itoa(int(permissions)))
2018-08-08 14:04:06 +00:00
}
func (s *Shares) baseShareUpdate(shareID string, key string, value string) error {
2018-08-08 14:04:06 +00:00
ro := &req.RequestOptions{
Data: map[string]string{key: value},
}
_, err := s.c.baseRequest(http.MethodPut, routes.shares, ro, shareID)
2018-08-08 14:04:06 +00:00
return err
}