github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/gateway/webdavstorageprovider.go (about)

     1  // Copyright 2018-2023 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file 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 gateway
    20  
    21  import (
    22  	"context"
    23  	"net/url"
    24  	"path"
    25  	"strings"
    26  
    27  	ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
    28  	"github.com/cs3org/reva/v2/pkg/errtypes"
    29  	"github.com/pkg/errors"
    30  )
    31  
    32  type webdavEndpoint struct {
    33  	filePath string
    34  	endpoint string
    35  	token    string
    36  }
    37  
    38  func (s *svc) extractEndpointInfo(ctx context.Context, targetURL string) (*webdavEndpoint, error) {
    39  	if targetURL == "" {
    40  		return nil, errtypes.BadRequest("gateway: ref target is an empty uri")
    41  	}
    42  
    43  	uri, err := url.Parse(targetURL)
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "gateway: error parsing target uri: "+targetURL)
    46  	}
    47  	if uri.Scheme != "webdav" {
    48  		return nil, errtypes.NotSupported("ref target does not have the webdav scheme")
    49  	}
    50  
    51  	m, err := url.ParseQuery(uri.RawQuery)
    52  	if err != nil {
    53  		return nil, errors.Wrap(err, "gateway: error parsing target resource name")
    54  	}
    55  
    56  	return &webdavEndpoint{
    57  		filePath: m["name"][0],
    58  		endpoint: uri.Host,
    59  		token:    uri.User.String(),
    60  	}, nil
    61  }
    62  
    63  func (s *svc) getWebdavEndpoint(ctx context.Context, domain string) (string, error) {
    64  	meshProvider, err := s.GetInfoByDomain(ctx, &ocmprovider.GetInfoByDomainRequest{
    65  		Domain: domain,
    66  	})
    67  	if err != nil {
    68  		return "", errors.Wrap(err, "gateway: error calling GetInfoByDomain")
    69  	}
    70  	for _, s := range meshProvider.ProviderInfo.Services {
    71  		if strings.ToLower(s.Endpoint.Type.Name) == "webdav" {
    72  			return s.Endpoint.Path, nil
    73  		}
    74  	}
    75  	return "", errtypes.NotFound(domain)
    76  }
    77  
    78  func (s *svc) getWebdavHost(ctx context.Context, domain string) (string, error) {
    79  	meshProvider, err := s.GetInfoByDomain(ctx, &ocmprovider.GetInfoByDomainRequest{
    80  		Domain: domain,
    81  	})
    82  	if err != nil {
    83  		return "", errors.Wrap(err, "gateway: error calling GetInfoByDomain")
    84  	}
    85  	for _, s := range meshProvider.ProviderInfo.Services {
    86  		if strings.ToLower(s.Endpoint.Type.Name) == "webdav" {
    87  			return s.Host, nil
    88  		}
    89  	}
    90  	return "", errtypes.NotFound(domain)
    91  }
    92  
    93  func appendNameQuery(targetURL string, nameQueries ...string) (string, error) {
    94  	uri, err := url.Parse(targetURL)
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  	q, err := url.ParseQuery(uri.RawQuery)
    99  	if err != nil {
   100  		return "", err
   101  	}
   102  	name := append([]string{q["name"][0]}, nameQueries...)
   103  	q.Set("name", path.Join(name...))
   104  	uri.RawQuery = q.Encode()
   105  	return uri.String(), nil
   106  }