github.com/cs3org/reva/v2@v2.27.7/pkg/sdk/common/net/webdav.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this filePath except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package net 20 21 import ( 22 "fmt" 23 "io" 24 "strconv" 25 26 types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" 27 "github.com/studio-b12/gowebdav" 28 29 "github.com/cs3org/reva/v2/pkg/sdk/common" 30 ) 31 32 const ( 33 // WebDAVTokenName is the header name of the WebDAV token. 34 WebDAVTokenName = "webdav-token" 35 // WebDAVPathName is the header name of the WebDAV file path. 36 WebDAVPathName = "webdav-file-path" 37 ) 38 39 // WebDAVClient is a simple client wrapper for down- and uploading files via WebDAV. 40 type WebDAVClient struct { 41 client *gowebdav.Client 42 } 43 44 func (webdav *WebDAVClient) initClient(endpoint string, userName string, password string, accessToken string) error { 45 // Create the WebDAV client 46 webdav.client = gowebdav.NewClient(endpoint, userName, password) 47 48 if accessToken != "" { 49 webdav.client.SetHeader(AccessTokenName, accessToken) 50 } 51 52 return nil 53 } 54 55 // Read reads all data of the specified remote file. 56 func (webdav *WebDAVClient) Read(file string) ([]byte, error) { 57 reader, err := webdav.client.ReadStream(file) 58 if err != nil { 59 return nil, fmt.Errorf("unable to create reader: %v", err) 60 } 61 defer reader.Close() 62 63 data, err := io.ReadAll(reader) 64 if err != nil { 65 return nil, fmt.Errorf("unable to read the data: %v", err) 66 } 67 return data, nil 68 } 69 70 // Write writes data to the specified remote file. 71 func (webdav *WebDAVClient) Write(file string, data io.Reader, size int64) error { 72 webdav.client.SetHeader("Upload-Length", strconv.FormatInt(size, 10)) 73 74 if err := webdav.client.WriteStream(file, data, 0700, ""); err != nil { 75 return fmt.Errorf("unable to write the data: %v", err) 76 } 77 78 return nil 79 } 80 81 // Remove deletes the entire file/path. 82 func (webdav *WebDAVClient) Remove(path string) error { 83 if err := webdav.client.Remove(path); err != nil { 84 return fmt.Errorf("error removing '%v' :%v", path, err) 85 } 86 87 return nil 88 } 89 90 func newWebDAVClient(endpoint string, userName string, password string, accessToken string) (*WebDAVClient, error) { 91 client := &WebDAVClient{} 92 if err := client.initClient(endpoint, userName, password, accessToken); err != nil { 93 return nil, fmt.Errorf("unable to create the WebDAV client: %v", err) 94 } 95 return client, nil 96 } 97 98 // NewWebDAVClientWithAccessToken creates a new WebDAV client using an access token. 99 func NewWebDAVClientWithAccessToken(endpoint string, accessToken string) (*WebDAVClient, error) { 100 return newWebDAVClient(endpoint, "", "", accessToken) 101 } 102 103 // NewWebDAVClientWithOpaque creates a new WebDAV client using the information stored in the opaque. 104 func NewWebDAVClientWithOpaque(endpoint string, opaque *types.Opaque) (*WebDAVClient, map[string]string, error) { 105 values, err := common.GetValuesFromOpaque(opaque, []string{WebDAVTokenName, WebDAVPathName}, true) 106 if err != nil { 107 return nil, nil, fmt.Errorf("invalid opaque object: %v", err) 108 } 109 110 client, err := NewWebDAVClientWithAccessToken(endpoint, values[WebDAVTokenName]) 111 if err != nil { 112 return nil, nil, err 113 } 114 return client, values, nil 115 } 116 117 // NewWebDAVClient creates a new WebDAV client with user credentials. 118 func NewWebDAVClient(endpoint string, userName string, password string) (*WebDAVClient, error) { 119 return newWebDAVClient(endpoint, userName, password, "") 120 }